From 72bb7cae7b370fac3cfae359b658c1c53d683c60 Mon Sep 17 00:00:00 2001 From: Mauri de Souza Meneguzzo Date: Tue, 17 Jun 2025 08:07:32 -0300 Subject: [PATCH 1/2] otel: fix service.name for beats receivers (#44831) * otel: fix service.name for beats receivers Beats receivers currently use the hostname as the "service.name" logger field. Adjust it to reflect the beat name for consistency with standard beats. * use require.Contains before Equal * fix linter issues (cherry picked from commit df3dcadbd23c7d4df8caadfdb78f819a46abd837) # Conflicts: # x-pack/filebeat/fbreceiver/receiver_test.go --- libbeat/otelbeat/oteltest/oteltest.go | 34 ++++++++++++++++--- x-pack/filebeat/fbreceiver/receiver_test.go | 11 ++++++ x-pack/libbeat/cmd/instance/beat.go | 2 +- x-pack/metricbeat/mbreceiver/receiver_test.go | 3 ++ 4 files changed, 44 insertions(+), 6 deletions(-) diff --git a/libbeat/otelbeat/oteltest/oteltest.go b/libbeat/otelbeat/oteltest/oteltest.go index 1fb35f04d7a5..b7a68b351bc9 100644 --- a/libbeat/otelbeat/oteltest/oteltest.go +++ b/libbeat/otelbeat/oteltest/oteltest.go @@ -39,8 +39,13 @@ import ( ) type ReceiverConfig struct { - Name string - Config component.Config + // Name is the unique identifier for the component + Name string + // Beat is the name of the Beat that is running as a receiver + Beat string + // Config is the configuration for the receiver component + Config component.Config + // Factory is the factory to instantiate the receiver Factory receiver.Factory } @@ -73,6 +78,9 @@ func CheckReceivers(params CheckReceiversParams) { createReceiver := func(t *testing.T, rc ReceiverConfig) receiver.Logs { t.Helper() + require.NotEmpty(t, rc.Name, "receiver name must not be empty") + require.NotEmpty(t, rc.Beat, "receiver beat must not be empty") + var receiverSettings receiver.Settings // Replicate the behavior of the collector logger @@ -127,15 +135,31 @@ func CheckReceivers(params CheckReceiversParams) { } }) + beatForCompID := func(compID string) string { + for _, rec := range params.Receivers { + if rec.Name == compID { + return rec.Beat + } + } + + return "" + } + require.EventuallyWithT(t, func(ct *assert.CollectT) { logsMu.Lock() defer logsMu.Unlock() - // Ensure the logger fields from the otel collector are present in the logs. + // Ensure the logger fields from the otel collector are present for _, zl := range zapLogs.All() { + require.Contains(t, zl.ContextMap(), "otelcol.component.kind") + require.Equal(t, "receiver", zl.ContextMap()["otelcol.component.kind"]) + require.Contains(t, zl.ContextMap(), "otelcol.signal") + require.Equal(t, "logs", zl.ContextMap()["otelcol.signal"]) require.Contains(t, zl.ContextMap(), "otelcol.component.id") - require.Equal(t, zl.ContextMap()["otelcol.component.kind"], "receiver") - require.Equal(t, zl.ContextMap()["otelcol.signal"], "logs") + compID, ok := zl.ContextMap()["otelcol.component.id"].(string) + require.True(t, ok, "otelcol.component.id should be a string") + require.Contains(t, zl.ContextMap(), "service.name") + require.Equal(t, beatForCompID(compID), zl.ContextMap()["service.name"]) break } diff --git a/x-pack/filebeat/fbreceiver/receiver_test.go b/x-pack/filebeat/fbreceiver/receiver_test.go index a5384023d5dc..1b0efc122fe3 100644 --- a/x-pack/filebeat/fbreceiver/receiver_test.go +++ b/x-pack/filebeat/fbreceiver/receiver_test.go @@ -51,6 +51,7 @@ func TestNewReceiver(t *testing.T) { Receivers: []oteltest.ReceiverConfig{ { Name: "r1", + Beat: "filebeat", Config: &config, Factory: NewFactory(), }, @@ -155,12 +156,22 @@ func TestMultipleReceivers(t *testing.T) { Receivers: []oteltest.ReceiverConfig{ { Name: "r1", +<<<<<<< HEAD Config: config(), +======= + Beat: "filebeat", + Config: config(monitorSocket1), +>>>>>>> df3dcadbd (otel: fix service.name for beats receivers (#44831)) Factory: factory, }, { Name: "r2", +<<<<<<< HEAD Config: config(), +======= + Beat: "filebeat", + Config: config(monitorSocket2), +>>>>>>> df3dcadbd (otel: fix service.name for beats receivers (#44831)) Factory: factory, }, }, diff --git a/x-pack/libbeat/cmd/instance/beat.go b/x-pack/libbeat/cmd/instance/beat.go index 4b71a01be1b5..22562256754a 100644 --- a/x-pack/libbeat/cmd/instance/beat.go +++ b/x-pack/libbeat/cmd/instance/beat.go @@ -110,7 +110,7 @@ func NewBeatForReceiver(settings instance.Settings, receiverConfig map[string]an logpConfig := logp.Config{} logpConfig.AddCaller = true - logpConfig.Beat = b.Info.Name + logpConfig.Beat = b.Info.Beat logpConfig.Files.MaxSize = 1 if b.Config.Logging == nil { diff --git a/x-pack/metricbeat/mbreceiver/receiver_test.go b/x-pack/metricbeat/mbreceiver/receiver_test.go index 7f5b0bcef7d9..86af125b7a18 100644 --- a/x-pack/metricbeat/mbreceiver/receiver_test.go +++ b/x-pack/metricbeat/mbreceiver/receiver_test.go @@ -73,6 +73,7 @@ func TestNewReceiver(t *testing.T) { Receivers: []oteltest.ReceiverConfig{ { Name: "r1", + Beat: "metricbeat", Config: &config, Factory: NewFactory(), }, @@ -178,11 +179,13 @@ func TestMultipleReceivers(t *testing.T) { Receivers: []oteltest.ReceiverConfig{ { Name: "r1", + Beat: "metricbeat", Config: &config1, Factory: factory, }, { Name: "r2", + Beat: "metricbeat", Config: &config2, Factory: factory, }, From 795b2b8ef17b9ef5dd9c73f21263fc6ec5b8cd1b Mon Sep 17 00:00:00 2001 From: Mauri de Souza Meneguzzo Date: Tue, 17 Jun 2025 08:59:51 -0300 Subject: [PATCH 2/2] fix conflicts --- x-pack/filebeat/fbreceiver/receiver_test.go | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/x-pack/filebeat/fbreceiver/receiver_test.go b/x-pack/filebeat/fbreceiver/receiver_test.go index 1b0efc122fe3..ddb9cc8724a9 100644 --- a/x-pack/filebeat/fbreceiver/receiver_test.go +++ b/x-pack/filebeat/fbreceiver/receiver_test.go @@ -156,22 +156,14 @@ func TestMultipleReceivers(t *testing.T) { Receivers: []oteltest.ReceiverConfig{ { Name: "r1", -<<<<<<< HEAD - Config: config(), -======= Beat: "filebeat", - Config: config(monitorSocket1), ->>>>>>> df3dcadbd (otel: fix service.name for beats receivers (#44831)) + Config: config(), Factory: factory, }, { Name: "r2", -<<<<<<< HEAD - Config: config(), -======= Beat: "filebeat", - Config: config(monitorSocket2), ->>>>>>> df3dcadbd (otel: fix service.name for beats receivers (#44831)) + Config: config(), Factory: factory, }, },