-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Discover service names from process env vars (#1195)
- Loading branch information
Showing
12 changed files
with
162 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package exec | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/prometheus/procfs" | ||
) | ||
|
||
func envStrsToMap(varsStr []string) map[string]string { | ||
vars := make(map[string]string, len(varsStr)) | ||
|
||
for _, s := range varsStr { | ||
keyVal := strings.SplitN(s, "=", 2) | ||
if len(keyVal) < 2 { | ||
continue | ||
} | ||
key := strings.TrimSpace(keyVal[0]) | ||
val := strings.TrimSpace(keyVal[1]) | ||
|
||
if key != "" && val != "" { | ||
vars[key] = val | ||
} | ||
} | ||
|
||
return vars | ||
} | ||
|
||
func EnvVars(pid int32) (map[string]string, error) { | ||
proc, err := procfs.NewProc(int(pid)) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
varsStr, err := proc.Environ() | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return envStrsToMap(varsStr), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package exec | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestEnvStrParsing(t *testing.T) { | ||
strs := []string{ | ||
"ok=\"= =\"", | ||
"nothing", | ||
"=wrong", | ||
"something=somethingelse", | ||
"something_empty=", | ||
"something= else", | ||
"weird== =", | ||
"resources=a=b,c=d,e= fg", | ||
"", | ||
} | ||
|
||
res := envStrsToMap(strs) | ||
assert.Equal(t, map[string]string{"something": "else", "ok": "\"= =\"", "weird": "= =", "resources": "a=b,c=d,e= fg"}, res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters