From 686721c36b7273c14cdfa61e43839d7a4299771c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juraj=20Mich=C3=A1lek?= Date: Wed, 16 Oct 2024 16:19:45 +0200 Subject: [PATCH] chore: prom translation fix metric comparison func (#35763) #### Description #### Link to tracking issue #35741 Fixes Wrong comparison func implement in translator/prometheusremotewrite. #### Testing #### Documentation Signed-off-by: Juraj Michalek --- ...-prom-translation-fix-comparison-func.yaml | 27 +++++++++++ .../prometheusremotewrite/metrics_to_prw.go | 2 +- .../metrics_to_prw_test.go | 45 +++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 .chloggen/jm-prom-translation-fix-comparison-func.yaml diff --git a/.chloggen/jm-prom-translation-fix-comparison-func.yaml b/.chloggen/jm-prom-translation-fix-comparison-func.yaml new file mode 100644 index 000000000000..7413f606bd89 --- /dev/null +++ b/.chloggen/jm-prom-translation-fix-comparison-func.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: pkg/translator/prometheusremotewrite + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Fix metric comparison func in prom translation layer + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [35741] + +# (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: [user] diff --git a/pkg/translator/prometheusremotewrite/metrics_to_prw.go b/pkg/translator/prometheusremotewrite/metrics_to_prw.go index 05d62498548a..65def1ccfa58 100644 --- a/pkg/translator/prometheusremotewrite/metrics_to_prw.go +++ b/pkg/translator/prometheusremotewrite/metrics_to_prw.go @@ -155,7 +155,7 @@ func isSameMetric(ts *prompb.TimeSeries, lbls []prompb.Label) bool { return false } for i, l := range ts.Labels { - if l.Name != ts.Labels[i].Name || l.Value != ts.Labels[i].Value { + if l.Name != lbls[i].Name || l.Value != lbls[i].Value { return false } } diff --git a/pkg/translator/prometheusremotewrite/metrics_to_prw_test.go b/pkg/translator/prometheusremotewrite/metrics_to_prw_test.go index f6171b2c2233..cda58ce5707a 100644 --- a/pkg/translator/prometheusremotewrite/metrics_to_prw_test.go +++ b/pkg/translator/prometheusremotewrite/metrics_to_prw_test.go @@ -8,6 +8,8 @@ import ( "testing" "time" + "github.com/prometheus/prometheus/prompb" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/pdata/pcommon" "go.opentelemetry.io/collector/pdata/pmetric" @@ -156,3 +158,46 @@ func generateExemplars(exemplars pmetric.ExemplarSlice, count int, ts pcommon.Ti e.SetTraceID(pcommon.TraceID{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}) } } + +func TestIsSameMetric(t *testing.T) { + tests := []struct { + name string + labels func() []prompb.Label + ts func() *prompb.TimeSeries + same bool + }{ + { + name: "same", + same: true, + labels: func() []prompb.Label { + return getPromLabels(label11, value11, label12, value12) + }, + ts: func() *prompb.TimeSeries { + return getTimeSeries( + getPromLabels(label11, value11, label12, value12), + getSample(float64(intVal1), msTime1), + ) + }, + }, + { + name: "not_same", + same: false, + labels: func() []prompb.Label { + return getPromLabels(label11, value11, label12, value12) + }, + ts: func() *prompb.TimeSeries { + return getTimeSeries( + getPromLabels(label11, value11, label21, value21), + getSample(float64(intVal1), msTime1), + ) + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + same := isSameMetric(tt.ts(), tt.labels()) + assert.Equal(t, tt.same, same) + }) + } +}