Skip to content

Commit

Permalink
extract system-probe module fx dependencies into a struct (#29012)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcacheux authored Sep 4, 2024
1 parent a09e2a1 commit 2172bc9
Show file tree
Hide file tree
Showing 18 changed files with 44 additions and 51 deletions.
12 changes: 12 additions & 0 deletions cmd/system-probe/api/module/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ package module

import (
"errors"

"github.com/DataDog/datadog-agent/comp/core/telemetry"
workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
"go.uber.org/fx"
)

// ErrNotEnabled is a special error type that should be returned by a Factory
Expand All @@ -20,3 +24,11 @@ type Module interface {
Register(*Router) error
Close()
}

// FactoryDependencies defines the fx dependencies for a module factory
type FactoryDependencies struct {
fx.In

WMeta workloadmeta.Component
Telemetry telemetry.Component
}
4 changes: 1 addition & 3 deletions cmd/system-probe/api/module/factory_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ package module

import (
sysconfigtypes "github.com/DataDog/datadog-agent/cmd/system-probe/config/types"
"github.com/DataDog/datadog-agent/comp/core/telemetry"
workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
)

// Factory encapsulates the initialization of a Module
type Factory struct {
Name sysconfigtypes.ModuleName
ConfigNamespaces []string
Fn func(cfg *sysconfigtypes.Config, wmeta workloadmeta.Component, telemetry telemetry.Component) (Module, error)
Fn func(cfg *sysconfigtypes.Config, deps FactoryDependencies) (Module, error)
NeedsEBPF func() bool
}
4 changes: 1 addition & 3 deletions cmd/system-probe/api/module/factory_others.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ package module

import (
sysconfigtypes "github.com/DataDog/datadog-agent/cmd/system-probe/config/types"
"github.com/DataDog/datadog-agent/comp/core/telemetry"
workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
)

// Factory encapsulates the initialization of a Module
type Factory struct {
Name sysconfigtypes.ModuleName
ConfigNamespaces []string
Fn func(cfg *sysconfigtypes.Config, wmeta workloadmeta.Component, telemetry telemetry.Component) (Module, error)
Fn func(cfg *sysconfigtypes.Config, deps FactoryDependencies) (Module, error)
}
12 changes: 10 additions & 2 deletions cmd/system-probe/api/module/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ func Register(cfg *sysconfigtypes.Config, httpMux *mux.Router, factories []Facto
var err error
var module Module
withModule(factory.Name, func() {
module, err = factory.Fn(cfg, wmeta, telemetry)
deps := FactoryDependencies{
WMeta: wmeta,
Telemetry: telemetry,
}
module, err = factory.Fn(cfg, deps)
})

// In case a module failed to be started, do not make the whole `system-probe` abort.
Expand Down Expand Up @@ -156,7 +160,11 @@ func RestartModule(factory Factory, wmeta workloadmeta.Component, telemetry tele
var err error
withModule(factory.Name, func() {
currentModule.Close()
newModule, err = factory.Fn(l.cfg, wmeta, telemetry)
deps := FactoryDependencies{
WMeta: wmeta,
Telemetry: telemetry,
}
newModule, err = factory.Fn(l.cfg, deps)
})
if err != nil {
l.errors[factory.Name] = err
Expand Down
4 changes: 1 addition & 3 deletions cmd/system-probe/modules/compliance.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (
"github.com/DataDog/datadog-agent/cmd/system-probe/config"
sysconfigtypes "github.com/DataDog/datadog-agent/cmd/system-probe/config/types"
"github.com/DataDog/datadog-agent/cmd/system-probe/utils"
"github.com/DataDog/datadog-agent/comp/core/telemetry"
workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
"github.com/DataDog/datadog-agent/pkg/compliance/dbconfig"
"github.com/DataDog/datadog-agent/pkg/util/log"

Expand All @@ -36,7 +34,7 @@ import (
var ComplianceModule = module.Factory{
Name: config.ComplianceModule,
ConfigNamespaces: []string{},
Fn: func(_ *sysconfigtypes.Config, _ workloadmeta.Component, _ telemetry.Component) (module.Module, error) {
Fn: func(_ *sysconfigtypes.Config, _ module.FactoryDependencies) (module.Module, error) {
return &complianceModule{}, nil
},
NeedsEBPF: func() bool {
Expand Down
4 changes: 1 addition & 3 deletions cmd/system-probe/modules/crashdetect_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import (
"github.com/DataDog/datadog-agent/cmd/system-probe/config"
sysconfigtypes "github.com/DataDog/datadog-agent/cmd/system-probe/config/types"
"github.com/DataDog/datadog-agent/cmd/system-probe/utils"
"github.com/DataDog/datadog-agent/comp/core/telemetry"
workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
"github.com/DataDog/datadog-agent/pkg/collector/corechecks/system/wincrashdetect/probe"
"github.com/DataDog/datadog-agent/pkg/util/log"
)
Expand All @@ -25,7 +23,7 @@ import (
var WinCrashProbe = module.Factory{
Name: config.WindowsCrashDetectModule,
ConfigNamespaces: []string{"windows_crash_detection"},
Fn: func(cfg *sysconfigtypes.Config, _ workloadmeta.Component, _ telemetry.Component) (module.Module, error) {
Fn: func(cfg *sysconfigtypes.Config, _ module.FactoryDependencies) (module.Module, error) {
log.Infof("Starting the WinCrashProbe probe")
cp, err := probe.NewWinCrashProbe(cfg)
if err != nil {
Expand Down
4 changes: 1 addition & 3 deletions cmd/system-probe/modules/dynamic_instrumentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import (
"github.com/DataDog/datadog-agent/cmd/system-probe/api/module"
"github.com/DataDog/datadog-agent/cmd/system-probe/config"
sysconfigtypes "github.com/DataDog/datadog-agent/cmd/system-probe/config/types"
"github.com/DataDog/datadog-agent/comp/core/telemetry"
workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
"github.com/DataDog/datadog-agent/pkg/dynamicinstrumentation"
"github.com/DataDog/datadog-agent/pkg/ebpf"
)
Expand All @@ -24,7 +22,7 @@ import (
var DynamicInstrumentation = module.Factory{
Name: config.DynamicInstrumentationModule,
ConfigNamespaces: []string{},
Fn: func(agentConfiguration *sysconfigtypes.Config, _ workloadmeta.Component, _ telemetry.Component) (module.Module, error) {
Fn: func(agentConfiguration *sysconfigtypes.Config, _ module.FactoryDependencies) (module.Module, error) {
config, err := dynamicinstrumentation.NewConfig(agentConfiguration)
if err != nil {
return nil, fmt.Errorf("invalid dynamic instrumentation module configuration: %w", err)
Expand Down
4 changes: 1 addition & 3 deletions cmd/system-probe/modules/ebpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (
"github.com/DataDog/datadog-agent/cmd/system-probe/config"
sysconfigtypes "github.com/DataDog/datadog-agent/cmd/system-probe/config/types"
"github.com/DataDog/datadog-agent/cmd/system-probe/utils"
"github.com/DataDog/datadog-agent/comp/core/telemetry"
workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
"github.com/DataDog/datadog-agent/pkg/collector/corechecks/ebpf/probe/ebpfcheck"
"github.com/DataDog/datadog-agent/pkg/ebpf"
"github.com/DataDog/datadog-agent/pkg/util/log"
Expand All @@ -29,7 +27,7 @@ import (
var EBPFProbe = module.Factory{
Name: config.EBPFModule,
ConfigNamespaces: []string{},
Fn: func(_ *sysconfigtypes.Config, _ workloadmeta.Component, _ telemetry.Component) (module.Module, error) {
Fn: func(_ *sysconfigtypes.Config, _ module.FactoryDependencies) (module.Module, error) {
log.Infof("Starting the ebpf probe")
okp, err := ebpfcheck.NewProbe(ebpf.NewConfig())
if err != nil {
Expand Down
6 changes: 2 additions & 4 deletions cmd/system-probe/modules/eventmonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ package modules
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/comp/core/telemetry"
workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
"github.com/DataDog/datadog-agent/pkg/eventmonitor"
emconfig "github.com/DataDog/datadog-agent/pkg/eventmonitor/config"
netconfig "github.com/DataDog/datadog-agent/pkg/network/config"
Expand All @@ -24,7 +22,7 @@ import (

var eventMonitorModuleConfigNamespaces = []string{"event_monitoring_config", "runtime_security_config"}

func createEventMonitorModule(_ *sysconfigtypes.Config, wmeta workloadmeta.Component, telemetry telemetry.Component) (module.Module, error) {
func createEventMonitorModule(_ *sysconfigtypes.Config, deps module.FactoryDependencies) (module.Module, error) {
emconfig := emconfig.NewConfig()

secconfig, err := secconfig.NewConfig()
Expand All @@ -43,7 +41,7 @@ func createEventMonitorModule(_ *sysconfigtypes.Config, wmeta workloadmeta.Compo
secmodule.DisableRuntimeSecurity(secconfig)
}

evm, err := eventmonitor.NewEventMonitor(emconfig, secconfig, opts, wmeta, telemetry)
evm, err := eventmonitor.NewEventMonitor(emconfig, secconfig, opts, deps.WMeta, deps.Telemetry)
if err != nil {
log.Errorf("error initializing event monitoring module: %v", err)
return nil, module.ErrNotEnabled
Expand Down
4 changes: 1 addition & 3 deletions cmd/system-probe/modules/language_detection.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import (
"github.com/DataDog/datadog-agent/cmd/system-probe/api/module"
"github.com/DataDog/datadog-agent/cmd/system-probe/config"
sysconfigtypes "github.com/DataDog/datadog-agent/cmd/system-probe/config/types"
"github.com/DataDog/datadog-agent/comp/core/telemetry"
workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
"github.com/DataDog/datadog-agent/pkg/languagedetection/languagemodels"
"github.com/DataDog/datadog-agent/pkg/languagedetection/privileged"
languageDetectionProto "github.com/DataDog/datadog-agent/pkg/proto/pbgo/languagedetection"
Expand All @@ -29,7 +27,7 @@ import (
var LanguageDetectionModule = module.Factory{
Name: config.LanguageDetectionModule,
ConfigNamespaces: []string{"language_detection"},
Fn: func(_ *sysconfigtypes.Config, _ workloadmeta.Component, _ telemetry.Component) (module.Module, error) {
Fn: func(_ *sysconfigtypes.Config, _ module.FactoryDependencies) (module.Module, error) {
return &languageDetectionModule{
languageDetector: privileged.NewLanguageDetector(),
}, nil
Expand Down
6 changes: 2 additions & 4 deletions cmd/system-probe/modules/network_tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ 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/cmd/system-probe/utils"
telemetryComponent "github.com/DataDog/datadog-agent/comp/core/telemetry"
workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
coreconfig "github.com/DataDog/datadog-agent/pkg/config/setup"
"github.com/DataDog/datadog-agent/pkg/network"
networkconfig "github.com/DataDog/datadog-agent/pkg/network/config"
Expand All @@ -48,7 +46,7 @@ const inactivityRestartDuration = 20 * time.Minute

var networkTracerModuleConfigNamespaces = []string{"network_config", "service_monitoring_config"}

func createNetworkTracerModule(cfg *sysconfigtypes.Config, _ workloadmeta.Component, telemetryComponent telemetryComponent.Component) (module.Module, error) {
func createNetworkTracerModule(cfg *sysconfigtypes.Config, deps module.FactoryDependencies) (module.Module, error) {
ncfg := networkconfig.New()

// Checking whether the current OS + kernel version is supported by the tracer
Expand All @@ -63,7 +61,7 @@ func createNetworkTracerModule(cfg *sysconfigtypes.Config, _ workloadmeta.Compon
log.Info("enabling universal service monitoring (USM)")
}

t, err := tracer.NewTracer(ncfg, telemetryComponent)
t, err := tracer.NewTracer(ncfg, deps.Telemetry)

done := make(chan struct{})
if err == nil {
Expand Down
4 changes: 1 addition & 3 deletions cmd/system-probe/modules/oom_kill_probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (
"github.com/DataDog/datadog-agent/cmd/system-probe/config"
sysconfigtypes "github.com/DataDog/datadog-agent/cmd/system-probe/config/types"
"github.com/DataDog/datadog-agent/cmd/system-probe/utils"
"github.com/DataDog/datadog-agent/comp/core/telemetry"
workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
"github.com/DataDog/datadog-agent/pkg/collector/corechecks/ebpf/probe/oomkill"
"github.com/DataDog/datadog-agent/pkg/ebpf"
"github.com/DataDog/datadog-agent/pkg/util/log"
Expand All @@ -29,7 +27,7 @@ import (
var OOMKillProbe = module.Factory{
Name: config.OOMKillProbeModule,
ConfigNamespaces: []string{},
Fn: func(_ *sysconfigtypes.Config, _ workloadmeta.Component, _ telemetry.Component) (module.Module, error) {
Fn: func(_ *sysconfigtypes.Config, _ module.FactoryDependencies) (module.Module, error) {
log.Infof("Starting the OOM Kill probe")
okp, err := oomkill.NewProbe(ebpf.NewConfig())
if err != nil {
Expand Down
4 changes: 1 addition & 3 deletions cmd/system-probe/modules/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (
"github.com/DataDog/datadog-agent/cmd/system-probe/api/module"
"github.com/DataDog/datadog-agent/cmd/system-probe/config"
sysconfigtypes "github.com/DataDog/datadog-agent/cmd/system-probe/config/types"
"github.com/DataDog/datadog-agent/comp/core/telemetry"
workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
pingcheck "github.com/DataDog/datadog-agent/pkg/networkdevice/pinger"
"github.com/DataDog/datadog-agent/pkg/util/log"
)
Expand All @@ -37,7 +35,7 @@ type pinger struct{}
var Pinger = module.Factory{
Name: config.PingModule,
ConfigNamespaces: []string{"ping"},
Fn: func(_ *sysconfigtypes.Config, _ workloadmeta.Component, _ telemetry.Component) (module.Module, error) {
Fn: func(_ *sysconfigtypes.Config, _ module.FactoryDependencies) (module.Module, error) {
return &pinger{}, nil
},
NeedsEBPF: func() bool {
Expand Down
4 changes: 1 addition & 3 deletions cmd/system-probe/modules/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
"github.com/DataDog/datadog-agent/cmd/system-probe/api/module"
"github.com/DataDog/datadog-agent/cmd/system-probe/config"
sysconfigtypes "github.com/DataDog/datadog-agent/cmd/system-probe/config/types"
"github.com/DataDog/datadog-agent/comp/core/telemetry"
workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"

//nolint:revive // TODO(PROC) Fix revive linter
sysconfig "github.com/DataDog/datadog-agent/cmd/system-probe/config"
Expand All @@ -38,7 +36,7 @@ var ErrProcessUnsupported = errors.New("process module unsupported")
var Process = module.Factory{
Name: config.ProcessModule,
ConfigNamespaces: []string{},
Fn: func(_ *sysconfigtypes.Config, _ workloadmeta.Component, _ telemetry.Component) (module.Module, error) {
Fn: func(_ *sysconfigtypes.Config, _ module.FactoryDependencies) (module.Module, error) {
log.Infof("Creating process module for: %s", filepath.Base(os.Args[0]))

// we disable returning zero values for stats to reduce parsing work on process-agent side
Expand Down
4 changes: 1 addition & 3 deletions cmd/system-probe/modules/tcp_queue_tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (
"github.com/DataDog/datadog-agent/cmd/system-probe/config"
sysconfigtypes "github.com/DataDog/datadog-agent/cmd/system-probe/config/types"
"github.com/DataDog/datadog-agent/cmd/system-probe/utils"
"github.com/DataDog/datadog-agent/comp/core/telemetry"
workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
"github.com/DataDog/datadog-agent/pkg/collector/corechecks/ebpf/probe/tcpqueuelength"
"github.com/DataDog/datadog-agent/pkg/ebpf"
)
Expand All @@ -28,7 +26,7 @@ import (
var TCPQueueLength = module.Factory{
Name: config.TCPQueueLengthTracerModule,
ConfigNamespaces: []string{},
Fn: func(_ *sysconfigtypes.Config, _ workloadmeta.Component, _ telemetry.Component) (module.Module, error) {
Fn: func(_ *sysconfigtypes.Config, _ module.FactoryDependencies) (module.Module, error) {
t, err := tcpqueuelength.NewTracer(ebpf.NewConfig())
if err != nil {
return nil, fmt.Errorf("unable to start the TCP queue length tracer: %w", err)
Expand Down
6 changes: 2 additions & 4 deletions cmd/system-probe/modules/traceroute.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ 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/comp/core/telemetry"
workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
"github.com/DataDog/datadog-agent/pkg/networkpath/payload"
tracerouteutil "github.com/DataDog/datadog-agent/pkg/networkpath/traceroute"
"github.com/DataDog/datadog-agent/pkg/util/log"
Expand All @@ -38,8 +36,8 @@ var (
tracerouteConfigNamespaces = []string{"traceroute"}
)

func createTracerouteModule(_ *sysconfigtypes.Config, _ workloadmeta.Component, telemetry telemetry.Component) (module.Module, error) {
runner, err := tracerouteutil.NewRunner(telemetry)
func createTracerouteModule(_ *sysconfigtypes.Config, deps module.FactoryDependencies) (module.Module, error) {
runner, err := tracerouteutil.NewRunner(deps.Telemetry)
if err != nil {
return &traceroute{}, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ 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/cmd/system-probe/utils"
"github.com/DataDog/datadog-agent/comp/core/telemetry"
workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
"github.com/DataDog/datadog-agent/pkg/collector/corechecks/servicediscovery"
"github.com/DataDog/datadog-agent/pkg/collector/corechecks/servicediscovery/apm"
"github.com/DataDog/datadog-agent/pkg/collector/corechecks/servicediscovery/language"
Expand Down Expand Up @@ -67,7 +65,7 @@ type discovery struct {
}

// NewDiscoveryModule creates a new discovery system probe module.
func NewDiscoveryModule(*sysconfigtypes.Config, workloadmeta.Component, telemetry.Component) (module.Module, error) {
func NewDiscoveryModule(*sysconfigtypes.Config, module.FactoryDependencies) (module.Module, error) {
return &discovery{
mux: &sync.RWMutex{},
cache: make(map[int32]*serviceInfo),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,10 @@ func TestCache(t *testing.T) {
core.MockBundle(),
wmmock.MockModule(workloadmeta.NewParams()),
)
module, err := NewDiscoveryModule(nil, wmeta, nil)
deps := module.FactoryDependencies{
WMeta: wmeta,
}
module, err := NewDiscoveryModule(nil, deps)
require.NoError(t, err)
discovery := module.(*discovery)

Expand Down

0 comments on commit 2172bc9

Please sign in to comment.