Skip to content

Commit

Permalink
receiver/prometheus: Ensure AdjustMetrics doesn't partially modify me…
Browse files Browse the repository at this point in the history
…trics
  • Loading branch information
ridwanmsharif committed Dec 11, 2024
1 parent 9e9b27e commit 360b8e1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
10 changes: 8 additions & 2 deletions receiver/prometheusreceiver/internal/metrics_adjuster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions receiver/prometheusreceiver/internal/metrics_adjuster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,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))

Expand All @@ -767,7 +766,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)
})
Expand Down

0 comments on commit 360b8e1

Please sign in to comment.