Skip to content

Commit

Permalink
[ha-agent] Add agent_group tag to datadog.agent.running metric (#31156)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreYang authored Nov 26, 2024
1 parent 50a93b7 commit 3caec42
Show file tree
Hide file tree
Showing 32 changed files with 147 additions and 45 deletions.
2 changes: 2 additions & 0 deletions cmd/agent/subcommands/diagnose/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
wmcatalog "github.com/DataDog/datadog-agent/comp/core/workloadmeta/collectors/catalog"
workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
workloadmetafx "github.com/DataDog/datadog-agent/comp/core/workloadmeta/fx"
haagentfx "github.com/DataDog/datadog-agent/comp/haagent/fx"
"github.com/DataDog/datadog-agent/comp/serializer/compression/compressionimpl"
"github.com/DataDog/datadog-agent/pkg/api/util"
pkgconfigsetup "github.com/DataDog/datadog-agent/pkg/config/setup"
Expand Down Expand Up @@ -106,6 +107,7 @@ func Commands(globalParams *command.GlobalParams) []*cobra.Command {
autodiscoveryimpl.Module(),
compressionimpl.Module(),
diagnosesendermanagerimpl.Module(),
haagentfx.Module(),
)
},
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/agent/subcommands/flare/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
wmcatalog "github.com/DataDog/datadog-agent/comp/core/workloadmeta/collectors/catalog"
workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
workloadmetafx "github.com/DataDog/datadog-agent/comp/core/workloadmeta/fx"
haagentfx "github.com/DataDog/datadog-agent/comp/haagent/fx"
"github.com/DataDog/datadog-agent/comp/metadata/host/hostimpl"
"github.com/DataDog/datadog-agent/comp/metadata/inventoryagent/inventoryagentimpl"
"github.com/DataDog/datadog-agent/comp/metadata/inventoryhost/inventoryhostimpl"
Expand Down Expand Up @@ -144,6 +145,7 @@ func Commands(globalParams *command.GlobalParams) []*cobra.Command {
return nil
}),
core.Bundle(),
haagentfx.Module(),
)
},
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/agent/subcommands/jmx/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import (
"github.com/DataDog/datadog-agent/comp/dogstatsd/pidmap"
replay "github.com/DataDog/datadog-agent/comp/dogstatsd/replay/def"
dogstatsdServer "github.com/DataDog/datadog-agent/comp/dogstatsd/server"
haagentfx "github.com/DataDog/datadog-agent/comp/haagent/fx"
"github.com/DataDog/datadog-agent/comp/remote-config/rcservice"
"github.com/DataDog/datadog-agent/comp/remote-config/rcservicemrf"
"github.com/DataDog/datadog-agent/pkg/aggregator/sender"
Expand Down Expand Up @@ -167,6 +168,7 @@ func Commands(globalParams *command.GlobalParams) []*cobra.Command {
proccontainers.InitSharedContainerProvider(wmeta, tagger)
}),
fx.Provide(func() remoteagentregistry.Component { return nil }),
haagentfx.Module(),
)
}

