Skip to content

Commit

Permalink
receiver/prometheus: remove assumption that scraped metrics share a r…
Browse files Browse the repository at this point in the history
…esource

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.
  • Loading branch information
ridwanmsharif committed Nov 20, 2024
1 parent 75ceb55 commit 4a675f9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
27 changes: 27 additions & 0 deletions .chloggen/adjuster-resource-fix.yaml
Original file line number Diff line number Diff line change
@@ -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: []
32 changes: 15 additions & 17 deletions receiver/prometheusreceiver/internal/metrics_adjuster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand Down Expand Up @@ -303,6 +300,7 @@ func (a *initialPointAdjuster) AdjustMetrics(metrics pmetric.Metrics) error {
}
}
}
tsm.Unlock()
}
return nil
}
Expand Down

0 comments on commit 4a675f9

Please sign in to comment.