From 72df5e54015f353a166be373200563a6047d6db7 Mon Sep 17 00:00:00 2001 From: Silvia Mitter Date: Fri, 15 Feb 2019 09:38:24 +0100 Subject: [PATCH] Intoduce SkipAddHostName setting. (#10728) fixes #apm-server/1846 --- CHANGELOG-developer.next.asciidoc | 1 + libbeat/beat/pipeline.go | 4 ++ libbeat/publisher/pipeline/module.go | 3 -- libbeat/publisher/pipeline/processor.go | 11 ++++- libbeat/publisher/pipeline/processor_test.go | 48 ++++++++++++++++++++ 5 files changed, 63 insertions(+), 4 deletions(-) diff --git a/CHANGELOG-developer.next.asciidoc b/CHANGELOG-developer.next.asciidoc index a18201b14e4..f734ae0438e 100644 --- a/CHANGELOG-developer.next.asciidoc +++ b/CHANGELOG-developer.next.asciidoc @@ -34,3 +34,4 @@ The list below covers the major changes between 7.0.0-alpha2 and master only. - Introduce ILM and IndexManagement support to beat.Settings. {pull}10347[10347] - Generating index pattern on demand instead of shipping them in the packages. {pull}10478[10478] - Metricset generator generates beta modules by default now. {pull}10657[10657] +- Move host name addition to a processor. {pull}10728[10728] \ No newline at end of file diff --git a/libbeat/beat/pipeline.go b/libbeat/beat/pipeline.go index 3db7baae6d2..435f48203ea 100644 --- a/libbeat/beat/pipeline.go +++ b/libbeat/beat/pipeline.go @@ -80,6 +80,10 @@ type ClientConfig struct { // To skip adding that metadata set this to true. SkipAgentMetadata bool + // By default events are decorated with a host name. + // To skip adding that host.name set this to true. + SkipHostName bool + // ACK handler strategies. // Note: ack handlers are run in another go-routine owned by the publisher pipeline. // They should not block for to long, to not block the internal buffers for diff --git a/libbeat/publisher/pipeline/module.go b/libbeat/publisher/pipeline/module.go index 2c854c13676..7af329d8429 100644 --- a/libbeat/publisher/pipeline/module.go +++ b/libbeat/publisher/pipeline/module.go @@ -85,9 +85,6 @@ func Load( Annotations: Annotations{ Event: config.EventMetadata, Builtin: common.MapStr{ - "host": common.MapStr{ - "name": name, - }, "ecs": common.MapStr{ "version": "1.0.0-beta2", }, diff --git a/libbeat/publisher/pipeline/processor.go b/libbeat/publisher/pipeline/processor.go index 81834f7bdff..0dcaa59c98e 100644 --- a/libbeat/publisher/pipeline/processor.go +++ b/libbeat/publisher/pipeline/processor.go @@ -127,12 +127,17 @@ func newProcessorPipeline( processors.add(actions.NewAddFields(meta, needsCopy)) } - // setup 7: add agent metadata + // setup 7: add agent metadata and host name if !config.SkipAgentMetadata { needsCopy := global.alwaysCopy || global.processors != nil processors.add(actions.NewAddFields(createAgentFields(info), needsCopy)) } + if !config.SkipHostName { + needsCopy := global.alwaysCopy || global.processors != nil + processors.add(actions.NewAddFields(addHostName(info), needsCopy)) + } + // setup 8: pipeline processors list processors.add(global.processors) @@ -283,6 +288,10 @@ func createAgentFields(info beat.Info) common.MapStr { return common.MapStr{"agent": metadata} } +func addHostName(info beat.Info) common.MapStr { + return common.MapStr{"host": common.MapStr{"name": info.Name}} +} + func debugPrintProcessor(info beat.Info, monitors Monitors) *processorFn { // ensure only one go-routine is using the encoder (in case // beat.Client is shared between multiple go-routines by accident) diff --git a/libbeat/publisher/pipeline/processor_test.go b/libbeat/publisher/pipeline/processor_test.go index 07b643d0c81..ea1e06c6a78 100644 --- a/libbeat/publisher/pipeline/processor_test.go +++ b/libbeat/publisher/pipeline/processor_test.go @@ -38,6 +38,7 @@ func TestProcessors(t *testing.T) { events []common.MapStr expected []common.MapStr includeAgentMetadata bool + includeHostName bool } tests := []struct { @@ -148,6 +149,52 @@ func TestProcessors(t *testing.T) { }, }, }, + { + name: "add host name", + global: pipelineProcessors{ + fields: common.MapStr{"global": 1, "host": common.MapStr{"name": "host123"}}, + }, + info: &beat.Info{ + Hostname: "test.host.hostname", + Name: "test.host.name", + }, + local: []local{ + { + config: beat.ClientConfig{}, + events: []common.MapStr{{"value": "abc"}}, + expected: []common.MapStr{ + { + "host": common.MapStr{"name": "test.host.name"}, + "value": "abc", "global": 1, + }, + }, + includeHostName: true, + }, + }, + }, + { + name: "add host name to existing host", + global: pipelineProcessors{ + fields: common.MapStr{"global": 1, "host": common.MapStr{"name": "host123"}}, + }, + info: &beat.Info{ + Hostname: "test.host.hostname", + Name: "test.host.name", + }, + local: []local{ + { + config: beat.ClientConfig{}, + events: []common.MapStr{{"value": "abc", "host": common.MapStr{"hostname": "test.other.hostname"}}}, + expected: []common.MapStr{ + { + "host": common.MapStr{"name": "test.host.name", "hostname": "test.other.hostname"}, + "value": "abc", "global": 1, + }, + }, + includeHostName: true, + }, + }, + }, { name: "beat local fields", local: []local{ @@ -393,6 +440,7 @@ func TestProcessors(t *testing.T) { } for i, local := range test.local { local.config.SkipAgentMetadata = !local.includeAgentMetadata + local.config.SkipHostName = !local.includeHostName programs[i] = newProcessorPipeline(info, monitors, test.global, local.config) }