Expand Down
3 changes: 3 additions & 0 deletions cmd/agent/subcommands/snmp/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/DataDog/datadog-agent/comp/forwarder/eventplatform/eventplatformimpl"
"github.com/DataDog/datadog-agent/comp/forwarder/eventplatformreceiver/eventplatformreceiverimpl"
"github.com/DataDog/datadog-agent/comp/forwarder/orchestrator/orchestratorimpl"
haagentfx "github.com/DataDog/datadog-agent/comp/haagent/fx"
"github.com/DataDog/datadog-agent/comp/serializer/compression/compressionimpl"
snmpscan "github.com/DataDog/datadog-agent/comp/snmpscan/def"
snmpscanfx "github.com/DataDog/datadog-agent/comp/snmpscan/fx"
Expand Down Expand Up @@ -100,6 +101,7 @@ func Commands(globalParams *command.GlobalParams) []*cobra.Command {
nooptagger.Module(),
compressionimpl.Module(),
eventplatformreceiverimpl.Module(),
haagentfx.Module(),
)
if err != nil {
var ue configErr
Expand Down Expand Up @@ -163,6 +165,7 @@ func Commands(globalParams *command.GlobalParams) []*cobra.Command {
nooptagger.Module(),
compressionimpl.Module(),
snmpscanfx.Module(),
haagentfx.Module(),
)
if err != nil {
var ue configErr
Expand Down
2 changes: 2 additions & 0 deletions cmd/cluster-agent-cloudfoundry/subcommands/run/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import (
"github.com/DataDog/datadog-agent/comp/forwarder/eventplatform/eventplatformimpl"
"github.com/DataDog/datadog-agent/comp/forwarder/eventplatformreceiver/eventplatformreceiverimpl"
orchestratorForwarderImpl "github.com/DataDog/datadog-agent/comp/forwarder/orchestrator/orchestratorimpl"
haagentfx "github.com/DataDog/datadog-agent/comp/haagent/fx"
integrations "github.com/DataDog/datadog-agent/comp/logs/integrations/def"
"github.com/DataDog/datadog-agent/comp/serializer/compression/compressionimpl"
"github.com/DataDog/datadog-agent/pkg/clusteragent"
Expand Down Expand Up @@ -126,6 +127,7 @@ func Commands(globalParams *command.GlobalParams) []*cobra.Command {
fx.Invoke(func(wmeta workloadmeta.Component, tagger tagger.Component) {
proccontainers.InitSharedContainerProvider(wmeta, tagger)
}),
haagentfx.Module(),
)
},
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/cluster-agent/subcommands/start/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import (
"github.com/DataDog/datadog-agent/comp/forwarder/eventplatform/eventplatformimpl"
"github.com/DataDog/datadog-agent/comp/forwarder/eventplatformreceiver/eventplatformreceiverimpl"
orchestratorForwarderImpl "github.com/DataDog/datadog-agent/comp/forwarder/orchestrator/orchestratorimpl"
haagentfx "github.com/DataDog/datadog-agent/comp/haagent/fx"
integrations "github.com/DataDog/datadog-agent/comp/logs/integrations/def"
rccomp "github.com/DataDog/datadog-agent/comp/remote-config/rcservice"
"github.com/DataDog/datadog-agent/comp/remote-config/rcservice/rcserviceimpl"
Expand Down Expand Up @@ -203,6 +204,7 @@ func Commands(globalParams *command.GlobalParams) []*cobra.Command {
fx.Invoke(func(wmeta workloadmeta.Component, tagger tagger.Component) {
proccontainers.InitSharedContainerProvider(wmeta, tagger)
}),
haagentfx.Module(),
)
},
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/dogstatsd/subcommands/start/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"github.com/DataDog/datadog-agent/comp/forwarder/eventplatform/eventplatformimpl"
"github.com/DataDog/datadog-agent/comp/forwarder/eventplatformreceiver/eventplatformreceiverimpl"
orchestratorForwarderImpl "github.com/DataDog/datadog-agent/comp/forwarder/orchestrator/orchestratorimpl"
haagentfx "github.com/DataDog/datadog-agent/comp/haagent/fx"
"github.com/DataDog/datadog-agent/comp/metadata/host"
"github.com/DataDog/datadog-agent/comp/metadata/host/hostimpl"
"github.com/DataDog/datadog-agent/comp/metadata/inventoryagent"
Expand Down Expand Up @@ -173,6 +174,7 @@ func RunDogstatsdFct(cliParams *CLIParams, defaultConfPath string, defaultLogFil
}
}),
healthprobefx.Module(),
haagentfx.Module(),
)
}

