Skip to content

[fix] Add copy of label to prevent errors#745

Merged
ldmonster merged 2 commits intomainfrom
metric-collection-in-hook
Apr 1, 2026
Merged

[fix] Add copy of label to prevent errors#745
ldmonster merged 2 commits intomainfrom
metric-collection-in-hook

Conversation

@RottenRat
Copy link
Copy Markdown
Contributor

@RottenRat RottenRat commented Mar 30, 2026

Overview

Fix map reference sharing in MemoryMetricsCollector Set and Add methods by copying labels before storing.

What this PR does / why we need it

MetricsCollector.Set and MetricsCollector.Add stored a direct reference to the labels map passed by the caller. When a hook iterates over multiple objects and reuses the same map (mutating it each iteration), all previously appended MetricOperation entries end up pointing to the same underlying map. As a result, only the last set of labels survives - earlier time series are effectively overwritten.

This PR introduces a copyLabels helper that copies the map before storing it in MetricOperation, ensuring each metric has its own independent label set.

Example of affected hook pattern:

for _, v := range violations {
    metricLabels["violating_object_name"] = v.Name
    input.MetricsCollector.Set(metricName, 1, metricLabels) // same map mutated each iteration
}

After this fix, N violations produce N independent time series in Prometheus as expected.

Signed-off-by: Sinelnikov Michail <mikhail.sinelnikov@flant.com>
@RottenRat RottenRat self-assigned this Mar 30, 2026
@RottenRat RottenRat added the bug Something isn't working label Mar 30, 2026
@RottenRat RottenRat marked this pull request as ready for review March 30, 2026 13:31
@RottenRat RottenRat changed the title Add copy of label to prevent errors [fix] Add copy of label to prevent errors Mar 30, 2026
Signed-off-by: Sinelnikov Michail <mikhail.sinelnikov@flant.com>
@ldmonster ldmonster merged commit ef18a11 into main Apr 1, 2026
8 of 9 checks passed
@ldmonster ldmonster deleted the metric-collection-in-hook branch April 1, 2026 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants