Skip to content

Commit

Permalink
Make captured env vars configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
RononDex committed Jan 30, 2025
1 parent e565f5b commit 51cd4c2
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 13 deletions.
4 changes: 4 additions & 0 deletions cli_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
defaultProbabilisticInterval = 1 * time.Minute
defaultArgSendErrorFrames = false
defaultOffCPUThreshold = 0
defaultEnvVarsValue = ""

// This is the X in 2^(n + x) where n is the default hardcoded map size value
defaultArgMapScaleFactor = 0
Expand Down Expand Up @@ -67,6 +68,7 @@ var (
"Valid values are in the range [1..%d], and 0 to disable off-cpu profiling."+
"Default is %d.",
support.OffCPUThresholdMax, defaultOffCPUThreshold)
envVarsHelp = "Defines a comma seperated list of env vars that should be reported with the captured profiling samples"
)

// Package-scope variable, so that conditionally compiled other components can refer
Expand Down Expand Up @@ -123,6 +125,8 @@ func parseArgs() (*controller.Config, error) {
fs.UintVar(&args.OffCPUThreshold, "off-cpu-threshold",
defaultOffCPUThreshold, offCPUThresholdHelp)

fs.StringVar(&args.IncludeEnvVars, "env-vars", defaultEnvVarsValue, envVarsHelp)

fs.Usage = func() {
fs.PrintDefaults()
}
Expand Down
2 changes: 2 additions & 0 deletions internal/controller/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ type Config struct {
Reporter reporter.Reporter

Fs *flag.FlagSet

IncludeEnvVars string
}

const (
Expand Down
10 changes: 10 additions & 0 deletions internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controller // import "go.opentelemetry.io/ebpf-profiler/internal/control
import (
"context"
"fmt"
"strings"
"time"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -71,6 +72,14 @@ func (c *Controller) Start(ctx context.Context) error {
return fmt.Errorf("failed to start reporter: %w", err)
}

var envVars map[string]bool
splittedEnvVars := strings.Split(c.config.IncludeEnvVars, ",")
for _, envVar := range splittedEnvVars {
if envVar != "" {
envVars[envVar] = true
}
}

metrics.SetReporter(c.reporter)

// Load the eBPF code and map definitions
Expand All @@ -87,6 +96,7 @@ func (c *Controller) Start(ctx context.Context) error {
ProbabilisticInterval: c.config.ProbabilisticInterval,
ProbabilisticThreshold: c.config.ProbabilisticThreshold,
OffCPUThreshold: uint32(c.config.OffCPUThreshold),
IncludeEnvVars: envVars,
})
if err != nil {
return fmt.Errorf("failed to load eBPF tracer: %w", err)
Expand Down
3 changes: 2 additions & 1 deletion processmanager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ var (
// implementation.
func New(ctx context.Context, includeTracers types.IncludedTracers, monitorInterval time.Duration,
ebpf pmebpf.EbpfHandler, fileIDMapper FileIDMapper, symbolReporter reporter.SymbolReporter,
sdp nativeunwind.StackDeltaProvider, filterErrorFrames bool) (*ProcessManager, error) {
sdp nativeunwind.StackDeltaProvider, filterErrorFrames bool, includeEnvVars map[string]bool) (*ProcessManager, error) {
if fileIDMapper == nil {
var err error
fileIDMapper, err = newFileIDMapper(lruFileIDCacheSize)
Expand Down Expand Up @@ -101,6 +101,7 @@ func New(ctx context.Context, includeTracers types.IncludedTracers, monitorInter
reporter: symbolReporter,
metricsAddSlice: metrics.AddSlice,
filterErrorFrames: filterErrorFrames,
includeEnvVars: includeEnvVars,
}

collectInterpreterMetrics(ctx, pm, monitorInterval)
Expand Down
14 changes: 3 additions & 11 deletions processmanager/processinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"fmt"
"os"
"path"
"slices"
"strings"
"syscall"
"time"
Expand All @@ -38,15 +37,6 @@ 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) {
Expand Down Expand Up @@ -105,7 +95,9 @@ func (pm *ProcessManager) updatePidInformation(pid libpf.PID, m *Mapping) (bool,
splittedVars := strings.Split(string(envVars), "\000")
for _, envVar := range splittedVars {
keyValuePair := strings.SplitN(envVar, "=", 2)
if slices.Contains(pm_cfg.extractEnvVars, keyValuePair[0]) {
_, envVarShouldBeCaptured := pm.includeEnvVars[keyValuePair[0]]

if envVarShouldBeCaptured {
envVarMap[keyValuePair[0]] = keyValuePair[1]
}
}
Expand Down
3 changes: 3 additions & 0 deletions processmanager/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ type ProcessManager struct {

// filterErrorFrames determines whether error frames are dropped by `ConvertTrace`.
filterErrorFrames bool

// includeEnvVars holds a list of env vars that should be captured from processes
includeEnvVars map[string]bool
}

// Mapping represents an executable memory mapping of a process.
Expand Down
4 changes: 3 additions & 1 deletion tracer/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ type Config struct {
ProbabilisticThreshold uint
// OffCPUThreshold is the user defined threshold for off-cpu profiling.
OffCPUThreshold uint32
// IncludeEnvVars holds a list of environment variables that should be captured and reported from processes
IncludeEnvVars map[string]bool
}

// hookPoint specifies the group and name of the hooked point in the kernel.
Expand Down Expand Up @@ -296,7 +298,7 @@ func NewTracer(ctx context.Context, cfg *Config) (*Tracer, error) {

processManager, err := pm.New(ctx, cfg.IncludeTracers, cfg.Intervals.MonitorInterval(),
ebpfHandler, nil, cfg.Reporter, elfunwindinfo.NewStackDeltaProvider(),
cfg.FilterErrorFrames)
cfg.FilterErrorFrames, cfg.IncludeEnvVars)
if err != nil {
return nil, fmt.Errorf("failed to create processManager: %v", err)
}
Expand Down

0 comments on commit 51cd4c2

Please sign in to comment.