diff --git a/cmd/serverless/main.go b/cmd/serverless/main.go index 4ac960ed6a32e..77f46c01fded4 100644 --- a/cmd/serverless/main.go +++ b/cmd/serverless/main.go @@ -345,7 +345,7 @@ func startOtlpAgent(wg *sync.WaitGroup, metricAgent *metrics.ServerlessMetricAge func startTraceAgent(wg *sync.WaitGroup, lambdaSpanChan chan *pb.Span, coldStartSpanId uint64, serverlessDaemon *daemon.Daemon, tagger tagger.Component, rcService *remoteconfig.CoreAgentService) { defer wg.Done() traceAgent := trace.StartServerlessTraceAgent(trace.StartServerlessTraceAgentArgs{ - Enabled: pkgconfigsetup.Datadog().GetBool("apm_config.enabled"), + Enabled: configUtils.IsAPMEnabled(pkgconfigsetup.Datadog()), LoadConfig: &trace.LoadConfig{Path: datadogConfigPath, Tagger: tagger}, LambdaSpanChan: lambdaSpanChan, ColdStartSpanID: coldStartSpanId, diff --git a/comp/trace/config/setup.go b/comp/trace/config/setup.go index 70b0b3ac6355a..c095990736ceb 100644 --- a/comp/trace/config/setup.go +++ b/comp/trace/config/setup.go @@ -194,7 +194,7 @@ func applyDatadogConfig(c *config.AgentConfig, core corecompcfg.Component) error c.SkipSSLValidation = core.GetBool("skip_ssl_validation") } if core.IsSet("apm_config.enabled") { - c.Enabled = core.GetBool("apm_config.enabled") + c.Enabled = utils.IsAPMEnabled(core) } if pkgconfigsetup.Datadog().IsSet("apm_config.log_file") { c.LogFilePath = pkgconfigsetup.Datadog().GetString("apm_config.log_file") diff --git a/pkg/config/setup/config.go b/pkg/config/setup/config.go index 5690289442f25..e5a1e0f420b0d 100644 --- a/pkg/config/setup/config.go +++ b/pkg/config/setup/config.go @@ -1121,6 +1121,10 @@ func agent(config pkgconfigmodel.Setup) { config.SetKnown("proxy.http") config.SetKnown("proxy.https") config.SetKnown("proxy.no_proxy") + + // Core agent (disabled for Error Tracking Standalone, Logs Collection Only) + config.BindEnvAndSetDefault("core_agent.enabled", true) + pkgconfigmodel.AddOverrideFunc(toggleDefaultPayloads) } func fleet(config pkgconfigmodel.Setup) { @@ -2384,6 +2388,17 @@ func sanitizeExternalMetricsProviderChunkSize(config pkgconfigmodel.Config) { } } +func toggleDefaultPayloads(config pkgconfigmodel.Config) { + // Disables metric data submission (including Custom Metrics) so that hosts stop showing up in Datadog. + // Used namely for Error Tracking Standalone where it is not needed. + if !config.GetBool("core_agent.enabled") { + config.Set("enable_payloads.events", false, pkgconfigmodel.SourceAgentRuntime) + config.Set("enable_payloads.series", false, pkgconfigmodel.SourceAgentRuntime) + config.Set("enable_payloads.service_checks", false, pkgconfigmodel.SourceAgentRuntime) + config.Set("enable_payloads.sketches", false, pkgconfigmodel.SourceAgentRuntime) + } +} + func bindEnvAndSetLogsConfigKeys(config pkgconfigmodel.Setup, prefix string) { config.BindEnv(prefix + "logs_dd_url") // Send the logs to a proxy. Must respect format ':' and '' to be an integer config.BindEnv(prefix + "dd_url") @@ -2595,7 +2610,7 @@ func GetRemoteConfigurationAllowedIntegrations(cfg pkgconfigmodel.Reader) map[st return allowMap } -// IsAgentTelemetryEnabled returns true if Agent Telemetry ise enabled +// IsAgentTelemetryEnabled returns true if Agent Telemetry is enabled func IsAgentTelemetryEnabled(cfg pkgconfigmodel.Reader) bool { // Disable Agent Telemetry for GovCloud if cfg.GetBool("fips.enabled") || cfg.GetString("site") == "ddog-gov.com" { diff --git a/pkg/config/setup/config_test.go b/pkg/config/setup/config_test.go index a6250a7156220..1421770758180 100644 --- a/pkg/config/setup/config_test.go +++ b/pkg/config/setup/config_test.go @@ -1423,6 +1423,29 @@ func TestServerlessConfigInit(t *testing.T) { assert.False(t, conf.IsKnown("inventories_enabled")) } +func TestDisableCoreAgent(t *testing.T) { + pkgconfigmodel.CleanOverride(t) + conf := pkgconfigmodel.NewConfig("datadog", "DD", strings.NewReplacer(".", "_")) // nolint: forbidigo // legit use case + pkgconfigmodel.AddOverrideFunc(toggleDefaultPayloads) + + InitConfig(conf) + assert.True(t, conf.GetBool("core_agent.enabled")) + pkgconfigmodel.ApplyOverrideFuncs(conf) + // ensure events default payloads are enabled + assert.True(t, conf.GetBool("enable_payloads.events")) + assert.True(t, conf.GetBool("enable_payloads.series")) + assert.True(t, conf.GetBool("enable_payloads.service_checks")) + assert.True(t, conf.GetBool("enable_payloads.sketches")) + + conf.BindEnvAndSetDefault("core_agent.enabled", false) + pkgconfigmodel.ApplyOverrideFuncs(conf) + // ensure events default payloads are disabled + assert.False(t, conf.GetBool("enable_payloads.events")) + assert.False(t, conf.GetBool("enable_payloads.series")) + assert.False(t, conf.GetBool("enable_payloads.service_checks")) + assert.False(t, conf.GetBool("enable_payloads.sketches")) +} + func TestAgentConfigInit(t *testing.T) { conf := newTestConf() diff --git a/pkg/config/utils/miscellaneous.go b/pkg/config/utils/miscellaneous.go index 18f87b9ade392..faf3572d318d3 100644 --- a/pkg/config/utils/miscellaneous.go +++ b/pkg/config/utils/miscellaneous.go @@ -31,3 +31,29 @@ func SetLogLevel(level string, config pkgconfigmodel.Writer, source pkgconfigmod config.Set("log_level", seelogLogLevel, source) return nil } + +// IsCoreAgentEnabled checks if the Agent is able to send the payloads it and other Agents need to function with +func IsCoreAgentEnabled(cfg pkgconfigmodel.Reader) bool { + if !cfg.GetBool("core_agent.enabled") { + return false + } + + // core_agent.enabled can be true but the following payloads if set to false means + // core_agent is disabled + if !cfg.GetBool("enable_payloads.events") && + !cfg.GetBool("enable_payloads.series") && + !cfg.GetBool("enable_payloads.service_checks") && + !cfg.GetBool("enable_payloads.sketches") { + return false + } + + return true +} + +// IsAPMEnabled checks if APM is enabled or if Error Tracking standalone is enabled, simplifying the setup for +// Error Tracking standalone only via the apm_config.error_tracking_standalone.enabled option instead of requiring +// to enable also apm_config.enabled. +func IsAPMEnabled(cfg pkgconfigmodel.Reader) bool { + return (cfg.GetBool("apm_config.enabled") && IsCoreAgentEnabled(cfg)) || + cfg.GetBool("apm_config.error_tracking_standalone.enabled") +} diff --git a/pkg/config/utils/miscellaneous_test.go b/pkg/config/utils/miscellaneous_test.go new file mode 100644 index 0000000000000..96c65f5b33b9c --- /dev/null +++ b/pkg/config/utils/miscellaneous_test.go @@ -0,0 +1,66 @@ +// 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. + +package utils + +import ( + "github.com/stretchr/testify/assert" + "testing" + + configmock "github.com/DataDog/datadog-agent/pkg/config/mock" + "github.com/DataDog/datadog-agent/pkg/config/model" +) + +func TestIsCoreAgentEnabled(t *testing.T) { + + tests := []struct { + name string + expected bool + setConfig func(m model.Config) + }{ + { + name: "core_agent.enabled false", + expected: false, + setConfig: func(m model.Config) { + m.SetWithoutSource("core_agent.enabled", false) + }, + }, + { + name: "All enable_payloads.enabled false", + expected: false, + setConfig: func(m model.Config) { + m.SetWithoutSource("enable_payloads.events", false) + m.SetWithoutSource("enable_payloads.series", false) + m.SetWithoutSource("enable_payloads.service_checks", false) + m.SetWithoutSource("enable_payloads.sketches", false) + }, + }, + { + name: "Some enable_payloads.enabled false", + expected: true, + setConfig: func(m model.Config) { + m.SetWithoutSource("enable_payloads.events", false) + m.SetWithoutSource("enable_payloads.series", true) + m.SetWithoutSource("enable_payloads.service_checks", false) + m.SetWithoutSource("enable_payloads.sketches", true) + }, + }, + { + name: "default values", + expected: true, + setConfig: func(_ model.Config) {}, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + mockConfig := configmock.New(t) + test.setConfig(mockConfig) + assert.Equal(t, + test.expected, IsCoreAgentEnabled(mockConfig), + "Was expecting IsCoreAgentEnabled to return", test.expected) + }) + } +}