Skip to content
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

profiler: add enable flag to control profiler activation #2840

Merged
merged 8 commits into from
Oct 25, 2024
3 changes: 3 additions & 0 deletions profiler/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ type config struct {
logStartup bool
traceConfig executionTraceConfig
endpointCountEnabled bool
enable bool
}

// logStartup records the configuration to the configured logger in JSON format
Expand Down Expand Up @@ -146,6 +147,7 @@ func logStartup(c *config) {
"execution_trace_size_limit": c.traceConfig.Limit,
"endpoint_count_enabled": c.endpointCountEnabled,
"custom_profiler_label_keys": c.customProfilerLabels,
"enable": c.enable,
felixge marked this conversation as resolved.
Show resolved Hide resolved
}
b, err := json.Marshal(info)
if err != nil {
Expand Down Expand Up @@ -208,6 +210,7 @@ func defaultConfig() (*config, error) {
} else {
c.agentURL = url.String() + "/profiling/v1/input"
}
c.enable = internal.BoolEnv("DD_PROFILING_ENABLED", true)
felixge marked this conversation as resolved.
Show resolved Hide resolved
if v := os.Getenv("DD_PROFILING_UPLOAD_TIMEOUT"); v != "" {
d, err := time.ParseDuration(v)
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions profiler/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,21 @@ func TestEnvVars(t *testing.T) {
assert.Equal(t, "http://localhost:6218/profiling/v1/input", cfg.agentURL)
})

t.Run("DD_PROFILING_ENABLED", func(t *testing.T) {
t.Run("default", func(t *testing.T) {
cfg, err := defaultConfig()
require.NoError(t, err)
assert.Equal(t, true, cfg.enable)
})

t.Run("override", func(t *testing.T) {
t.Setenv("DD_PROFILING_ENABLED", "false")
cfg, err := defaultConfig()
require.NoError(t, err)
assert.Equal(t, false, cfg.enable)
})
})

t.Run("DD_PROFILING_UPLOAD_TIMEOUT", func(t *testing.T) {
t.Setenv("DD_PROFILING_UPLOAD_TIMEOUT", "3s")
cfg, err := defaultConfig()
Expand Down
3 changes: 3 additions & 0 deletions profiler/profiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ func Start(opts ...Option) error {
if err != nil {
return err
}
if !p.cfg.enable {
return nil
}
activeProfiler = p
activeProfiler.run()
traceprof.SetProfilerEnabled(true)
Expand Down
14 changes: 14 additions & 0 deletions profiler/profiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ func TestStart(t *testing.T) {
mu.Unlock()
})

t.Run("dd_profiling_not_enabled", func(t *testing.T) {
t.Setenv("DD_PROFILING_ENABLED", "false")
if err := Start(); err != nil {
t.Fatal(err)
}
defer Stop()

mu.Lock()
// if DD_PROFILING_ENABLED is false, the profiler should not be started even if Start() is called
// So we should not have an activeProfiler
assert.Nil(t, activeProfiler)
mu.Unlock()
})
felixge marked this conversation as resolved.
Show resolved Hide resolved

t.Run("options", func(t *testing.T) {
if err := Start(); err != nil {
t.Fatal(err)
Expand Down
Loading