From 96edeb3dca58452dc26e3afbf07448f6697bb796 Mon Sep 17 00:00:00 2001 From: Eliott Bouhana Date: Fri, 7 Feb 2025 17:25:19 +0100 Subject: [PATCH] fix issue where reducing the value of DD_TELEMETRY_HEARTBEAT_INTERVAL would not reduce the value of the flushing interval Signed-off-by: Eliott Bouhana --- internal/newtelemetry/client_config.go | 13 ++++++------- internal/newtelemetry/internal/range.go | 8 ++++++++ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/internal/newtelemetry/client_config.go b/internal/newtelemetry/client_config.go index f4d6751c0a..45776e246f 100644 --- a/internal/newtelemetry/client_config.go +++ b/internal/newtelemetry/client_config.go @@ -83,7 +83,7 @@ var ( agentlessURL = "https://instrumentation-telemetry-intake.datadoghq.com/api/v2/apmtelemetry" // defaultHeartbeatInterval is the default interval at which the agent sends a heartbeat. - defaultHeartbeatInterval = time.Minute // seconds + defaultHeartbeatInterval = time.Minute // defaultExtendedHeartbeatInterval is the default interval at which the agent sends an extended heartbeat. defaultExtendedHeartbeatInterval = 24 * time.Hour @@ -162,12 +162,6 @@ func defaultConfig(config ClientConfig) ClientConfig { } } - if config.HeartbeatInterval == 0 { - config.HeartbeatInterval = globalinternal.DurationEnv("DD_TELEMETRY_HEARTBEAT_INTERVAL", defaultHeartbeatInterval) - } else { - config.HeartbeatInterval = defaultAuthorizedHearbeatRange.Clamp(config.HeartbeatInterval) - } - if config.FlushInterval.Min == 0 { config.FlushInterval.Min = defaultFlushIntervalRange.Min } else { @@ -180,6 +174,11 @@ func defaultConfig(config ClientConfig) ClientConfig { config.FlushInterval.Max = defaultAuthorizedHearbeatRange.Clamp(config.FlushInterval.Max) } + envVal := globalinternal.FloatEnv("DD_TELEMETRY_HEARTBEAT_INTERVAL", defaultHeartbeatInterval.Seconds()) + config.HeartbeatInterval = defaultAuthorizedHearbeatRange.Clamp(time.Duration(envVal * float64(time.Second))) + // Make sure we flush at least at each heartbeat interval + config.FlushInterval = config.FlushInterval.ReduceMax(config.HeartbeatInterval) + if config.DependencyLoader == nil && globalinternal.BoolEnv("DD_TELEMETRY_DEPENDENCY_COLLECTION_ENABLED", true) { config.DependencyLoader = debug.ReadBuildInfo } diff --git a/internal/newtelemetry/internal/range.go b/internal/newtelemetry/internal/range.go index c084dc51de..c2e1e1852f 100644 --- a/internal/newtelemetry/internal/range.go +++ b/internal/newtelemetry/internal/range.go @@ -29,3 +29,11 @@ func (r Range[T]) Contains(value T) bool { func (r Range[T]) Clamp(value T) T { return max(min(r.Max, value), r.Min) } + +// ReduceMax returns a new range where value is the new max and min is either the current min or the new value to make sure the range is ordered. +func (r Range[T]) ReduceMax(value T) Range[T] { + return Range[T]{ + Min: min(r.Min, value), + Max: value, + } +}