Skip to content

Commit a078f7a

Browse files
authored
[procesor/groupbyattrsprocessor]: fix droping metadata when processing metrics (#33781)
**Description:** Fixes the metadata dropping when processing metrics **Link to tracking Issue:** #33419 **Testing:** <Describe what testing was performed and which tests were added.> - unit tests --------- Signed-off-by: odubajDT <[email protected]>
1 parent 62fa62f commit a078f7a

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

.chloggen/fix-metrics-metadata.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: processor/groupbyattrsprocessor
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: "Fix dropping of metadata fields when processing metrics."
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [33419]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

processor/groupbyattrsprocessor/processor.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ func getMetricInInstrumentationLibrary(ilm pmetric.ScopeMetrics, searchedMetric
206206
metric.SetDescription(searchedMetric.Description())
207207
metric.SetName(searchedMetric.Name())
208208
metric.SetUnit(searchedMetric.Unit())
209+
searchedMetric.Metadata().CopyTo(metric.Metadata())
209210

210211
// Move other special type specific values
211212
//exhaustive:enforce

processor/groupbyattrsprocessor/processor_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,59 @@ func TestCompacting(t *testing.T) {
869869
}
870870
}
871871

872+
func Test_GetMetricInInstrumentationLibrary(t *testing.T) {
873+
// input metric with datapoint
874+
m := pmetric.NewMetric()
875+
m.SetName("metric")
876+
m.SetDescription("description")
877+
m.SetUnit("unit")
878+
d := m.SetEmptyGauge().DataPoints().AppendEmpty()
879+
d.SetDoubleValue(1.0)
880+
881+
// expected metric without datapoint
882+
// the datapoints are not copied to the resulting metric, since
883+
// datapoints are moved in between metrics in the processor
884+
m2 := pmetric.NewMetric()
885+
m2.SetName("metric")
886+
m2.SetDescription("description")
887+
m2.SetUnit("unit")
888+
m2.SetEmptyGauge()
889+
890+
metadata := pcommon.NewMap()
891+
metadata.PutStr("key", "val")
892+
metadata.CopyTo(m.Metadata())
893+
metadata.CopyTo(m2.Metadata())
894+
895+
sm := pmetric.NewScopeMetrics()
896+
m.CopyTo(sm.Metrics().AppendEmpty())
897+
898+
tests := []struct {
899+
name string
900+
ilm pmetric.ScopeMetrics
901+
searched pmetric.Metric
902+
want pmetric.Metric
903+
}{
904+
{
905+
name: "existing metric",
906+
ilm: sm,
907+
searched: m,
908+
want: m,
909+
},
910+
{
911+
name: "non-existing metric - datapoints will be removed",
912+
ilm: pmetric.NewScopeMetrics(),
913+
searched: m,
914+
want: m2,
915+
},
916+
}
917+
918+
for _, tt := range tests {
919+
t.Run(tt.name, func(t *testing.T) {
920+
require.Equal(t, getMetricInInstrumentationLibrary(tt.ilm, tt.searched), tt.want)
921+
})
922+
}
923+
}
924+
872925
func BenchmarkCompacting(bb *testing.B) {
873926
runs := []struct {
874927
ilCount int

0 commit comments

Comments
 (0)