diff --git a/cmd/agent/subcommands/diagnose/command.go b/cmd/agent/subcommands/diagnose/command.go index b060a1981fe48..42c5ce6320a31 100644 --- a/cmd/agent/subcommands/diagnose/command.go +++ b/cmd/agent/subcommands/diagnose/command.go @@ -267,19 +267,8 @@ This command print the security-agent metadata payload. This payload is used by }, } - showPayloadCommand.AddCommand(payloadV5Cmd) - showPayloadCommand.AddCommand(payloadGohaiCmd) - showPayloadCommand.AddCommand(payloadInventoriesAgentCmd) - showPayloadCommand.AddCommand(payloadInventoriesHostCmd) - showPayloadCommand.AddCommand(payloadInventoriesOtelCmd) - showPayloadCommand.AddCommand(payloadInventoriesChecksCmd) - showPayloadCommand.AddCommand(payloadInventoriesPkgSigningCmd) - showPayloadCommand.AddCommand(payloadSystemProbeCmd) - showPayloadCommand.AddCommand(payloadSecurityAgentCmd) - diagnoseCommand.AddCommand(showPayloadCommand) - showAgentTelemetryCommand := &cobra.Command{ - Use: "show-telemetry", + Use: "agent-telemetry", Short: "Print agent telemetry payloads sent by the agent.", Long: `.`, RunE: func(_ *cobra.Command, _ []string) error { @@ -290,7 +279,18 @@ This command print the security-agent metadata payload. This payload is used by ) }, } - diagnoseCommand.AddCommand(showAgentTelemetryCommand) + + showPayloadCommand.AddCommand(payloadV5Cmd) + showPayloadCommand.AddCommand(payloadGohaiCmd) + showPayloadCommand.AddCommand(payloadInventoriesAgentCmd) + showPayloadCommand.AddCommand(payloadInventoriesHostCmd) + showPayloadCommand.AddCommand(payloadInventoriesOtelCmd) + showPayloadCommand.AddCommand(payloadInventoriesChecksCmd) + showPayloadCommand.AddCommand(payloadInventoriesPkgSigningCmd) + showPayloadCommand.AddCommand(payloadSystemProbeCmd) + showPayloadCommand.AddCommand(payloadSecurityAgentCmd) + showPayloadCommand.AddCommand(showAgentTelemetryCommand) + diagnoseCommand.AddCommand(showPayloadCommand) return []*cobra.Command{diagnoseCommand} } diff --git a/cmd/agent/subcommands/diagnose/command_test.go b/cmd/agent/subcommands/diagnose/command_test.go index 3316432edf833..9c1f2dcb049d4 100644 --- a/cmd/agent/subcommands/diagnose/command_test.go +++ b/cmd/agent/subcommands/diagnose/command_test.go @@ -115,3 +115,13 @@ func TestShowMetadataSecurityAgentCommand(t *testing.T) { require.Equal(t, false, secretParams.Enabled) }) } + +func TestShowAgentTelemetryCommand(t *testing.T) { + fxutil.TestOneShotSubcommand(t, + Commands(&command.GlobalParams{}), + []string{"diagnose", "show-metadata", "agent-telemetry"}, + printPayload, + func(payload payloadName) { + require.Equal(t, payloadName("agent-telemetry"), payload) + }) +} diff --git a/comp/core/agenttelemetry/fx/fx.go b/comp/core/agenttelemetry/fx/fx.go index e3cec8fc125eb..23b9be4733686 100644 --- a/comp/core/agenttelemetry/fx/fx.go +++ b/comp/core/agenttelemetry/fx/fx.go @@ -7,11 +7,17 @@ package fx import ( + agenttelemetry "github.com/DataDog/datadog-agent/comp/core/agenttelemetry/def" agenttelemetryimpl "github.com/DataDog/datadog-agent/comp/core/agenttelemetry/impl" "github.com/DataDog/datadog-agent/pkg/util/fxutil" ) // Module defines the fx options for this component func Module() fxutil.Module { - return agenttelemetryimpl.Module() + return fxutil.Component( + fxutil.ProvideComponentConstructor( + agenttelemetryimpl.NewComponent, + ), + fxutil.ProvideOptional[agenttelemetry.Component](), + ) } diff --git a/comp/core/agenttelemetry/impl/agenttelemetry.go b/comp/core/agenttelemetry/impl/agenttelemetry.go index 9d3401d66cd8b..467c8561e851e 100644 --- a/comp/core/agenttelemetry/impl/agenttelemetry.go +++ b/comp/core/agenttelemetry/impl/agenttelemetry.go @@ -15,11 +15,11 @@ package agenttelemetryimpl import ( "context" "encoding/json" + "errors" "fmt" "net/http" "strconv" - "go.uber.org/fx" "golang.org/x/exp/maps" api "github.com/DataDog/datadog-agent/comp/api/api/def" @@ -27,20 +27,13 @@ import ( "github.com/DataDog/datadog-agent/comp/core/config" log "github.com/DataDog/datadog-agent/comp/core/log/def" "github.com/DataDog/datadog-agent/comp/core/telemetry" - "github.com/DataDog/datadog-agent/pkg/util/fxutil" + compdef "github.com/DataDog/datadog-agent/comp/def" httputils "github.com/DataDog/datadog-agent/pkg/util/http" "github.com/DataDog/datadog-agent/pkg/util/scrubber" dto "github.com/prometheus/client_model/go" ) -// Module defines the fx options for this component. -func Module() fxutil.Module { - return fxutil.Component( - fx.Provide(newAgentTelemetryProvider), - ) -} - type atel struct { cfgComp config.Component logComp log.Component @@ -55,21 +48,23 @@ type atel struct { cancel context.CancelFunc } -type provides struct { - fx.Out +// Provides defines the output of the agenttelemetry component +type Provides struct { + compdef.Out Comp agenttelemetry.Component Endpoint api.AgentEndpointProvider } -type dependencies struct { - fx.In +// Requires declares the input types to the constructor +type Requires struct { + compdef.In Log log.Component Config config.Component Telemetry telemetry.Component - Lc fx.Lifecycle + Lc compdef.Lifecycle } // Interfacing with runner. @@ -142,7 +137,8 @@ func createAtel( } } -func newAgentTelemetryProvider(deps dependencies) provides { +// NewComponent creates a new agent telemetry component. +func NewComponent(deps Requires) Provides { a := createAtel( deps.Config, deps.Log, @@ -153,7 +149,7 @@ func newAgentTelemetryProvider(deps dependencies) provides { // If agent telemetry is enabled and configured properly add the start and stop hooks if a.enabled { - deps.Lc.Append(fx.Hook{ + deps.Lc.Append(compdef.Hook{ OnStart: func(_ context.Context) error { return a.start() }, @@ -163,7 +159,7 @@ func newAgentTelemetryProvider(deps dependencies) provides { }) } - return provides{ + return Provides{ Comp: a, Endpoint: api.NewAgentEndpointProvider(a.writePayload, "/metadata/agent-telemetry", "GET"), } @@ -376,6 +372,11 @@ func (a *atel) run(profiles []*Profile) { } func (a *atel) writePayload(w http.ResponseWriter, _ *http.Request) { + if !a.enabled { + httputils.SetJSONError(w, errors.New("agent-telemetry is not enabled. Please enable agent telemetry"), 400) + return + } + a.logComp.Info("Dumping agent telemetry payload") payload, err := a.GetAsJSON() diff --git a/comp/core/agenttelemetry/impl/agenttelemetry_test.go b/comp/core/agenttelemetry/impl/agenttelemetry_test.go index e7edd23d06415..57e23ee76ab2d 100644 --- a/comp/core/agenttelemetry/impl/agenttelemetry_test.go +++ b/comp/core/agenttelemetry/impl/agenttelemetry_test.go @@ -554,6 +554,7 @@ func TestTwoProfilesOnTheSameScheduleGenerateSinglePayload(t *testing.T) { // Single payload whcich has sub-payloads for each metric requestType, ok := payload["request_type"] + assert.True(t, ok) assert.Equal(t, "agent-metrics", requestType) metricsPayload, ok := payload["payload"].(map[string]interface{}) assert.True(t, ok) @@ -600,6 +601,7 @@ func TestOneProfileWithOneMetricMultipleContextsGenerateTwoPayloads(t *testing.T // One payloads each has the same metric (different tags) requestType, ok := payload["request_type"] + assert.True(t, ok) assert.Equal(t, "message-batch", requestType) metricPayloads, ok := payload["payload"].([]interface{}) assert.True(t, ok) @@ -608,6 +610,7 @@ func TestOneProfileWithOneMetricMultipleContextsGenerateTwoPayloads(t *testing.T // 2 metrics // 1-st payload1, ok := metricPayloads[0].(map[string]interface{}) + assert.True(t, ok) requestType1, ok := payload1["request_type"] assert.True(t, ok) assert.Equal(t, "agent-metrics", requestType1) @@ -621,6 +624,7 @@ func TestOneProfileWithOneMetricMultipleContextsGenerateTwoPayloads(t *testing.T // 2-nd payload2, ok := metricPayloads[1].(map[string]interface{}) + assert.True(t, ok) requestType2, ok := payload2["request_type"] assert.True(t, ok) assert.Equal(t, "agent-metrics", requestType2) @@ -672,6 +676,7 @@ func TestOneProfileWithTwoMetricGenerateSinglePayloads(t *testing.T) { // Single payload whcich has sub-payloads for each metric requestType, ok := payload["request_type"] + assert.True(t, ok) assert.Equal(t, "agent-metrics", requestType) metricsPayload, ok := payload["payload"].(map[string]interface{}) assert.True(t, ok)