Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fx related issues for agenttelemetry improvements #29789

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions cmd/agent/subcommands/diagnose/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This is a breaking change. I guess this should be OK as the command is very new.

Copy link
Member Author

@GustavoCaso GustavoCaso Oct 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not a breaking change as the command is not release yet. This PR uses this PR as the starting point #29770

The starting point PR hasn't being merge yet

Short: "Print agent telemetry payloads sent by the agent.",
Long: `.`,
RunE: func(_ *cobra.Command, _ []string) error {
Expand All @@ -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}
}
Expand Down
10 changes: 10 additions & 0 deletions cmd/agent/subcommands/diagnose/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(_ core.BundleParams, secretParams secrets.Params) {
require.Equal(t, false, secretParams.Enabled)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❓ question: ‏What is the goal of this test ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect this test to check "agent-telemetry"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ogaca-dd @misteriaud fixed 😄

})
}
8 changes: 7 additions & 1 deletion comp/core/agenttelemetry/fx/fx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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](),
)
}
35 changes: 18 additions & 17 deletions comp/core/agenttelemetry/impl/agenttelemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,25 @@ 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"
agenttelemetry "github.com/DataDog/datadog-agent/comp/core/agenttelemetry/def"
"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
Expand All @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -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()
},
Expand All @@ -163,7 +159,7 @@ func newAgentTelemetryProvider(deps dependencies) provides {
})
}

return provides{
return Provides{
Comp: a,
Endpoint: api.NewAgentEndpointProvider(a.writePayload, "/metadata/agent-telemetry", "GET"),
}
Expand Down Expand Up @@ -376,6 +372,11 @@ func (a *atel) run(profiles []*Profile) {
}

func (a *atel) writePayload(w http.ResponseWriter, _ *http.Request) {
if !a.enabled {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to check id the feature is enable otherwise a.logComp would panic 😄

httputils.SetJSONError(w, errors.New("agent-telemetry is not enabled. Please enable agent telemetry"), 400)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iglendd I'm open to change the error message display to the users, maybe a link to the documentation would be nice

return
}

a.logComp.Info("Dumping agent telemetry payload")

payload, err := a.GetAsJSON()
Expand Down
5 changes: 5 additions & 0 deletions comp/core/agenttelemetry/impl/agenttelemetry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Loading