Skip to content

Commit

Permalink
Set agent.name to hostname by default
Browse files Browse the repository at this point in the history
Since ECS does not define agent.hostname it will be removed in a future release.
In order to always have a field available to identify the agent by name we will
set the agent.name to hostname unless the user has provided a custom name.

Relates elastic#16377
  • Loading branch information
andrewkroh committed May 2, 2020
1 parent 522ffd5 commit 22e18a2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Add keystore support for autodiscover static configurations. {pull]16306[16306]
- Add Kerberos support to Elasticsearch output. {pull}17927[17927]
- Add support for fixed length extraction in `dissect` processor. {pull}17191[17191]
- Set `agent.name` to the hostname by default. {issue}16377[16377] {pull}18000[18000]

*Auditbeat*

Expand Down
2 changes: 1 addition & 1 deletion heartbeat/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var RootCmd *cmd.BeatsRootCmd
func init() {
settings := instance.Settings{
Name: Name,
Processing: processing.MakeDefaultSupport(true, processing.WithECS, processing.WithBeatMeta("agent")),
Processing: processing.MakeDefaultSupport(true, processing.WithECS, processing.WithAgentMeta()),
HasDashboards: false,
}
RootCmd = cmd.GenRootCmdWithSettings(beater.New, settings)
Expand Down
37 changes: 29 additions & 8 deletions libbeat/publisher/processing/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ type builtinModifier func(beat.Info) common.MapStr
// MakeDefaultBeatSupport automatically adds the `ecs.version`, `host.name` and `agent.X` fields
// to each event.
func MakeDefaultBeatSupport(normalize bool) SupportFactory {
return MakeDefaultSupport(normalize, WithECS, WithHost, WithBeatMeta("agent"))
return MakeDefaultSupport(normalize, WithECS, WithHost, WithAgentMeta())
}

// MakeDefaultObserverSupport creates a new SupportFactory based on NewDefaultSupport.
// MakeDefaultObserverSupport automatically adds the `ecs.version` and `observer.X` fields
// to each event.
func MakeDefaultObserverSupport(normalize bool) SupportFactory {
return MakeDefaultSupport(normalize, WithECS, WithBeatMeta("observer"))
return MakeDefaultSupport(normalize, WithECS, WithObserverMeta())
}

// MakeDefaultSupport creates a new SupportFactory for use with the publisher pipeline.
Expand Down Expand Up @@ -139,21 +139,42 @@ var WithHost modifier = builtinModifier(func(info beat.Info) common.MapStr {
}
})

// WithBeatMeta adds beat meta information as builtin fields to a processing pipeline.
// The `key` parameter defines the field to be used.
func WithBeatMeta(key string) modifier {
// WithAgentMeta adds agent meta information as builtin fields to a processing
// pipeline.
func WithAgentMeta() modifier {
return builtinModifier(func(info beat.Info) common.MapStr {
metadata := common.MapStr{
"type": info.Beat,
"ephemeral_id": info.EphemeralID.String(),
"hostname": info.Hostname,
"id": info.ID.String(),
"name": info.Hostname,
"type": info.Beat,
"version": info.Version,
// hostname is deprecated. To be removed for 8.0. It's not in ECS.
// See https://github.com/elastic/beats/issues/16377.
"hostname": info.Hostname,
}
if info.Name != "" {
metadata["name"] = info.Name
}
return common.MapStr{"agent": metadata}
})
}

// WithObserverMeta adds beat meta information as builtin fields to a processing
// pipeline.
func WithObserverMeta() modifier {
return builtinModifier(func(info beat.Info) common.MapStr {
metadata := common.MapStr{
"type": info.Beat, // Per ECS this is not a valid type value.
"ephemeral_id": info.EphemeralID.String(), // Not in ECS.
"hostname": info.Hostname,
"id": info.ID.String(), // Not in ECS.
"version": info.Version,
}
if info.Name != info.Hostname {
metadata.Put("name", info.Name)
}
return common.MapStr{key: metadata}
return common.MapStr{"observer": metadata}
})
}

Expand Down
1 change: 1 addition & 0 deletions libbeat/publisher/processing/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ func TestProcessorsConfigs(t *testing.T) {
"agent": common.MapStr{
"ephemeral_id": "123e4567-e89b-12d3-a456-426655440000",
"hostname": "test.host.name",
"name": "test.host.name",
"id": "123e4567-e89b-12d3-a456-426655440001",
"type": "test",
"version": "0.1",
Expand Down

0 comments on commit 22e18a2

Please sign in to comment.