-
Notifications
You must be signed in to change notification settings - Fork 287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support the capturing of environment variables #327
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -16,6 +16,9 @@ import ( | |||||
"fmt" | ||||||
"os" | ||||||
"path" | ||||||
"slices" | ||||||
"strconv" | ||||||
"strings" | ||||||
"syscall" | ||||||
"time" | ||||||
|
||||||
|
@@ -36,6 +39,15 @@ import ( | |||||
"go.opentelemetry.io/ebpf-profiler/util" | ||||||
) | ||||||
|
||||||
type ProcessManagerConfig struct { | ||||||
extractEnvVars []string | ||||||
} | ||||||
|
||||||
var pm_cfg = ProcessManagerConfig{extractEnvVars: []string{ | ||||||
"PIPELINE_PPOID", | ||||||
"PIPELINE_SOFTWARENAME", | ||||||
"PIPELINE_JOBID"}} | ||||||
|
||||||
// assignTSDInfo updates the TSDInfo for the Interpreters on given PID. | ||||||
// Caller must hold pm.mu write lock. | ||||||
func (pm *ProcessManager) assignTSDInfo(pid libpf.PID, tsdInfo *tpbase.TSDInfo) { | ||||||
|
@@ -88,11 +100,26 @@ func (pm *ProcessManager) updatePidInformation(pid libpf.PID, m *Mapping) (bool, | |||||
if name, err := os.ReadFile(fmt.Sprintf("/prod/%d/comm", pid)); err == nil { | ||||||
processName = string(name) | ||||||
} | ||||||
|
||||||
envVarMap := make(map[string]string) | ||||||
if envVars, err := os.ReadFile(fmt.Sprintf("/proc/%d/environ", pid)); err == nil { | ||||||
splittedVars := strings.Split(string(envVars), "\000") | ||||||
fmt.Println("EnvVars for PID" + strconv.Itoa(int(pid))) | ||||||
for _, envVar := range splittedVars { | ||||||
keyValuePair := strings.Split(envVar, "=") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not correct as values can themselves contain "=".
Suggested change
|
||||||
if slices.Contains(pm_cfg.extractEnvVars, keyValuePair[0]) { | ||||||
envVarMap[keyValuePair[0]] = keyValuePair[1] | ||||||
fmt.Println(envVar) | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
info = &processInfo{ | ||||||
name: processName, | ||||||
executable: exePath, | ||||||
mappings: make(map[libpf.Address]*Mapping), | ||||||
mappingsByFileID: make(map[host.FileID]map[libpf.Address]*Mapping), | ||||||
envVariables: envVarMap, | ||||||
tsdInfo: nil, | ||||||
} | ||||||
pm.pidToProcessInfo[pid] = info | ||||||
|
@@ -730,3 +757,14 @@ func (pm *ProcessManager) ProcessedUntil(traceCaptureKTime times.KTime) { | |||||
|
||||||
// Compile time check to make sure we satisfy the interface. | ||||||
var _ tracehandler.TraceProcessor = (*ProcessManager)(nil) | ||||||
|
||||||
func (pm *ProcessManager) EnvVarsForPID(pid libpf.PID) map[string]string { | ||||||
var envVars map[string]string | ||||||
|
||||||
pm.mu.RLock() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How critical are information about environment variables, that they should hold a global lock on the ProcessManager? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When a user requires this information, it has the same criticality as data from other *ForPID functions. In I'd suggest to fetch the data with a single function call / single lock in a follow-up PR. |
||||||
defer pm.mu.RUnlock() | ||||||
if procInfo, ok := pm.pidToProcessInfo[pid]; ok { | ||||||
envVars = procInfo.envVariables | ||||||
} | ||||||
return envVars | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest using a map for faster (O(1)) lookup.
Also, this needs to be configurable, as you already said.
This map of env vars could be part of
tracer/Config
and be passed to processmanager.New().Then it needs a configuration option exposed to the user, see
cli_flags.go
.The scanning of the
environ
file should only happen if that map of env variables is not empty.