Skip to content

Commit

Permalink
Create function to access system probe config (#27971)
Browse files Browse the repository at this point in the history
  • Loading branch information
hush-hush authored Aug 8, 2024
1 parent 4a5c4ce commit 6108bcf
Show file tree
Hide file tree
Showing 50 changed files with 256 additions and 285 deletions.
4 changes: 2 additions & 2 deletions cmd/agent/subcommands/flare/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ func readProfileData(seconds int) (flare.ProfileData, error) {
agentCollectors["trace"] = serviceProfileCollector(tcpGet("apm_config.debug.port"), traceCpusec)
}

if pkgconfig.SystemProbe.GetBool("system_probe_config.enabled") {
probeUtil, probeUtilErr := net.GetRemoteSystemProbeUtil(pkgconfig.SystemProbe.GetString("system_probe_config.sysprobe_socket"))
if pkgconfig.SystemProbe().GetBool("system_probe_config.enabled") {
probeUtil, probeUtilErr := net.GetRemoteSystemProbeUtil(pkgconfig.SystemProbe().GetString("system_probe_config.sysprobe_socket"))

if !errors.Is(probeUtilErr, net.ErrNotImplemented) {
sysProbeGet := func() pprofGetter {
Expand Down
14 changes: 7 additions & 7 deletions cmd/agent/subcommands/run/dependent_services_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,27 @@ var subservices = []Servicedef{
"process_config.process_collection.enabled": config.Datadog(),
"process_config.container_collection.enabled": config.Datadog(),
"process_config.process_discovery.enabled": config.Datadog(),
"network_config.enabled": config.SystemProbe,
"system_probe_config.enabled": config.SystemProbe,
"network_config.enabled": config.SystemProbe(),
"system_probe_config.enabled": config.SystemProbe(),
},
serviceName: "datadog-process-agent",
serviceInit: processInit,
},
{
name: "sysprobe",
configKeys: map[string]config.Config{
"network_config.enabled": config.SystemProbe,
"system_probe_config.enabled": config.SystemProbe,
"windows_crash_detection.enabled": config.SystemProbe,
"runtime_security_config.enabled": config.SystemProbe,
"network_config.enabled": config.SystemProbe(),
"system_probe_config.enabled": config.SystemProbe(),
"windows_crash_detection.enabled": config.SystemProbe(),
"runtime_security_config.enabled": config.SystemProbe(),
},
serviceName: "datadog-system-probe",
serviceInit: sysprobeInit,
},
{
name: "cws",
configKeys: map[string]config.Config{
"runtime_security_config.enabled": config.SystemProbe,
"runtime_security_config.enabled": config.SystemProbe(),
},
serviceName: "datadog-security-agent",
serviceInit: securityInit,
Expand Down
2 changes: 1 addition & 1 deletion cmd/system-probe/config/adjust_npm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestAdjustConnectionRollup(t *testing.T) {
for _, te := range tests {
t.Run(fmt.Sprintf("npm_enabled_%t_usm_enabled_%t", te.npmEnabled, te.usmEnabled), func(t *testing.T) {
config.ResetSystemProbeConfig(t)
cfg := config.SystemProbe
cfg := config.SystemProbe()
cfg.Set(netNS("enable_connection_rollup"), te.npmEnabled, model.SourceUnknown)
cfg.Set(smNS("enable_connection_rollup"), te.usmEnabled, model.SourceUnknown)
Adjust(cfg)
Expand Down
16 changes: 8 additions & 8 deletions cmd/system-probe/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,22 @@ func New(configPath string, fleetPoliciesDirPath string) (*types.Config, error)
}

func newSysprobeConfig(configPath string, fleetPoliciesDirPath string) (*types.Config, error) {
aconfig.SystemProbe.SetConfigName("system-probe")
aconfig.SystemProbe().SetConfigName("system-probe")
// set the paths where a config file is expected
if len(configPath) != 0 {
// if the configuration file path was supplied on the command line,
// add that first, so it's first in line
aconfig.SystemProbe.AddConfigPath(configPath)
aconfig.SystemProbe().AddConfigPath(configPath)
// If they set a config file directly, let's try to honor that
if strings.HasSuffix(configPath, ".yaml") {
aconfig.SystemProbe.SetConfigFile(configPath)
aconfig.SystemProbe().SetConfigFile(configPath)
}
} else {
// only add default if a custom configPath was not supplied
aconfig.SystemProbe.AddConfigPath(defaultConfigDir)
aconfig.SystemProbe().AddConfigPath(defaultConfigDir)
}
// load the configuration
err := aconfig.LoadCustom(aconfig.SystemProbe, aconfig.Datadog().GetEnvVars())
err := aconfig.LoadCustom(aconfig.SystemProbe(), aconfig.Datadog().GetEnvVars())
if err != nil {
if errors.Is(err, fs.ErrPermission) {
// special-case permission-denied with a clearer error message
Expand All @@ -84,7 +84,7 @@ func newSysprobeConfig(configPath string, fleetPoliciesDirPath string) (*types.C

// Load the remote configuration
if fleetPoliciesDirPath != "" {
err := aconfig.SystemProbe.MergeFleetPolicy(path.Join(fleetPoliciesDirPath, "system-probe.yaml"))
err := aconfig.SystemProbe().MergeFleetPolicy(path.Join(fleetPoliciesDirPath, "system-probe.yaml"))
if err != nil {
return nil, err
}
Expand All @@ -94,7 +94,7 @@ func newSysprobeConfig(configPath string, fleetPoliciesDirPath string) (*types.C
}

func load() (*types.Config, error) {
cfg := aconfig.SystemProbe
cfg := aconfig.SystemProbe()
Adjust(cfg)

c := &types.Config{
Expand Down Expand Up @@ -183,7 +183,7 @@ func SetupOptionalDatadogConfigWithDir(configDir, configFile string) error {
aconfig.Datadog().SetConfigFile(configFile)
}
// load the configuration
_, err := aconfig.LoadDatadogCustom(aconfig.Datadog(), "datadog.yaml", optional.NewNoneOption[secrets.Component](), aconfig.SystemProbe.GetEnvVars())
_, err := aconfig.LoadDatadogCustom(aconfig.Datadog(), "datadog.yaml", optional.NewNoneOption[secrets.Component](), aconfig.SystemProbe().GetEnvVars())
// If `!failOnMissingFile`, do not issue an error if we cannot find the default config file.
var e viper.ConfigFileNotFoundError
if err != nil && !errors.As(err, &e) {
Expand Down
9 changes: 4 additions & 5 deletions cmd/system-probe/config/config_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

func TestNetworkProcessEventMonitoring(t *testing.T) {
newConfig(t)
config.MockSystemProbe(t)

for i, te := range []struct {
network, netProcEvents bool
Expand All @@ -44,7 +44,7 @@ func TestNetworkProcessEventMonitoring(t *testing.T) {
}

func TestDynamicInstrumentation(t *testing.T) {
newConfig(t)
config.MockSystemProbe(t)
os.Setenv("DD_DYNAMIC_INSTRUMENTATION_ENABLED", "true")
defer os.Unsetenv("DD_DYNAMIC_INSTRUMENTATION_ENABLED")

Expand All @@ -62,8 +62,7 @@ func TestDynamicInstrumentation(t *testing.T) {
func TestEventStreamEnabledForSupportedKernelsLinux(t *testing.T) {
config.ResetSystemProbeConfig(t)
t.Setenv("DD_SYSTEM_PROBE_EVENT_MONITORING_NETWORK_PROCESS_ENABLED", strconv.FormatBool(true))

cfg := config.SystemProbe
cfg := config.SystemProbe()
Adjust(cfg)

if ProcessEventDataStreamSupported() {
Expand All @@ -88,7 +87,7 @@ func TestNPMEnabled(t *testing.T) {
{true, true, true, true},
}

newConfig(t)
config.MockSystemProbe(t)
for _, te := range tests {
t.Run("", func(t *testing.T) {
t.Setenv("DD_SYSTEM_PROBE_NETWORK_ENABLED", strconv.FormatBool(te.npm))
Expand Down
28 changes: 6 additions & 22 deletions cmd/system-probe/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"os"
"runtime"
"strconv"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -21,17 +20,8 @@ import (
"github.com/DataDog/datadog-agent/pkg/config"
)

func newConfig(t *testing.T) {
originalConfig := config.SystemProbe
t.Cleanup(func() {
config.SystemProbe = originalConfig
})
config.SystemProbe = config.NewConfig("system-probe", "DD", strings.NewReplacer(".", "_"))
config.InitSystemProbeConfig(config.SystemProbe)
}

func TestEventMonitor(t *testing.T) {
newConfig(t)
config.MockSystemProbe(t)

for i, tc := range []struct {
cws, fim, processEvents, networkEvents bool
Expand Down Expand Up @@ -77,8 +67,7 @@ func TestEventStreamEnabledForSupportedKernelsWindowsUnsupported(t *testing.T) {
}
config.ResetSystemProbeConfig(t)
t.Setenv("DD_SYSTEM_PROBE_EVENT_MONITORING_NETWORK_PROCESS_ENABLED", strconv.FormatBool(true))

cfg := config.SystemProbe
cfg := config.SystemProbe()
Adjust(cfg)

require.False(t, cfg.GetBool("event_monitoring_config.network_process.enabled"))
Expand All @@ -89,8 +78,7 @@ func TestEventStreamEnabledForSupportedKernelsWindowsUnsupported(t *testing.T) {
}
config.ResetSystemProbeConfig(t)
t.Setenv("DD_SYSTEM_PROBE_EVENT_MONITORING_NETWORK_PROCESS_ENABLED", strconv.FormatBool(true))

cfg := config.SystemProbe
cfg := config.SystemProbe()
Adjust(cfg)

require.False(t, cfg.GetBool("event_monitoring_config.network_process.enabled"))
Expand All @@ -110,16 +98,12 @@ discovery:
t.Run("via ENV variable", func(t *testing.T) {
config.ResetSystemProbeConfig(t)
t.Setenv("DD_DISCOVERY_ENABLED", "true")
cfg := config.SystemProbe

assert.True(t, cfg.GetBool(discoveryNS("enabled")))
assert.True(t, config.SystemProbe().GetBool(discoveryNS("enabled")))
})

t.Run("default", func(t *testing.T) {
config.ResetSystemProbeConfig(t)
cfg := config.SystemProbe

assert.False(t, cfg.GetBool(discoveryNS("enabled")))
assert.False(t, config.SystemProbe().GetBool(discoveryNS("enabled")))
})
}

Expand All @@ -135,5 +119,5 @@ func configurationFromYAML(t *testing.T, yaml string) config.Config {
f.Sync()

_, _ = New(f.Name(), "")
return config.SystemProbe
return config.SystemProbe()
}
2 changes: 1 addition & 1 deletion cmd/system-probe/modules/eventmonitor_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var EventMonitor = module.Factory{
ConfigNamespaces: eventMonitorModuleConfigNamespaces,
Fn: createEventMonitorModule,
NeedsEBPF: func() bool {
return !coreconfig.SystemProbe.GetBool("runtime_security_config.ebpfless.enabled")
return !coreconfig.SystemProbe().GetBool("runtime_security_config.ebpfless.enabled")
},
}

Expand Down
10 changes: 5 additions & 5 deletions cmd/system-probe/modules/network_tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (nt *networkTracer) Register(httpMux *module.Router) error {
})

httpMux.HandleFunc("/debug/http_monitoring", func(w http.ResponseWriter, req *http.Request) {
if !coreconfig.SystemProbe.GetBool("service_monitoring_config.enable_http_monitoring") {
if !coreconfig.SystemProbe().GetBool("service_monitoring_config.enable_http_monitoring") {
writeDisabledProtocolMessage("http", w)
return
}
Expand All @@ -164,7 +164,7 @@ func (nt *networkTracer) Register(httpMux *module.Router) error {
})

httpMux.HandleFunc("/debug/kafka_monitoring", func(w http.ResponseWriter, req *http.Request) {
if !coreconfig.SystemProbe.GetBool("service_monitoring_config.enable_kafka_monitoring") {
if !coreconfig.SystemProbe().GetBool("service_monitoring_config.enable_kafka_monitoring") {
writeDisabledProtocolMessage("kafka", w)
return
}
Expand All @@ -180,7 +180,7 @@ func (nt *networkTracer) Register(httpMux *module.Router) error {
})

httpMux.HandleFunc("/debug/postgres_monitoring", func(w http.ResponseWriter, req *http.Request) {
if !coreconfig.SystemProbe.GetBool("service_monitoring_config.enable_postgres_monitoring") {
if !coreconfig.SystemProbe().GetBool("service_monitoring_config.enable_postgres_monitoring") {
writeDisabledProtocolMessage("postgres", w)
return
}
Expand All @@ -196,7 +196,7 @@ func (nt *networkTracer) Register(httpMux *module.Router) error {
})

httpMux.HandleFunc("/debug/redis_monitoring", func(w http.ResponseWriter, req *http.Request) {
if !coreconfig.SystemProbe.GetBool("service_monitoring_config.enable_redis_monitoring") {
if !coreconfig.SystemProbe().GetBool("service_monitoring_config.enable_redis_monitoring") {
writeDisabledProtocolMessage("redis", w)
return
}
Expand All @@ -212,7 +212,7 @@ func (nt *networkTracer) Register(httpMux *module.Router) error {
})

httpMux.HandleFunc("/debug/http2_monitoring", func(w http.ResponseWriter, req *http.Request) {
if !coreconfig.SystemProbe.GetBool("service_monitoring_config.enable_http2_monitoring") {
if !coreconfig.SystemProbe().GetBool("service_monitoring_config.enable_http2_monitoring") {
writeDisabledProtocolMessage("http2", w)
return
}
Expand Down
4 changes: 2 additions & 2 deletions comp/core/config/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ func setupConfig(config pkgconfigmodel.Config, deps configDependencies) (*pkgcon
var err error
var warnings *pkgconfigmodel.Warnings
if resolver, ok := deps.getSecretResolver(); ok {
warnings, err = pkgconfigsetup.LoadWithSecret(config, resolver, pkgconfigsetup.SystemProbe.GetEnvVars())
warnings, err = pkgconfigsetup.LoadWithSecret(config, resolver, pkgconfigsetup.SystemProbe().GetEnvVars())
} else {
warnings, err = pkgconfigsetup.LoadWithoutSecret(config, pkgconfigsetup.SystemProbe.GetEnvVars())
warnings, err = pkgconfigsetup.LoadWithoutSecret(config, pkgconfigsetup.SystemProbe().GetEnvVars())
}

// If `!failOnMissingFile`, do not issue an error if we cannot find the default config file.
Expand Down
2 changes: 1 addition & 1 deletion comp/core/sysprobeconfig/sysprobeconfigimpl/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func newConfig(deps dependencies) (sysprobeconfig.Component, error) {
return nil, err
}

return &cfg{Config: config.SystemProbe, syscfg: syscfg}, nil
return &cfg{Config: config.SystemProbe(), syscfg: syscfg}, nil
}

func (c *cfg) Warnings() *config.Warnings {
Expand Down
17 changes: 9 additions & 8 deletions comp/core/sysprobeconfig/sysprobeconfigimpl/config_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (
"testing"

"github.com/DataDog/datadog-agent/comp/core/sysprobeconfig"
"github.com/DataDog/datadog-agent/pkg/config"
"github.com/DataDog/datadog-agent/pkg/config/model"
"github.com/DataDog/datadog-agent/pkg/config/setup"
"github.com/DataDog/datadog-agent/pkg/util/fxutil"
"go.uber.org/fx"
)
Expand Down Expand Up @@ -50,15 +51,15 @@ func NewMock(t *testing.T) sysprobeconfig.Component {
}

func newMock(deps mockDependencies, t testing.TB) sysprobeconfig.Component {
old := config.SystemProbe
config.SystemProbe = config.NewConfig("mock", "XXXX", strings.NewReplacer())
old := setup.SystemProbe()
setup.SetSystemProbe(model.NewConfig("mock", "XXXX", strings.NewReplacer()))
c := &cfg{
warnings: &config.Warnings{},
Config: config.SystemProbe,
warnings: &model.Warnings{},
Config: setup.SystemProbe(),
}

// call InitSystemProbeConfig to set defaults.
config.InitSystemProbeConfig(config.SystemProbe)
setup.InitSystemProbeConfig(setup.SystemProbe())

// Viper's `GetXxx` methods read environment variables at the time they are
// called, if those names were passed explicitly to BindEnv*(), so we must
Expand All @@ -80,11 +81,11 @@ func newMock(deps mockDependencies, t testing.TB) sysprobeconfig.Component {
// Overrides are explicit and will take precedence over any other
// setting
for k, v := range deps.Params.Overrides {
config.SystemProbe.SetWithoutSource(k, v)
setup.SystemProbe().SetWithoutSource(k, v)
}

// swap the existing config back at the end of the test.
t.Cleanup(func() { config.SystemProbe = old })
t.Cleanup(func() { setup.SetSystemProbe(old) })

syscfg, err := setupConfig(deps)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type collector struct {
// NewCollector returns a new local process collector provider and an error.
// Currently, this is only used on Linux when language detection and run in core agent are enabled.
func NewCollector() (workloadmeta.CollectorProvider, error) {
wlmExtractor := processwlm.GetSharedWorkloadMetaExtractor(config.SystemProbe)
wlmExtractor := processwlm.GetSharedWorkloadMetaExtractor(config.SystemProbe())
processData := NewProcessData()
processData.Register(wlmExtractor)

Expand Down
4 changes: 2 additions & 2 deletions pkg/collector/corechecks/ebpf/ebpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (m *EBPFCheck) Configure(senderManager sender.SenderManager, _ uint64, conf
if err := m.config.Parse(config); err != nil {
return fmt.Errorf("ebpf check config: %s", err)
}
if err := processnet.CheckPath(ddconfig.SystemProbe.GetString("system_probe_config.sysprobe_socket")); err != nil {
if err := processnet.CheckPath(ddconfig.SystemProbe().GetString("system_probe_config.sysprobe_socket")); err != nil {
return fmt.Errorf("sysprobe socket: %s", err)
}

Expand All @@ -80,7 +80,7 @@ func (m *EBPFCheck) Run() error {
if m.sysProbeUtil == nil {
var err error
m.sysProbeUtil, err = processnet.GetRemoteSystemProbeUtil(
ddconfig.SystemProbe.GetString("system_probe_config.sysprobe_socket"),
ddconfig.SystemProbe().GetString("system_probe_config.sysprobe_socket"),
)
if err != nil {
return fmt.Errorf("sysprobe connection: %s", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/collector/corechecks/ebpf/oomkill/oom_kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (m *OOMKillCheck) Run() error {
}

sysProbeUtil, err := process_net.GetRemoteSystemProbeUtil(
dd_config.SystemProbe.GetString("system_probe_config.sysprobe_socket"))
dd_config.SystemProbe().GetString("system_probe_config.sysprobe_socket"))
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/collector/corechecks/ebpf/probe/ebpfcheck/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,17 @@ func NewProbe(cfg *ddebpf.Config) (*Probe, error) {
return nil, err
}

if ddconfig.SystemProbe.GetBool("ebpf_check.kernel_bpf_stats") {
if ddconfig.SystemProbe().GetBool("ebpf_check.kernel_bpf_stats") {
probe.statsFD, err = ebpf.EnableStats(unix.BPF_STATS_RUN_TIME)
if err != nil {
log.Warnf("kernel ebpf stats failed to enable, program runtime and run count will be unavailable: %s", err)
}
}

probe.mapBuffers.keysBufferSizeLimit = uint32(ddconfig.SystemProbe.GetInt("ebpf_check.entry_count.max_keys_buffer_size_bytes"))
probe.mapBuffers.valuesBufferSizeLimit = uint32(ddconfig.SystemProbe.GetInt("ebpf_check.entry_count.max_values_buffer_size_bytes"))
probe.mapBuffers.iterationRestartDetectionEntries = ddconfig.SystemProbe.GetInt("ebpf_check.entry_count.entries_for_iteration_restart_detection")
probe.entryCountMaxRestarts = ddconfig.SystemProbe.GetInt("ebpf_check.entry_count.max_restarts")
probe.mapBuffers.keysBufferSizeLimit = uint32(ddconfig.SystemProbe().GetInt("ebpf_check.entry_count.max_keys_buffer_size_bytes"))
probe.mapBuffers.valuesBufferSizeLimit = uint32(ddconfig.SystemProbe().GetInt("ebpf_check.entry_count.max_values_buffer_size_bytes"))
probe.mapBuffers.iterationRestartDetectionEntries = ddconfig.SystemProbe().GetInt("ebpf_check.entry_count.entries_for_iteration_restart_detection")
probe.entryCountMaxRestarts = ddconfig.SystemProbe().GetInt("ebpf_check.entry_count.max_restarts")

if isForEachElemHelperAvailable() {
probe.mphCache = newMapProgHelperCache()
Expand Down
Loading

0 comments on commit 6108bcf

Please sign in to comment.