From 4a675f9d51cb4ba9b4370e9282cc6f8ca33f19ea Mon Sep 17 00:00:00 2001 From: Ridwan Sharif Date: Wed, 20 Nov 2024 20:56:33 +0000 Subject: [PATCH] receiver/prometheus: remove assumption that scraped metrics share a resource This change removes as assumption that all metrics in a single scrape come from the same resource. This is indeed not true when `honor_labels` is set to `true` AND when the scraped metrics contain a `job` or `instance` label. --- .chloggen/adjuster-resource-fix.yaml | 27 ++++++++++++++++ .../internal/metrics_adjuster.go | 32 +++++++++---------- 2 files changed, 42 insertions(+), 17 deletions(-) create mode 100644 .chloggen/adjuster-resource-fix.yaml diff --git a/.chloggen/adjuster-resource-fix.yaml b/.chloggen/adjuster-resource-fix.yaml new file mode 100644 index 000000000000..8fb0802b035b --- /dev/null +++ b/.chloggen/adjuster-resource-fix.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: receiver/prometheusreceiver + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Metric adjuster no longer assumes that all metrics from a scrape come from the same resource + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [36477] + +# (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/receiver/prometheusreceiver/internal/metrics_adjuster.go b/receiver/prometheusreceiver/internal/metrics_adjuster.go index 060450cc0f53..b4431fd24228 100644 --- a/receiver/prometheusreceiver/internal/metrics_adjuster.go +++ b/receiver/prometheusreceiver/internal/metrics_adjuster.go @@ -255,25 +255,22 @@ func NewInitialPointAdjuster(logger *zap.Logger, gcInterval time.Duration, useCr // AdjustMetrics takes a sequence of metrics and adjust their start times based on the initial and // previous points in the timeseriesMap. func (a *initialPointAdjuster) AdjustMetrics(metrics pmetric.Metrics) error { - // By contract metrics will have at least 1 data point, so for sure will have at least one ResourceMetrics. - - job, found := metrics.ResourceMetrics().At(0).Resource().Attributes().Get(semconv.AttributeServiceName) - if !found { - return errors.New("adjusting metrics without job") - } - - instance, found := metrics.ResourceMetrics().At(0).Resource().Attributes().Get(semconv.AttributeServiceInstanceID) - if !found { - return errors.New("adjusting metrics without instance") - } - tsm := a.jobsMap.get(job.Str(), instance.Str()) - - // The lock on the relevant timeseriesMap is held throughout the adjustment process to ensure that - // nothing else can modify the data used for adjustment. - tsm.Lock() - defer tsm.Unlock() for i := 0; i < metrics.ResourceMetrics().Len(); i++ { rm := metrics.ResourceMetrics().At(i) + job, found := rm.Resource().Attributes().Get(semconv.AttributeServiceName) + if !found { + return errors.New("adjusting metrics without job") + } + + instance, found := rm.Resource().Attributes().Get(semconv.AttributeServiceInstanceID) + if !found { + return errors.New("adjusting metrics without instance") + } + tsm := a.jobsMap.get(job.Str(), instance.Str()) + + // The lock on the relevant timeseriesMap is held throughout the adjustment process to ensure that + // nothing else can modify the data used for adjustment. + tsm.Lock() for j := 0; j < rm.ScopeMetrics().Len(); j++ { ilm := rm.ScopeMetrics().At(j) for k := 0; k < ilm.Metrics().Len(); k++ { @@ -303,6 +300,7 @@ func (a *initialPointAdjuster) AdjustMetrics(metrics pmetric.Metrics) error { } } } + tsm.Unlock() } return nil }