From a078f7abbc0aded6f40ff40ac2ace661e6a0aa42 Mon Sep 17 00:00:00 2001 From: odubajDT <93584209+odubajDT@users.noreply.github.com> Date: Mon, 15 Jul 2024 13:59:03 +0200 Subject: [PATCH] [procesor/groupbyattrsprocessor]: fix droping metadata when processing metrics (#33781) **Description:** Fixes the metadata dropping when processing metrics **Link to tracking Issue:** #33419 **Testing:** - unit tests --------- Signed-off-by: odubajDT --- .chloggen/fix-metrics-metadata.yaml | 27 ++++++++++ processor/groupbyattrsprocessor/processor.go | 1 + .../groupbyattrsprocessor/processor_test.go | 53 +++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 .chloggen/fix-metrics-metadata.yaml diff --git a/.chloggen/fix-metrics-metadata.yaml b/.chloggen/fix-metrics-metadata.yaml new file mode 100644 index 000000000000..b9d52e72809e --- /dev/null +++ b/.chloggen/fix-metrics-metadata.yaml @@ -0,0 +1,27 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: bug_fix + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: processor/groupbyattrsprocessor + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: "Fix dropping of metadata fields when processing metrics." + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [33419] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] diff --git a/processor/groupbyattrsprocessor/processor.go b/processor/groupbyattrsprocessor/processor.go index 734721bebe22..d91193145264 100644 --- a/processor/groupbyattrsprocessor/processor.go +++ b/processor/groupbyattrsprocessor/processor.go @@ -206,6 +206,7 @@ func getMetricInInstrumentationLibrary(ilm pmetric.ScopeMetrics, searchedMetric metric.SetDescription(searchedMetric.Description()) metric.SetName(searchedMetric.Name()) metric.SetUnit(searchedMetric.Unit()) + searchedMetric.Metadata().CopyTo(metric.Metadata()) // Move other special type specific values //exhaustive:enforce diff --git a/processor/groupbyattrsprocessor/processor_test.go b/processor/groupbyattrsprocessor/processor_test.go index 73c8818dad3a..315880f90cee 100644 --- a/processor/groupbyattrsprocessor/processor_test.go +++ b/processor/groupbyattrsprocessor/processor_test.go @@ -869,6 +869,59 @@ func TestCompacting(t *testing.T) { } } +func Test_GetMetricInInstrumentationLibrary(t *testing.T) { + // input metric with datapoint + m := pmetric.NewMetric() + m.SetName("metric") + m.SetDescription("description") + m.SetUnit("unit") + d := m.SetEmptyGauge().DataPoints().AppendEmpty() + d.SetDoubleValue(1.0) + + // expected metric without datapoint + // the datapoints are not copied to the resulting metric, since + // datapoints are moved in between metrics in the processor + m2 := pmetric.NewMetric() + m2.SetName("metric") + m2.SetDescription("description") + m2.SetUnit("unit") + m2.SetEmptyGauge() + + metadata := pcommon.NewMap() + metadata.PutStr("key", "val") + metadata.CopyTo(m.Metadata()) + metadata.CopyTo(m2.Metadata()) + + sm := pmetric.NewScopeMetrics() + m.CopyTo(sm.Metrics().AppendEmpty()) + + tests := []struct { + name string + ilm pmetric.ScopeMetrics + searched pmetric.Metric + want pmetric.Metric + }{ + { + name: "existing metric", + ilm: sm, + searched: m, + want: m, + }, + { + name: "non-existing metric - datapoints will be removed", + ilm: pmetric.NewScopeMetrics(), + searched: m, + want: m2, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + require.Equal(t, getMetricInInstrumentationLibrary(tt.ilm, tt.searched), tt.want) + }) + } +} + func BenchmarkCompacting(bb *testing.B) { runs := []struct { ilCount int