Skip to content

Commit

Permalink
Add metadata to diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
cmacknz committed Feb 25, 2025
1 parent c356857 commit d2047ac
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 20 deletions.
25 changes: 13 additions & 12 deletions internal/pkg/agent/application/coordinator/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -791,20 +791,21 @@ func (c *Coordinator) DiagnosticHooks() diagnostics.Hooks {
Description: "current state of the agent information of the running Elastic Agent",
ContentType: "application/yaml",
Hook: func(_ context.Context) []byte {
meta, err := c.agentInfo.ECSMetadata(c.logger)
if err != nil {
c.logger.Errorw("Error getting ECS metadata", "error.message", err)
}

output := struct {
AgentID string `yaml:"agent_id"`
Headers map[string]string `yaml:"headers"`
LogLevel string `yaml:"log_level"`
Snapshot bool `yaml:"snapshot"`
Version string `yaml:"version"`
Unprivileged bool `yaml:"unprivileged"`
Headers map[string]string `yaml:"headers"`
LogLevel string `yaml:"log_level"`
RawLogLevel string `yaml:"log_level_raw"`
Metadata *info.ECSMeta `yaml:"metadata"`
}{
AgentID: c.agentInfo.AgentID(),
Headers: c.agentInfo.Headers(),
LogLevel: c.agentInfo.LogLevel(),
Snapshot: c.agentInfo.Snapshot(),
Version: c.agentInfo.Version(),
Unprivileged: c.agentInfo.Unprivileged(),
Headers: c.agentInfo.Headers(),
LogLevel: c.agentInfo.LogLevel(),
RawLogLevel: c.agentInfo.RawLogLevel(),
Metadata: meta,
}
o, err := yaml.Marshal(output)
if err != nil {
Expand Down
63 changes: 55 additions & 8 deletions internal/pkg/agent/application/coordinator/diagnostics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/elastic/elastic-agent-client/v7/pkg/client"
"github.com/elastic/elastic-agent-client/v7/pkg/proto"

"github.com/elastic/elastic-agent/internal/pkg/agent/application/info"
"github.com/elastic/elastic-agent/internal/pkg/agent/application/upgrade/details"
"github.com/elastic/elastic-agent/internal/pkg/agent/configuration"
"github.com/elastic/elastic-agent/internal/pkg/agent/transpiler"
Expand All @@ -27,6 +28,7 @@ import (
"github.com/elastic/elastic-agent/pkg/component"
"github.com/elastic/elastic-agent/pkg/component/runtime"
agentclient "github.com/elastic/elastic-agent/pkg/control/v2/client"
"github.com/elastic/elastic-agent/pkg/core/logger"
"github.com/elastic/elastic-agent/pkg/utils/broadcaster"
)

Expand Down Expand Up @@ -144,21 +146,61 @@ func TestDiagnosticAgentInfo(t *testing.T) {
"header1": "value1",
"header2": "value2",
},
logLevel: "trace",
snapshot: true,
version: "8.14.0",
unprivileged: true,
logLevel: "trace",
meta: &info.ECSMeta{
Elastic: &info.ElasticECSMeta{
Agent: &info.AgentECSMeta{
BuildOriginal: "8.14.0-SNAPSHOT",
ID: "agent-id",
LogLevel: "trace",
Snapshot: true,
Version: "8.14.0",
Unprivileged: true,
Upgradeable: true,
},
},
Host: &info.HostECSMeta{
Arch: "arm64",
Hostname: "Test-Macbook-Pro.local",
},
OS: &info.SystemECSMeta{
Name: "macos",
Platform: "darwin",
},
},
}}

expected := `
agent_id: agent-id
headers:
header1: value1
header2: value2
log_level: trace
snapshot: true
version: 8.14.0
unprivileged: true
log_level_raw: trace
metadata:
elastic:
agent:
buildoriginal: "8.14.0-SNAPSHOT"
complete: false
id: agent-id
loglevel: trace
snapshot: true
unprivileged: true
upgradeable: true
version: 8.14.0
host:
arch: arm64
hostname: Test-Macbook-Pro.local
name: ""
id: ""
ip: []
mac: []
os:
family: ""
kernel: ""
platform: darwin
version: ""
name: macos
fullname: ""
`

hook, ok := diagnosticHooksMap(coord)["agent-info"]
Expand Down Expand Up @@ -606,6 +648,7 @@ type fakeAgentInfo struct {
version string
unprivileged bool
isStandalone bool
meta *info.ECSMeta
}

func (a fakeAgentInfo) AgentID() string {
Expand Down Expand Up @@ -640,5 +683,9 @@ func (a fakeAgentInfo) IsStandalone() bool {
return a.isStandalone
}

func (a fakeAgentInfo) ECSMetadata(l *logger.Logger) (*info.ECSMeta, error) {
return a.meta, nil
}

func (a fakeAgentInfo) ReloadID(ctx context.Context) error { panic("implement me") }
func (a fakeAgentInfo) SetLogLevel(ctx context.Context, level string) error { panic("implement me") }
3 changes: 3 additions & 0 deletions internal/pkg/agent/application/info/agent_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ type Agent interface {

// IsStandalone returns true is the agent is running in standalone mode, i.e, without fleet
IsStandalone() bool

// ECSMetadata returns the ECS metadata that is attached as part of every Fleet checkin.
ECSMetadata(*logger.Logger) (*ECSMeta, error)
}

// AgentInfo is a collection of information about agent.
Expand Down

0 comments on commit d2047ac

Please sign in to comment.