From 834bb255f093b4dc554271e97d3c99b83c074c5a Mon Sep 17 00:00:00 2001 From: Ridwan Sharif Date: Wed, 11 Dec 2024 23:59:58 +0600 Subject: [PATCH] receiver/prometheus: Ensure AdjustMetrics doesn't partially modify metrics --- .../prometheusreceiver/internal/metrics_adjuster.go | 10 ++++++++-- .../internal/metrics_adjuster_test.go | 3 +-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/receiver/prometheusreceiver/internal/metrics_adjuster.go b/receiver/prometheusreceiver/internal/metrics_adjuster.go index b4431fd24228..ebb33bb970b5 100644 --- a/receiver/prometheusreceiver/internal/metrics_adjuster.go +++ b/receiver/prometheusreceiver/internal/metrics_adjuster.go @@ -257,15 +257,21 @@ func NewInitialPointAdjuster(logger *zap.Logger, gcInterval time.Duration, useCr func (a *initialPointAdjuster) AdjustMetrics(metrics pmetric.Metrics) error { for i := 0; i < metrics.ResourceMetrics().Len(); i++ { rm := metrics.ResourceMetrics().At(i) - job, found := rm.Resource().Attributes().Get(semconv.AttributeServiceName) + _, found := rm.Resource().Attributes().Get(semconv.AttributeServiceName) if !found { return errors.New("adjusting metrics without job") } - instance, found := rm.Resource().Attributes().Get(semconv.AttributeServiceInstanceID) + _, found = rm.Resource().Attributes().Get(semconv.AttributeServiceInstanceID) if !found { return errors.New("adjusting metrics without instance") } + } + + for i := 0; i < metrics.ResourceMetrics().Len(); i++ { + rm := metrics.ResourceMetrics().At(i) + job, _ := rm.Resource().Attributes().Get(semconv.AttributeServiceName) + instance, _ := rm.Resource().Attributes().Get(semconv.AttributeServiceInstanceID) tsm := a.jobsMap.get(job.Str(), instance.Str()) // The lock on the relevant timeseriesMap is held throughout the adjustment process to ensure that diff --git a/receiver/prometheusreceiver/internal/metrics_adjuster_test.go b/receiver/prometheusreceiver/internal/metrics_adjuster_test.go index 3998503d897e..1f7779269556 100644 --- a/receiver/prometheusreceiver/internal/metrics_adjuster_test.go +++ b/receiver/prometheusreceiver/internal/metrics_adjuster_test.go @@ -134,6 +134,7 @@ func TestSumWithDifferentResources(t *testing.T) { } runScript(t, NewInitialPointAdjuster(zap.NewNop(), time.Minute, true), "job", "0", script) } + func TestSummaryNoCount(t *testing.T) { script := []*metricsAdjusterTest{ { @@ -752,7 +753,6 @@ func runScript(t *testing.T, ma MetricsAdjuster, job, instance string, tests []* if !found { rm.Resource().Attributes().PutStr(semconv.AttributeServiceInstanceID, instance) } - } assert.NoError(t, ma.AdjustMetrics(adjusted)) @@ -767,7 +767,6 @@ func runScript(t *testing.T, ma MetricsAdjuster, job, instance string, tests []* if !found { rm.Resource().Attributes().PutStr(semconv.AttributeServiceInstanceID, instance) } - } assert.EqualValues(t, test.adjusted, adjusted) })