Skip to content

Commit d668787

Browse files
authored
Add the full set of ECS metadata sent to Fleet to Diagnostics (#7029)
* Add metadata to diagnostics * Add changelog. * Update agent info mock. * Add missing method to manual mock.
1 parent 81dec1f commit d668787

File tree

6 files changed

+168
-21
lines changed

6 files changed

+168
-21
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Kind can be one of:
2+
# - breaking-change: a change to previously-documented behavior
3+
# - deprecation: functionality that is being removed in a later release
4+
# - bug-fix: fixes a problem in a previous version
5+
# - enhancement: extends functionality but does not break or fix existing behavior
6+
# - feature: new functionality
7+
# - known-issue: problems that we are aware of in a given version
8+
# - security: impacts on the security of a product or a user’s deployment.
9+
# - upgrade: important information for someone upgrading from a prior version
10+
# - other: does not fit into any of the other categories
11+
kind: enhancement
12+
13+
# Change summary; a 80ish characters long description of the change.
14+
summary: Include all metadata that is sent to Fleet in the agent-info.yaml file in diagnostics by default.
15+
16+
# Long description; in case the summary is not enough to describe the change
17+
# this field accommodate a description without length limits.
18+
# NOTE: This field will be rendered only for breaking-change and known-issue kinds at the moment.
19+
#description:
20+
21+
# Affected component; usually one of "elastic-agent", "fleet-server", "filebeat", "metricbeat", "auditbeat", "all", etc.
22+
component: "elastic-agent"
23+
24+
# PR URL; optional; the PR number that added the changeset.
25+
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added.
26+
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number.
27+
# Please provide it if you are adding a fragment for a different PR.
28+
pr: https://github.com/elastic/elastic-agent/pull/7029
29+
30+
# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of).
31+
# If not present is automatically filled by the tooling with the issue linked to the PR number.
32+
#issue: https://github.com/owner/repo/1234

internal/pkg/agent/application/coordinator/coordinator.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -791,20 +791,21 @@ func (c *Coordinator) DiagnosticHooks() diagnostics.Hooks {
791791
Description: "current state of the agent information of the running Elastic Agent",
792792
ContentType: "application/yaml",
793793
Hook: func(_ context.Context) []byte {
794+
meta, err := c.agentInfo.ECSMetadata(c.logger)
795+
if err != nil {
796+
c.logger.Errorw("Error getting ECS metadata", "error.message", err)
797+
}
798+
794799
output := struct {
795-
AgentID string `yaml:"agent_id"`
796-
Headers map[string]string `yaml:"headers"`
797-
LogLevel string `yaml:"log_level"`
798-
Snapshot bool `yaml:"snapshot"`
799-
Version string `yaml:"version"`
800-
Unprivileged bool `yaml:"unprivileged"`
800+
Headers map[string]string `yaml:"headers"`
801+
LogLevel string `yaml:"log_level"`
802+
RawLogLevel string `yaml:"log_level_raw"`
803+
Metadata *info.ECSMeta `yaml:"metadata"`
801804
}{
802-
AgentID: c.agentInfo.AgentID(),
803-
Headers: c.agentInfo.Headers(),
804-
LogLevel: c.agentInfo.LogLevel(),
805-
Snapshot: c.agentInfo.Snapshot(),
806-
Version: c.agentInfo.Version(),
807-
Unprivileged: c.agentInfo.Unprivileged(),
805+
Headers: c.agentInfo.Headers(),
806+
LogLevel: c.agentInfo.LogLevel(),
807+
RawLogLevel: c.agentInfo.RawLogLevel(),
808+
Metadata: meta,
808809
}
809810
o, err := yaml.Marshal(output)
810811
if err != nil {

internal/pkg/agent/application/coordinator/diagnostics_test.go

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/elastic/elastic-agent-client/v7/pkg/client"
1919
"github.com/elastic/elastic-agent-client/v7/pkg/proto"
2020

21+
"github.com/elastic/elastic-agent/internal/pkg/agent/application/info"
2122
"github.com/elastic/elastic-agent/internal/pkg/agent/application/upgrade/details"
2223
"github.com/elastic/elastic-agent/internal/pkg/agent/configuration"
2324
"github.com/elastic/elastic-agent/internal/pkg/agent/transpiler"
@@ -27,6 +28,7 @@ import (
2728
"github.com/elastic/elastic-agent/pkg/component"
2829
"github.com/elastic/elastic-agent/pkg/component/runtime"
2930
agentclient "github.com/elastic/elastic-agent/pkg/control/v2/client"
31+
"github.com/elastic/elastic-agent/pkg/core/logger"
3032
"github.com/elastic/elastic-agent/pkg/utils/broadcaster"
3133
)
3234

@@ -144,21 +146,61 @@ func TestDiagnosticAgentInfo(t *testing.T) {
144146
"header1": "value1",
145147
"header2": "value2",
146148
},
147-
logLevel: "trace",
148-
snapshot: true,
149-
version: "8.14.0",
150-
unprivileged: true,
149+
logLevel: "trace",
150+
meta: &info.ECSMeta{
151+
Elastic: &info.ElasticECSMeta{
152+
Agent: &info.AgentECSMeta{
153+
BuildOriginal: "8.14.0-SNAPSHOT",
154+
ID: "agent-id",
155+
LogLevel: "trace",
156+
Snapshot: true,
157+
Version: "8.14.0",
158+
Unprivileged: true,
159+
Upgradeable: true,
160+
},
161+
},
162+
Host: &info.HostECSMeta{
163+
Arch: "arm64",
164+
Hostname: "Test-Macbook-Pro.local",
165+
},
166+
OS: &info.SystemECSMeta{
167+
Name: "macos",
168+
Platform: "darwin",
169+
},
170+
},
151171
}}
152172

153173
expected := `
154-
agent_id: agent-id
155174
headers:
156175
header1: value1
157176
header2: value2
158177
log_level: trace
159-
snapshot: true
160-
version: 8.14.0
161-
unprivileged: true
178+
log_level_raw: trace
179+
metadata:
180+
elastic:
181+
agent:
182+
buildoriginal: "8.14.0-SNAPSHOT"
183+
complete: false
184+
id: agent-id
185+
loglevel: trace
186+
snapshot: true
187+
unprivileged: true
188+
upgradeable: true
189+
version: 8.14.0
190+
host:
191+
arch: arm64
192+
hostname: Test-Macbook-Pro.local
193+
name: ""
194+
id: ""
195+
ip: []
196+
mac: []
197+
os:
198+
family: ""
199+
kernel: ""
200+
platform: darwin
201+
version: ""
202+
name: macos
203+
fullname: ""
162204
`
163205

164206
hook, ok := diagnosticHooksMap(coord)["agent-info"]
@@ -606,6 +648,7 @@ type fakeAgentInfo struct {
606648
version string
607649
unprivileged bool
608650
isStandalone bool
651+
meta *info.ECSMeta
609652
}
610653

611654
func (a fakeAgentInfo) AgentID() string {
@@ -640,5 +683,9 @@ func (a fakeAgentInfo) IsStandalone() bool {
640683
return a.isStandalone
641684
}
642685

686+
func (a fakeAgentInfo) ECSMetadata(l *logger.Logger) (*info.ECSMeta, error) {
687+
return a.meta, nil
688+
}
689+
643690
func (a fakeAgentInfo) ReloadID(ctx context.Context) error { panic("implement me") }
644691
func (a fakeAgentInfo) SetLogLevel(ctx context.Context, level string) error { panic("implement me") }

internal/pkg/agent/application/info/agent_info.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ type Agent interface {
4343

4444
// IsStandalone returns true is the agent is running in standalone mode, i.e, without fleet
4545
IsStandalone() bool
46+
47+
// ECSMetadata returns the ECS metadata that is attached as part of every Fleet checkin.
48+
ECSMetadata(*logger.Logger) (*ECSMeta, error)
4649
}
4750

4851
// AgentInfo is a collection of information about agent.

pkg/component/runtime/runtime_comm_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import (
1515

1616
"github.com/elastic/elastic-agent-client/v7/pkg/client"
1717
"github.com/elastic/elastic-agent-client/v7/pkg/proto"
18+
"github.com/elastic/elastic-agent/internal/pkg/agent/application/info"
1819
"github.com/elastic/elastic-agent/internal/pkg/core/authority"
20+
"github.com/elastic/elastic-agent/pkg/core/logger"
1921
)
2022

2123
type agentInfoMock struct {
@@ -50,6 +52,7 @@ func (a agentInfoMock) LogLevel() string { pa
5052
func (a agentInfoMock) RawLogLevel() string { panic("implement me") }
5153
func (a agentInfoMock) ReloadID(ctx context.Context) error { panic("implement me") }
5254
func (a agentInfoMock) SetLogLevel(ctx context.Context, level string) error { panic("implement me") }
55+
func (a agentInfoMock) ECSMetadata(l *logger.Logger) (*info.ECSMeta, error) { panic("implement me") }
5356

5457
func TestCheckinExpected(t *testing.T) {
5558
ca, err := authority.NewCA()

testing/mocks/internal_/pkg/agent/application/info/agent_mock.go

Lines changed: 62 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)