Expand Down
2 changes: 2 additions & 0 deletions cmd/systray/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
log "github.com/DataDog/datadog-agent/comp/core/log/def"
nooptagger "github.com/DataDog/datadog-agent/comp/core/tagger/fx-noop"
workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
haagentfx "github.com/DataDog/datadog-agent/comp/haagent/fx"
"github.com/DataDog/datadog-agent/comp/metadata/inventoryagent/inventoryagentimpl"
"github.com/DataDog/datadog-agent/comp/serializer/compression/compressionimpl"
"github.com/DataDog/datadog-agent/comp/systray/systray"
Expand Down Expand Up @@ -125,6 +126,7 @@ func MakeCommand() *cobra.Command {
systrayimpl.Module(),
// require the systray component, causing it to start
fx.Invoke(func(_ systray.Component) {}),
haagentfx.Module(),
)
},
}
Expand Down
2 changes: 2 additions & 0 deletions comp/aggregator/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/DataDog/datadog-agent/comp/forwarder/defaultforwarder"
"github.com/DataDog/datadog-agent/comp/forwarder/eventplatform/eventplatformimpl"
orchestratorForwarderImpl "github.com/DataDog/datadog-agent/comp/forwarder/orchestrator/orchestratorimpl"
haagentmock "github.com/DataDog/datadog-agent/comp/haagent/mock"
"github.com/DataDog/datadog-agent/comp/serializer/compression/compressionimpl"
"github.com/DataDog/datadog-agent/pkg/util/fxutil"
)
Expand All @@ -26,5 +27,6 @@ func TestBundleDependencies(t *testing.T) {
orchestratorForwarderImpl.MockModule(),
eventplatformimpl.MockModule(),
nooptagger.Module(),
haagentmock.Module(),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/DataDog/datadog-agent/comp/forwarder/defaultforwarder"
"github.com/DataDog/datadog-agent/comp/forwarder/eventplatform"
orchestratorforwarder "github.com/DataDog/datadog-agent/comp/forwarder/orchestrator"
haagent "github.com/DataDog/datadog-agent/comp/haagent/def"
"github.com/DataDog/datadog-agent/comp/serializer/compression"
"github.com/DataDog/datadog-agent/pkg/aggregator"
"github.com/DataDog/datadog-agent/pkg/aggregator/sender"
Expand All @@ -42,6 +43,7 @@ type dependencies struct {
SharedForwarder defaultforwarder.Component
OrchestratorForwarder orchestratorforwarder.Component
EventPlatformForwarder eventplatform.Component
HaAgent haagent.Component
Compressor compression.Component
Tagger tagger.Component

Expand Down Expand Up @@ -86,6 +88,7 @@ func newDemultiplexer(deps dependencies) (provides, error) {
deps.OrchestratorForwarder,
options,
deps.EventPlatformForwarder,
deps.HaAgent,
deps.Compressor,
deps.Tagger,
hostnameDetected,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/DataDog/datadog-agent/comp/core/hostname"
log "github.com/DataDog/datadog-agent/comp/core/log/def"
"github.com/DataDog/datadog-agent/comp/forwarder/defaultforwarder"
haagentmock "github.com/DataDog/datadog-agent/comp/haagent/mock"
"github.com/DataDog/datadog-agent/comp/serializer/compression/compressionimpl"
"github.com/DataDog/datadog-agent/pkg/aggregator"
"github.com/DataDog/datadog-agent/pkg/aggregator/sender"
Expand Down Expand Up @@ -76,6 +77,7 @@ func newMock(deps mockDependencies) MockProvides {
Hostname: deps.Hostname,
SharedForwarder: defaultforwarder.NoopForwarder{},
Compressor: compressionimpl.NewMockCompressor(),
HaAgent: haagentmock.NewMockHaAgent(),
}

instance := &mock{AgentDemultiplexer: aggregator.InitAndStartAgentDemultiplexerForTest(aggDeps, opts, "")}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/DataDog/datadog-agent/comp/forwarder/defaultforwarder"
"github.com/DataDog/datadog-agent/comp/forwarder/eventplatform/eventplatformimpl"
"github.com/DataDog/datadog-agent/comp/forwarder/orchestrator/orchestratorimpl"
haagentmock "github.com/DataDog/datadog-agent/comp/haagent/mock"
"github.com/DataDog/datadog-agent/comp/serializer/compression/compressionimpl"
"github.com/DataDog/datadog-agent/pkg/util/fxutil"
)
Expand Down Expand Up @@ -61,6 +62,7 @@ func TestStatusOutPut(t *testing.T) {
core.MockBundle(),
compressionimpl.MockModule(),
defaultforwarder.MockModule(),
haagentmock.Module(),
orchestratorimpl.MockModule(),
eventplatformimpl.MockModule(),
fx.Provide(func() tagger.Component {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/DataDog/datadog-agent/comp/forwarder/defaultforwarder"
"github.com/DataDog/datadog-agent/comp/forwarder/eventplatform"
"github.com/DataDog/datadog-agent/comp/forwarder/eventplatform/eventplatformimpl"
haagentmock "github.com/DataDog/datadog-agent/comp/haagent/mock"
"github.com/DataDog/datadog-agent/comp/serializer/compression"
"github.com/DataDog/datadog-agent/pkg/aggregator"
pkgconfigsetup "github.com/DataDog/datadog-agent/pkg/config/setup"
Expand Down Expand Up @@ -185,6 +186,6 @@ func initTestAgentDemultiplexerWithFlushInterval(log log.Component, hostname hos
sharedForwarder := defaultforwarder.NewDefaultForwarder(pkgconfigsetup.Datadog(), log, sharedForwarderOptions)
orchestratorForwarder := optional.NewOption[defaultforwarder.Forwarder](defaultforwarder.NoopForwarder{})
eventPlatformForwarder := optional.NewOptionPtr[eventplatform.Forwarder](eventplatformimpl.NewNoopEventPlatformForwarder(hostname))
demux := aggregator.InitAndStartAgentDemultiplexer(log, sharedForwarder, &orchestratorForwarder, opts, eventPlatformForwarder, compressor, noopimpl.NewComponent(), "hostname")
demux := aggregator.InitAndStartAgentDemultiplexer(log, sharedForwarder, &orchestratorForwarder, opts, eventPlatformForwarder, haagentmock.NewMockHaAgent(), compressor, noopimpl.NewComponent(), "hostname")
return NewTestAgentDemultiplexer(demux)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/DataDog/datadog-agent/comp/forwarder/defaultforwarder"
"github.com/DataDog/datadog-agent/comp/forwarder/eventplatform"
"github.com/DataDog/datadog-agent/comp/forwarder/eventplatform/eventplatformimpl"
haagent "github.com/DataDog/datadog-agent/comp/haagent/def"
"github.com/DataDog/datadog-agent/comp/serializer/compression"
"github.com/DataDog/datadog-agent/pkg/aggregator"
"github.com/DataDog/datadog-agent/pkg/aggregator/sender"
Expand All @@ -39,6 +40,7 @@ type dependencies struct {
Hostname hostname.Component
Compressor compression.Component
Tagger tagger.Component
HaAgent haagent.Component
}

type diagnoseSenderManager struct {
Expand Down Expand Up @@ -69,6 +71,7 @@ func (sender *diagnoseSenderManager) LazyGetSenderManager() (sender.SenderManage

log := sender.deps.Log
config := sender.deps.Config
haAgent := sender.deps.HaAgent
forwarder := defaultforwarder.NewDefaultForwarder(config, log, defaultforwarder.NewOptions(config, log, nil))
orchestratorForwarder := optional.NewOptionPtr[defaultforwarder.Forwarder](defaultforwarder.NoopForwarder{})
eventPlatformForwarder := optional.NewOptionPtr[eventplatform.Forwarder](eventplatformimpl.NewNoopEventPlatformForwarder(sender.deps.Hostname))
Expand All @@ -78,6 +81,7 @@ func (sender *diagnoseSenderManager) LazyGetSenderManager() (sender.SenderManage
orchestratorForwarder,
opts,
eventPlatformForwarder,
haAgent,
sender.deps.Compressor,
sender.deps.Tagger,
hostnameDetected)
Expand Down
53 changes: 38 additions & 15 deletions comp/haagent/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,60 @@
package mock

import (
"testing"
"go.uber.org/fx"

log "github.com/DataDog/datadog-agent/comp/core/log/def"
haagent "github.com/DataDog/datadog-agent/comp/haagent/def"
"github.com/DataDog/datadog-agent/pkg/util/fxutil"
)

type mock struct {
type mockHaAgent struct {
Logger log.Component

group string
enabled bool
}

func (m *mockHaAgent) GetGroup() string {
return m.group
}

func (m *mockHaAgent) Enabled() bool {
return m.enabled
}

func (m *mock) GetGroup() string {
return "mockGroup01"
func (m *mockHaAgent) SetLeader(_ string) {
}

func (m *mock) Enabled() bool {
return true
func (m *mockHaAgent) IsLeader() bool { return false }

func (m *mockHaAgent) SetGroup(group string) {
m.group = group
}

func (m *mock) SetLeader(_ string) {
func (m *mockHaAgent) SetEnabled(enabled bool) {
m.enabled = enabled
}

func (m *mock) IsLeader() bool { return false }
// Component is the component type.
type Component interface {
haagent.Component

// Provides that defines the output of mocked snmpscan component
type Provides struct {
comp haagent.Component
SetGroup(string)
SetEnabled(bool)
}

// Mock returns a mock for haagent component.
func Mock(_ *testing.T) Provides {
return Provides{
comp: &mock{},
// NewMockHaAgent returns a new Mock
func NewMockHaAgent() haagent.Component {
return &mockHaAgent{
enabled: false,
group: "group01",
}
}

// Module defines the fx options for the mock component.
func Module() fxutil.Module {
return fxutil.Component(
fx.Provide(NewMockHaAgent),
)
}
8 changes: 7 additions & 1 deletion pkg/aggregator/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
tagger "github.com/DataDog/datadog-agent/comp/core/tagger/def"
"github.com/DataDog/datadog-agent/comp/core/tagger/types"
"github.com/DataDog/datadog-agent/comp/forwarder/eventplatform"
haagent "github.com/DataDog/datadog-agent/comp/haagent/def"
"github.com/DataDog/datadog-agent/pkg/aggregator/internal/tags"
checkid "github.com/DataDog/datadog-agent/pkg/collector/check/id"
"github.com/DataDog/datadog-agent/pkg/config/model"
Expand Down Expand Up @@ -252,6 +253,7 @@ type BufferedAggregator struct {
flushMutex sync.Mutex // to start multiple flushes in parallel
serializer serializer.MetricSerializer
eventPlatformForwarder eventplatform.Component
haAgent haagent.Component
hostname string
hostnameUpdate chan string
hostnameUpdateDone chan struct{} // signals that the hostname update is finished
Expand Down Expand Up @@ -283,7 +285,7 @@ func NewFlushAndSerializeInParallel(config model.Config) FlushAndSerializeInPara
}

// NewBufferedAggregator instantiates a BufferedAggregator
func NewBufferedAggregator(s serializer.MetricSerializer, eventPlatformForwarder eventplatform.Component, tagger tagger.Component, hostname string, flushInterval time.Duration) *BufferedAggregator {
func NewBufferedAggregator(s serializer.MetricSerializer, eventPlatformForwarder eventplatform.Component, haAgent haagent.Component, tagger tagger.Component, hostname string, flushInterval time.Duration) *BufferedAggregator {
bufferSize := pkgconfigsetup.Datadog().GetInt("aggregator_buffer_size")

agentName := flavor.GetFlavor()
Expand Down Expand Up @@ -326,6 +328,7 @@ func NewBufferedAggregator(s serializer.MetricSerializer, eventPlatformForwarder
flushInterval: flushInterval,
serializer: s,
eventPlatformForwarder: eventPlatformForwarder,
haAgent: haAgent,
hostname: hostname,
hostnameUpdate: make(chan string),
hostnameUpdateDone: make(chan struct{}),
Expand Down Expand Up @@ -861,6 +864,9 @@ func (agg *BufferedAggregator) tags(withVersion bool) []string {
tags = append(tags, "package_version:"+version.AgentPackageVersion)
}
}
if agg.haAgent.Enabled() {
tags = append(tags, "agent_group:"+agg.haAgent.GetGroup())
}
// nil to empty string
// This is expected by other components/tests
if tags == nil {
Expand Down
Loading

0 comments on commit 3caec42

Please sign in to comment.