Skip to content

Commit

Permalink
[NPM] Allow safely overriding connections check interval (#31044)
Browse files Browse the repository at this point in the history
  • Loading branch information
akarpz authored Nov 18, 2024
1 parent fa7d613 commit 57d7604
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 16 deletions.
8 changes: 7 additions & 1 deletion pkg/config/setup/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ const (

// DefaultProcessDiscoveryHintFrequency is the default frequency in terms of number of checks which we send a process discovery hint
DefaultProcessDiscoveryHintFrequency = 60

// DefaultConnectionsMinCheckInterval is the minimum interval allowed for the connections check
DefaultConnectionsMinCheckInterval = 5 * time.Second

// DefaultConnectionsMaxCheckInterval is the maximum interval allowed for the connections check
DefaultConnectionsMaxCheckInterval = 5 * time.Minute
)

// setupProcesses is meant to be called multiple times for different configs, but overrides apply to all configs, so
Expand Down Expand Up @@ -172,7 +178,7 @@ func setupProcesses(config pkgconfigmodel.Setup) {
"DD_PROCESS_ADDITIONAL_ENDPOINTS",
)
procBindEnvAndSetDefault(config, "process_config.events_additional_endpoints", make(map[string][]string))
config.SetKnown("process_config.intervals.connections")
procBindEnvAndSetDefault(config, "process_config.intervals.connections", 30*time.Second)
procBindEnvAndSetDefault(config, "process_config.expvar_port", DefaultProcessExpVarPort)
procBindEnvAndSetDefault(config, "process_config.log_file", DefaultProcessAgentLogFile)
procBindEnvAndSetDefault(config, "process_config.internal_profiling.enabled", false)
Expand Down
20 changes: 15 additions & 5 deletions pkg/process/checks/interval.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ const (
//nolint:revive // TODO(PROC) Fix revive linter
RTContainerCheckDefaultInterval = 2 * time.Second
//nolint:revive // TODO(PROC) Fix revive linter
ConnectionsCheckDefaultInterval = 30 * time.Second
//nolint:revive // TODO(PROC) Fix revive linter
ProcessDiscoveryCheckDefaultInterval = 4 * time.Hour

discoveryMinInterval = 10 * time.Minute
Expand All @@ -38,7 +36,6 @@ const (
configRTProcessInterval = configIntervals + "process_realtime"
configContainerInterval = configIntervals + "container"
configRTContainerInterval = configIntervals + "container_realtime"
configConnectionsInterval = configIntervals + "connections"
)

var (
Expand All @@ -47,7 +44,6 @@ var (
RTProcessCheckName: RTProcessCheckDefaultInterval,
ContainerCheckName: ContainerCheckDefaultInterval,
RTContainerCheckName: RTContainerCheckDefaultInterval,
ConnectionsCheckName: ConnectionsCheckDefaultInterval,
DiscoveryCheckName: ProcessDiscoveryCheckDefaultInterval,
ProcessEventsCheckName: pkgconfigsetup.DefaultProcessEventsCheckInterval,
}
Expand All @@ -57,7 +53,6 @@ var (
RTProcessCheckName: configRTProcessInterval,
ContainerCheckName: configContainerInterval,
RTContainerCheckName: configRTContainerInterval,
ConnectionsCheckName: configConnectionsInterval,
}
)

Expand Down Expand Up @@ -87,6 +82,21 @@ func GetInterval(cfg pkgconfigmodel.Reader, checkName string) time.Duration {
pkgconfigsetup.DefaultProcessEventsMinCheckInterval.String(), pkgconfigsetup.DefaultProcessEventsCheckInterval.String())
}
return eventsInterval
case ConnectionsCheckName:
connectionsInterval := cfg.GetDuration("process_config.intervals.connections")
minInterval := pkgconfigsetup.DefaultConnectionsMinCheckInterval
if connectionsInterval < minInterval {
connectionsInterval = minInterval
_ = log.Warnf("Invalid interval for connections check (< %s) using default value of %s",
minInterval.String(), minInterval.String())
}
maxInterval := pkgconfigsetup.DefaultConnectionsMaxCheckInterval
if connectionsInterval > maxInterval {
connectionsInterval = maxInterval
_ = log.Warnf("Invalid interval for connections check (> %s) using default value of %s",
maxInterval.String(), maxInterval.String())
}
return connectionsInterval

default:
defaultInterval := defaultIntervals[checkName]
Expand Down
45 changes: 35 additions & 10 deletions pkg/process/checks/interval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ func TestLegacyIntervalDefault(t *testing.T) {
checkName: RTProcessCheckName,
expectedInterval: RTProcessCheckDefaultInterval,
},
{
name: "connections default",
checkName: ConnectionsCheckName,
expectedInterval: ConnectionsCheckDefaultInterval,
},
} {
t.Run(tc.name, func(t *testing.T) {
cfg := configmock.New(t)
Expand Down Expand Up @@ -82,11 +77,6 @@ func TestLegacyIntervalOverride(t *testing.T) {
setting: "process_config.intervals.process_realtime",
checkName: RTProcessCheckName,
},
{
name: "connections default",
setting: "process_config.intervals.connections",
checkName: ConnectionsCheckName,
},
} {
t.Run(tc.name, func(t *testing.T) {
cfg := configmock.New(t)
Expand Down Expand Up @@ -148,3 +138,38 @@ func TestProcessEventsInterval(t *testing.T) {
})
}
}

func TestConnectionsInterval(t *testing.T) {
for _, tc := range []struct {
name string
interval time.Duration
expectedInterval time.Duration
}{
{
name: "allowed interval",
interval: 2 * time.Minute,
expectedInterval: 2 * time.Minute,
},
{
name: "below minimum",
interval: 0,
expectedInterval: pkgconfigsetup.DefaultConnectionsMinCheckInterval,
},
{
name: "above maximum",
interval: 2 * time.Hour,
expectedInterval: pkgconfigsetup.DefaultConnectionsMaxCheckInterval,
},
} {
t.Run(tc.name, func(t *testing.T) {
cfg := configmock.New(t)
cfg.SetWithoutSource("process_config.intervals.connections", tc.interval)

assert.Equal(t, tc.expectedInterval, GetInterval(cfg, ConnectionsCheckName))
})
}
t.Run("default", func(t *testing.T) {
cfg := configmock.New(t)
assert.Equal(t, 30*time.Second, GetInterval(cfg, ConnectionsCheckName))
})
}

0 comments on commit 57d7604

Please sign in to comment.