From 05bc673770626f3e826e278c86efb8612826fa30 Mon Sep 17 00:00:00 2001 From: Nick Ripley <97066770+nsrip-dd@users.noreply.github.com> Date: Thu, 29 Sep 2022 11:25:12 -0400 Subject: [PATCH] profiler: clamp CPU duration at profile period (#1486) If a user sets a profile period shorter than the default CPU duration, but neglects to update the CPU duration, CPU profiling will still take the default duration, exceeding the expected profile period. Add a check to clamp the CPU duration at the configured period. --- profiler/profiler.go | 3 +++ profiler/profiler_test.go | 2 -- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/profiler/profiler.go b/profiler/profiler.go index 83ba79959f..1069463a34 100644 --- a/profiler/profiler.go +++ b/profiler/profiler.go @@ -173,6 +173,9 @@ func newProfiler(opts ...Option) (*profiler, error) { return nil, fmt.Errorf("unknown profile type: %d", pt) } } + if cfg.cpuDuration > cfg.period { + cfg.cpuDuration = cfg.period + } if cfg.logStartup { logStartup(cfg) } diff --git a/profiler/profiler_test.go b/profiler/profiler_test.go index 423d05db7b..bda6602895 100644 --- a/profiler/profiler_test.go +++ b/profiler/profiler_test.go @@ -203,7 +203,6 @@ func TestStopLatency(t *testing.T) { Start( WithAgentAddr(server.Listener.Addr().String()), WithPeriod(time.Second), - CPUDuration(time.Second), WithUploadTimeout(time.Hour), ) @@ -412,7 +411,6 @@ func TestCorrectTags(t *testing.T) { HeapProfile, ), WithPeriod(10*time.Millisecond), - CPUDuration(10*time.Millisecond), WithService("xyz"), WithEnv("testing"), WithTags("foo:bar", "baz:bonk"),