Skip to content

Commit

Permalink
remove gopacket import in agent/process-agent (#31105)
Browse files Browse the repository at this point in the history
  • Loading branch information
brycekahle authored Nov 15, 2024
1 parent 10b3b64 commit 718aea5
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 75 deletions.
7 changes: 4 additions & 3 deletions cmd/system-probe/modules/traceroute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/system-probe/modules/traceroute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 4 additions & 3 deletions comp/networkpath/npcollector/npcollectorimpl/npcollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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{
Expand Down
3 changes: 2 additions & 1 deletion pkg/collector/corechecks/networkpath/networkpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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,
Expand Down
36 changes: 36 additions & 0 deletions pkg/networkpath/traceroute/config/config.go
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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 (
Expand All @@ -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
Expand All @@ -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()) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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?
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
40 changes: 1 addition & 39 deletions pkg/networkpath/traceroute/traceroute.go
Original file line number Diff line number Diff line change
@@ -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)
}
)
12 changes: 7 additions & 5 deletions pkg/networkpath/traceroute/traceroute_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/networkpath/traceroute/traceroute_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions pkg/networkpath/traceroute/traceroute_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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,
Expand Down
Loading

0 comments on commit 718aea5

Please sign in to comment.