diff --git a/cmd/system-probe/modules/traceroute.go b/cmd/system-probe/modules/traceroute.go index c860b2c068a30..71a2fb3701caa 100644 --- a/cmd/system-probe/modules/traceroute.go +++ b/cmd/system-probe/modules/traceroute.go @@ -22,12 +22,13 @@ import ( "github.com/DataDog/datadog-agent/cmd/system-probe/api/module" sysconfigtypes "github.com/DataDog/datadog-agent/cmd/system-probe/config/types" "github.com/DataDog/datadog-agent/pkg/networkpath/payload" - tracerouteutil "github.com/DataDog/datadog-agent/pkg/networkpath/traceroute" + tracerouteutil "github.com/DataDog/datadog-agent/pkg/networkpath/traceroute/config" + "github.com/DataDog/datadog-agent/pkg/networkpath/traceroute/runner" "github.com/DataDog/datadog-agent/pkg/util/log" ) type traceroute struct { - runner *tracerouteutil.Runner + runner *runner.Runner } var ( @@ -37,7 +38,7 @@ var ( ) func createTracerouteModule(_ *sysconfigtypes.Config, deps module.FactoryDependencies) (module.Module, error) { - runner, err := tracerouteutil.NewRunner(deps.Telemetry) + runner, err := runner.New(deps.Telemetry) if err != nil { return &traceroute{}, err } diff --git a/cmd/system-probe/modules/traceroute_test.go b/cmd/system-probe/modules/traceroute_test.go index 31451a241c70b..69045269f9a0f 100644 --- a/cmd/system-probe/modules/traceroute_test.go +++ b/cmd/system-probe/modules/traceroute_test.go @@ -12,7 +12,8 @@ import ( "net/http" "testing" - tracerouteutil "github.com/DataDog/datadog-agent/pkg/networkpath/traceroute" + tracerouteutil "github.com/DataDog/datadog-agent/pkg/networkpath/traceroute/config" + "github.com/gorilla/mux" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" diff --git a/comp/networkpath/npcollector/npcollectorimpl/npcollector.go b/comp/networkpath/npcollector/npcollectorimpl/npcollector.go index 4e0382ca99b3f..4ee787e567320 100644 --- a/comp/networkpath/npcollector/npcollectorimpl/npcollector.go +++ b/comp/networkpath/npcollector/npcollectorimpl/npcollector.go @@ -28,6 +28,7 @@ import ( "github.com/DataDog/datadog-agent/pkg/networkpath/payload" "github.com/DataDog/datadog-agent/pkg/networkpath/telemetry" "github.com/DataDog/datadog-agent/pkg/networkpath/traceroute" + "github.com/DataDog/datadog-agent/pkg/networkpath/traceroute/config" "github.com/DataDog/datadog-agent/pkg/process/statsd" ) @@ -72,7 +73,7 @@ type npCollectorImpl struct { TimeNowFn func() time.Time // TODO: instead of mocking traceroute via function replacement like this // we should ideally create a fake/mock traceroute instance that can be passed/injected in NpCollector - runTraceroute func(cfg traceroute.Config, telemetrycomp telemetryComp.Component) (payload.NetworkPath, error) + runTraceroute func(cfg config.Config, telemetrycomp telemetryComp.Component) (payload.NetworkPath, error) networkDevicesNamespace string } @@ -227,7 +228,7 @@ func (s *npCollectorImpl) runTracerouteForPath(ptest *pathteststore.PathtestCont s.logger.Debugf("Run Traceroute for ptest: %+v", ptest) startTime := s.TimeNowFn() - cfg := traceroute.Config{ + cfg := config.Config{ DestHostname: ptest.Pathtest.Hostname, DestPort: ptest.Pathtest.Port, MaxTTL: uint8(s.collectorConfigs.maxTTL), @@ -262,7 +263,7 @@ func (s *npCollectorImpl) runTracerouteForPath(ptest *pathteststore.PathtestCont } } -func runTraceroute(cfg traceroute.Config, telemetry telemetryComp.Component) (payload.NetworkPath, error) { +func runTraceroute(cfg config.Config, telemetry telemetryComp.Component) (payload.NetworkPath, error) { tr, err := traceroute.New(cfg, telemetry) if err != nil { return payload.NetworkPath{}, fmt.Errorf("new traceroute error: %s", err) diff --git a/comp/networkpath/npcollector/npcollectorimpl/npcollector_test.go b/comp/networkpath/npcollector/npcollectorimpl/npcollector_test.go index c44b54e1b1db9..390ce1c8684f4 100644 --- a/comp/networkpath/npcollector/npcollectorimpl/npcollector_test.go +++ b/comp/networkpath/npcollector/npcollectorimpl/npcollector_test.go @@ -32,7 +32,7 @@ import ( "github.com/DataDog/datadog-agent/pkg/logs/message" "github.com/DataDog/datadog-agent/pkg/networkpath/metricsender" "github.com/DataDog/datadog-agent/pkg/networkpath/payload" - "github.com/DataDog/datadog-agent/pkg/networkpath/traceroute" + "github.com/DataDog/datadog-agent/pkg/networkpath/traceroute/config" "github.com/DataDog/datadog-agent/pkg/trace/teststatsd" utillog "github.com/DataDog/datadog-agent/pkg/util/log" ) @@ -101,7 +101,7 @@ func Test_NpCollector_runningAndProcessing(t *testing.T) { assert.True(t, npCollector.running) - npCollector.runTraceroute = func(cfg traceroute.Config, _ telemetry.Component) (payload.NetworkPath, error) { + npCollector.runTraceroute = func(cfg config.Config, _ telemetry.Component) (payload.NetworkPath, error) { var p payload.NetworkPath if cfg.DestHostname == "10.0.0.2" { p = payload.NetworkPath{ diff --git a/pkg/collector/corechecks/networkpath/networkpath.go b/pkg/collector/corechecks/networkpath/networkpath.go index 31040c24b68b2..28bef69f2bbca 100644 --- a/pkg/collector/corechecks/networkpath/networkpath.go +++ b/pkg/collector/corechecks/networkpath/networkpath.go @@ -23,6 +23,7 @@ import ( "github.com/DataDog/datadog-agent/pkg/networkpath/metricsender" "github.com/DataDog/datadog-agent/pkg/networkpath/payload" "github.com/DataDog/datadog-agent/pkg/networkpath/telemetry" + "github.com/DataDog/datadog-agent/pkg/networkpath/traceroute/config" "github.com/DataDog/datadog-agent/pkg/util/log" "github.com/DataDog/datadog-agent/pkg/util/optional" @@ -50,7 +51,7 @@ func (c *Check) Run() error { } metricSender := metricsender.NewMetricSenderAgent(senderInstance) - cfg := traceroute.Config{ + cfg := config.Config{ DestHostname: c.config.DestHostname, DestPort: c.config.DestPort, MaxTTL: c.config.MaxTTL, diff --git a/pkg/networkpath/traceroute/config/config.go b/pkg/networkpath/traceroute/config/config.go new file mode 100644 index 0000000000000..f773c2516e6e4 --- /dev/null +++ b/pkg/networkpath/traceroute/config/config.go @@ -0,0 +1,36 @@ +// Unless explicitly stated otherwise all files in this repository are licensed +// under the Apache License Version 2.0. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright 2024-present Datadog, Inc. + +// Package config is the configuration for the traceroute functionality +package config + +import ( + "time" + + "github.com/DataDog/datadog-agent/pkg/networkpath/payload" +) + +// Config specifies the configuration of an instance +// of Traceroute +type Config struct { + // TODO: add common configuration + // Destination Hostname + DestHostname string + // Destination Port number + DestPort uint16 + // Destination service name + DestinationService string + // Source service name + SourceService string + // Source container ID + SourceContainerID string + // Max number of hops to try + MaxTTL uint8 + // TODO: do we want to expose this? + Timeout time.Duration + // Protocol is the protocol to use + // for traceroute, default is UDP + Protocol payload.Protocol +} diff --git a/pkg/networkpath/traceroute/runner.go b/pkg/networkpath/traceroute/runner/runner.go similarity index 92% rename from pkg/networkpath/traceroute/runner.go rename to pkg/networkpath/traceroute/runner/runner.go index ef211ef98610c..9087b66b4eb8a 100644 --- a/pkg/networkpath/traceroute/runner.go +++ b/pkg/networkpath/traceroute/runner/runner.go @@ -1,9 +1,10 @@ // Unless explicitly stated otherwise all files in this repository are licensed // under the Apache License Version 2.0. // This product includes software developed at Datadog (https://www.datadoghq.com/). -// Copyright 2016-present Datadog, Inc. +// Copyright 2024-present Datadog, Inc. -package traceroute +// Package runner is the functionality for actually performing traceroutes +package runner import ( "context" @@ -15,7 +16,6 @@ import ( "sort" "time" - "github.com/DataDog/datadog-agent/pkg/version" "github.com/Datadog/dublin-traceroute/go/dublintraceroute/probes/probev4" "github.com/Datadog/dublin-traceroute/go/dublintraceroute/results" "github.com/vishvananda/netns" @@ -24,6 +24,7 @@ import ( "github.com/DataDog/datadog-agent/pkg/config/setup" "github.com/DataDog/datadog-agent/pkg/network" "github.com/DataDog/datadog-agent/pkg/networkpath/payload" + "github.com/DataDog/datadog-agent/pkg/networkpath/traceroute/config" "github.com/DataDog/datadog-agent/pkg/networkpath/traceroute/tcp" "github.com/DataDog/datadog-agent/pkg/process/util" "github.com/DataDog/datadog-agent/pkg/telemetry" @@ -32,6 +33,7 @@ import ( "github.com/DataDog/datadog-agent/pkg/util/hostname" "github.com/DataDog/datadog-agent/pkg/util/kernel" "github.com/DataDog/datadog-agent/pkg/util/log" + "github.com/DataDog/datadog-agent/pkg/version" ) const ( @@ -45,21 +47,17 @@ const ( DefaultMinTTL = 1 // DefaultDelay defines the default delay DefaultDelay = 50 //msec - // DefaultOutputFormat defines the default output format - DefaultOutputFormat = "json" tracerouteRunnerModuleName = "traceroute_runner__" ) // Telemetry var tracerouteRunnerTelemetry = struct { - runs *telemetry.StatCounterWrapper - failedRuns *telemetry.StatCounterWrapper - reverseDNSTimetouts *telemetry.StatCounterWrapper + runs *telemetry.StatCounterWrapper + failedRuns *telemetry.StatCounterWrapper }{ telemetry.NewStatCounterWrapper(tracerouteRunnerModuleName, "runs", []string{}, "Counter measuring the number of traceroutes run"), telemetry.NewStatCounterWrapper(tracerouteRunnerModuleName, "failed_runs", []string{}, "Counter measuring the number of traceroute run failures"), - telemetry.NewStatCounterWrapper(tracerouteRunnerModuleName, "reverse_dns_timeouts", []string{}, "Counter measuring the number of traceroute reverse DNS timeouts"), } // Runner executes traceroutes @@ -69,8 +67,8 @@ type Runner struct { networkID string } -// NewRunner initializes a new traceroute runner -func NewRunner(telemetryComp telemetryComponent.Component) (*Runner, error) { +// New initializes a new traceroute runner +func New(telemetryComp telemetryComponent.Component) (*Runner, error) { var err error var networkID string if ec2.IsRunningOn(context.TODO()) { @@ -100,7 +98,7 @@ func NewRunner(telemetryComp telemetryComponent.Component) (*Runner, error) { // // This code is experimental and will be replaced with a more // complete implementation. -func (r *Runner) RunTraceroute(ctx context.Context, cfg Config) (payload.NetworkPath, error) { +func (r *Runner) RunTraceroute(ctx context.Context, cfg config.Config) (payload.NetworkPath, error) { defer tracerouteRunnerTelemetry.runs.Inc() dests, err := net.DefaultResolver.LookupIP(ctx, "ip4", cfg.DestHostname) if err != nil || len(dests) == 0 { @@ -164,7 +162,7 @@ func (r *Runner) RunTraceroute(ctx context.Context, cfg Config) (payload.Network return pathResult, nil } -func (r *Runner) runUDP(cfg Config, hname string, dest net.IP, maxTTL uint8, timeout time.Duration) (payload.NetworkPath, error) { +func (r *Runner) runUDP(cfg config.Config, hname string, dest net.IP, maxTTL uint8, timeout time.Duration) (payload.NetworkPath, error) { destPort, srcPort, useSourcePort := getPorts(cfg.DestPort) dt := &probev4.UDPv4{ @@ -194,7 +192,7 @@ func (r *Runner) runUDP(cfg Config, hname string, dest net.IP, maxTTL uint8, tim return pathResult, nil } -func (r *Runner) runTCP(cfg Config, hname string, target net.IP, maxTTL uint8, timeout time.Duration) (payload.NetworkPath, error) { +func (r *Runner) runTCP(cfg config.Config, hname string, target net.IP, maxTTL uint8, timeout time.Duration) (payload.NetworkPath, error) { destPort := cfg.DestPort if destPort == 0 { destPort = 80 // TODO: is this the default we want? diff --git a/pkg/networkpath/traceroute/runner_test.go b/pkg/networkpath/traceroute/runner/runner_test.go similarity index 92% rename from pkg/networkpath/traceroute/runner_test.go rename to pkg/networkpath/traceroute/runner/runner_test.go index 86f7b99f9c68a..4b0a512e7a969 100644 --- a/pkg/networkpath/traceroute/runner_test.go +++ b/pkg/networkpath/traceroute/runner/runner_test.go @@ -1,9 +1,9 @@ // Unless explicitly stated otherwise all files in this repository are licensed // under the Apache License Version 2.0. // This product includes software developed at Datadog (https://www.datadoghq.com/). -// Copyright 2023-present Datadog, Inc. +// Copyright 2024-present Datadog, Inc. -package traceroute +package runner import ( "testing" diff --git a/pkg/networkpath/traceroute/traceroute.go b/pkg/networkpath/traceroute/traceroute.go index 5fb757fc88f71..d94ddc6d55152 100644 --- a/pkg/networkpath/traceroute/traceroute.go +++ b/pkg/networkpath/traceroute/traceroute.go @@ -1,45 +1,7 @@ // Unless explicitly stated otherwise all files in this repository are licensed // under the Apache License Version 2.0. // This product includes software developed at Datadog (https://www.datadoghq.com/). -// Copyright 2016-present Datadog, Inc. +// Copyright 2024-present Datadog, Inc. // Package traceroute adds traceroute functionality to the agent package traceroute - -import ( - "context" - "time" - - "github.com/DataDog/datadog-agent/pkg/networkpath/payload" -) - -type ( - // Config specifies the configuration of an instance - // of Traceroute - Config struct { - // TODO: add common configuration - // Destination Hostname - DestHostname string - // Destination Port number - DestPort uint16 - // Destination service name - DestinationService string - // Source service name - SourceService string - // Source container ID - SourceContainerID string - // Max number of hops to try - MaxTTL uint8 - // TODO: do we want to expose this? - Timeout time.Duration - // Protocol is the protocol to use - // for traceroute, default is UDP - Protocol payload.Protocol - } - - // Traceroute defines an interface for running - // traceroutes for the Network Path integration - Traceroute interface { - Run(context.Context) (payload.NetworkPath, error) - } -) diff --git a/pkg/networkpath/traceroute/traceroute_darwin.go b/pkg/networkpath/traceroute/traceroute_darwin.go index cf0ef5d3e325e..38be9352f1715 100644 --- a/pkg/networkpath/traceroute/traceroute_darwin.go +++ b/pkg/networkpath/traceroute/traceroute_darwin.go @@ -13,6 +13,8 @@ import ( "github.com/DataDog/datadog-agent/comp/core/telemetry" "github.com/DataDog/datadog-agent/pkg/networkpath/payload" + "github.com/DataDog/datadog-agent/pkg/networkpath/traceroute/config" + "github.com/DataDog/datadog-agent/pkg/networkpath/traceroute/runner" "github.com/DataDog/datadog-agent/pkg/util/log" ) @@ -24,21 +26,21 @@ const ( // running traceroute from an agent running // on macOS type MacTraceroute struct { - cfg Config - runner *Runner + cfg config.Config + runner *runner.Runner } // New creates a new instance of MacTraceroute // based on an input configuration -func New(cfg Config, telemetry telemetry.Component) (*MacTraceroute, error) { +func New(cfg config.Config, telemetry telemetry.Component) (*MacTraceroute, error) { log.Debugf("Creating new traceroute with config: %+v", cfg) - runner, err := NewRunner(telemetry) + runner, err := runner.New(telemetry) if err != nil { return nil, err } // TCP is not supported at the moment due to the - // way go listensn for TCP in our implementation on BSD systems + // way go listens for TCP in our implementation on BSD systems if cfg.Protocol == payload.ProtocolTCP { return nil, fmt.Errorf(tcpNotSupportedMsg) } diff --git a/pkg/networkpath/traceroute/traceroute_linux.go b/pkg/networkpath/traceroute/traceroute_linux.go index 547f0fa2ff501..536d360f4f6c3 100644 --- a/pkg/networkpath/traceroute/traceroute_linux.go +++ b/pkg/networkpath/traceroute/traceroute_linux.go @@ -14,6 +14,7 @@ import ( "github.com/DataDog/datadog-agent/comp/core/telemetry" pkgconfigsetup "github.com/DataDog/datadog-agent/pkg/config/setup" "github.com/DataDog/datadog-agent/pkg/networkpath/payload" + "github.com/DataDog/datadog-agent/pkg/networkpath/traceroute/config" "github.com/DataDog/datadog-agent/pkg/process/net" "github.com/DataDog/datadog-agent/pkg/util/log" ) @@ -26,12 +27,12 @@ const ( // running traceroute from an agent running // on Linux type LinuxTraceroute struct { - cfg Config + cfg config.Config } // New creates a new instance of LinuxTraceroute // based on an input configuration -func New(cfg Config, _ telemetry.Component) (*LinuxTraceroute, error) { +func New(cfg config.Config, _ telemetry.Component) (*LinuxTraceroute, error) { log.Debugf("Creating new traceroute with config: %+v", cfg) return &LinuxTraceroute{ cfg: cfg, diff --git a/pkg/networkpath/traceroute/traceroute_windows.go b/pkg/networkpath/traceroute/traceroute_windows.go index 089f46d216766..8820ea1de8549 100644 --- a/pkg/networkpath/traceroute/traceroute_windows.go +++ b/pkg/networkpath/traceroute/traceroute_windows.go @@ -14,6 +14,7 @@ import ( "github.com/DataDog/datadog-agent/comp/core/telemetry" pkgconfigsetup "github.com/DataDog/datadog-agent/pkg/config/setup" "github.com/DataDog/datadog-agent/pkg/networkpath/payload" + "github.com/DataDog/datadog-agent/pkg/networkpath/traceroute/config" "github.com/DataDog/datadog-agent/pkg/process/net" "github.com/DataDog/datadog-agent/pkg/util/log" ) @@ -26,12 +27,12 @@ const ( // running traceroute from an agent running // on Windows type WindowsTraceroute struct { - cfg Config + cfg config.Config } // New creates a new instance of WindowsTraceroute // based on an input configuration -func New(cfg Config, _ telemetry.Component) (*WindowsTraceroute, error) { +func New(cfg config.Config, _ telemetry.Component) (*WindowsTraceroute, error) { log.Debugf("Creating new traceroute with config: %+v", cfg) return &WindowsTraceroute{ cfg: cfg, diff --git a/pkg/networkpath/traceroute/utils.go b/pkg/networkpath/traceroute/utils.go index 760c3a03acc38..764a4b48f31a3 100644 --- a/pkg/networkpath/traceroute/utils.go +++ b/pkg/networkpath/traceroute/utils.go @@ -11,6 +11,12 @@ import ( "net" "strings" "time" + + "github.com/DataDog/datadog-agent/pkg/telemetry" +) + +var ( + reverseDNSTimeouts = telemetry.NewStatCounterWrapper("traceroute", "reverse_dns_timeouts", []string{}, "Counter measuring the number of traceroute reverse DNS timeouts") ) var lookupAddrFn = net.DefaultResolver.LookupAddr @@ -30,7 +36,7 @@ func GetHostname(ipAddr string) string { defer cancel() currHostList, err := lookupAddrFn(ctx, ipAddr) if errors.Is(err, context.Canceled) { - tracerouteRunnerTelemetry.reverseDNSTimetouts.Inc() + reverseDNSTimeouts.Inc() } if len(currHostList) > 0 {