Skip to content

Commit 136f119

Browse files
committed
Simplify CNM baseline scheduling setup
1 parent e9898c4 commit 136f119

3 files changed

Lines changed: 46 additions & 22 deletions

File tree

comp/networkpath/npcollector/impl/baseline.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package npcollectorimpl
77

88
import (
99
"iter"
10+
"net/netip"
1011
"sort"
1112

1213
"github.com/DataDog/datadog-agent/comp/networkpath/npcollector/impl/common"
@@ -60,17 +61,15 @@ func addBaselineCandidate(selected []baselineCandidate, candidate baselineCandid
6061
return selected
6162
}
6263

63-
func (s *npCollectorImpl) scheduleBaselineNetworkPathTests(conns iter.Seq[npmodel.NetworkPathConnection]) {
64-
vpcSubnets, err := s.getVPCSubnets()
65-
if err != nil {
66-
s.logger.Errorf("Failed to get VPC subnets to skip: %s", err)
67-
return
68-
}
69-
64+
func (s *npCollectorImpl) scheduleBaselineNetworkPathTests(conns iter.Seq[npmodel.NetworkPathConnection], vpcSubnets []netip.Prefix) {
65+
startTime := s.TimeNowFn()
66+
connCount := 0
7067
selected := make([]baselineCandidate, 0, baselineSelectionsPerSnapshot)
7168
for conn := range conns {
69+
connCount++
7270
evaluation := s.evaluateNetworkPathForConn(conn, payload.PathOriginNetworkTraffic, vpcSubnets)
7371
if !evaluation.shouldSchedule {
72+
s.logger.Tracef("Skipped connection: addr=%s, protocol=%s", conn.Dest, conn.Type)
7473
continue
7574
}
7675
path := s.makePathtest(conn, payload.PathOriginNetworkTraffic)
@@ -88,4 +87,6 @@ func (s *npCollectorImpl) scheduleBaselineNetworkPathTests(conns iter.Seq[npmode
8887
s.logger.Errorf("Error scheduling baseline pathtest: %s", err)
8988
}
9089
}
90+
_ = s.statsdClient.Count(common.NetworkPathCollectorMetricPrefix+"schedule.conns_received", int64(connCount), []string{}, 1)
91+
_ = s.statsdClient.Gauge(common.NetworkPathCollectorMetricPrefix+"schedule.duration", s.TimeNowFn().Sub(startTime).Seconds(), nil, 1)
9192
}

comp/networkpath/npcollector/impl/baseline_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"net/netip"
1212
"slices"
1313
"testing"
14+
"time"
1415

1516
model "github.com/DataDog/agent-payload/v5/process"
1617
"github.com/DataDog/datadog-agent/pkg/networkpath/payload"
@@ -62,6 +63,28 @@ func TestBaselineSelectsCandidatesFromEverySnapshot(t *testing.T) {
6263
assert.Equal(t, "10.0.1.1", (<-collector.pathtestInputChan).Hostname)
6364
}
6465

66+
func TestBaselineReportsSnapshotTelemetry(t *testing.T) {
67+
stats := &teststatsd.Client{}
68+
_, collector := newTestNpCollector(t, map[string]any{
69+
"network_path.connections_monitoring.baseline_tests_enabled": true,
70+
"network_path.collector.monitor_ip_without_domain": true,
71+
}, stats, nil)
72+
timeNowCounter := 0
73+
collector.TimeNowFn = func() time.Time {
74+
now := MockTimeNow().Add(time.Duration(timeNowCounter) * time.Minute)
75+
timeNowCounter++
76+
return now
77+
}
78+
79+
collector.ScheduleNetworkPathTests(slices.Values([]npmodel.NetworkPathConnection{
80+
baselineConn("10.0.0.1", 2),
81+
baselineConn("10.0.0.2", 1),
82+
}))
83+
84+
assert.Equal(t, int64(2), stats.GetCountSummaries()["datadog.network_path.collector.schedule.conns_received"].Sum)
85+
assert.Equal(t, 60.0, stats.GetGaugeSummaries()["datadog.network_path.collector.schedule.duration"].Last)
86+
}
87+
6588
func TestBaselinePrioritizesDiagnosticConnections(t *testing.T) {
6689
_, collector := newTestNpCollector(t, map[string]any{
6790
"network_path.connections_monitoring.baseline_tests_enabled": true,

comp/networkpath/npcollector/impl/npcollector.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -286,31 +286,31 @@ func (s *npCollectorImpl) getVPCSubnets() ([]netip.Prefix, error) {
286286
}
287287

288288
func (s *npCollectorImpl) ScheduleNetworkPathTests(conns iter.Seq[npmodel.NetworkPathConnection]) {
289+
if !s.collectorConfigs.connectionsMonitoringEnabled && !s.collectorConfigs.baselineTestsEnabled {
290+
return
291+
}
292+
293+
vpcSubnets, err := s.getVPCSubnets()
294+
if err != nil {
295+
s.logger.Errorf("Failed to get VPC subnets to skip: %s", err)
296+
return
297+
}
298+
289299
if s.collectorConfigs.connectionsMonitoringEnabled {
290-
s.scheduleNetworkPathTests(payload.PathOriginNetworkTraffic, conns)
291-
} else if s.collectorConfigs.baselineTestsEnabled {
292-
s.scheduleBaselineNetworkPathTests(conns)
300+
s.scheduleNetworkPathTests(payload.PathOriginNetworkTraffic, conns, vpcSubnets)
301+
} else {
302+
s.scheduleBaselineNetworkPathTests(conns, vpcSubnets)
293303
}
294304
}
295305

296306
func (s *npCollectorImpl) ScheduleNetflowPathTests(conns iter.Seq[npmodel.NetworkPathConnection]) {
297307
if !s.collectorConfigs.netflowMonitoringEnabled {
298308
return
299309
}
300-
s.scheduleNetworkPathTests(payload.PathOriginNetflow, conns)
310+
s.scheduleNetworkPathTests(payload.PathOriginNetflow, conns, nil)
301311
}
302312

303-
func (s *npCollectorImpl) scheduleNetworkPathTests(origin payload.PathOrigin, conns iter.Seq[npmodel.NetworkPathConnection]) {
304-
var vpcSubnets []netip.Prefix
305-
if origin == payload.PathOriginNetworkTraffic {
306-
var err error
307-
vpcSubnets, err = s.getVPCSubnets()
308-
if err != nil {
309-
s.logger.Errorf("Failed to get VPC subnets to skip: %s", err)
310-
return
311-
}
312-
}
313-
313+
func (s *npCollectorImpl) scheduleNetworkPathTests(origin payload.PathOrigin, conns iter.Seq[npmodel.NetworkPathConnection], vpcSubnets []netip.Prefix) {
314314
startTime := s.TimeNowFn()
315315
connCount := 0
316316
for conn := range conns {

0 commit comments

Comments
 (0)