Skip to content

fix(model): remove all labels in WithoutLabels regardless of argument order - #6106

Open
amalsaidov wants to merge 2 commits into
grafana:mainfrom
amalsaidov:fix/labels-withoutlabels-unsorted
Open

amalsaidov wants to merge 2 commits into
grafana:mainfrom
amalsaidov:fix/labels-withoutlabels-unsorted

Conversation

@amalsaidov

Copy link
Copy Markdown

What this does

Labels.WithoutLabels is meant to return a copy of the label set with the
given names removed. It collects the matching label pairs in the order of the
names argument and then calls Subtract, which performs a merge join and
assumes its input is sorted. When the caller passes names in any order other
than sorted, the merge join advances past entries that should have been
dropped, so some requested labels are silently left in the result.

The sibling helpers WithLabels and BytesWithLabels scan the label set for
each name and are therefore order independent. WithoutLabels should behave the
same way.

This is reachable from the query path: timeseries.Builder and
heatmap.Builder call lbs.WithoutLabels(by...) where by is the group-by
list taken straight from the request (TimeSeries.GroupBy, Heatmap.GroupBy),
which is not guaranteed to be sorted. In that case exemplar/point labels keep
group-by labels that should have been stripped.

Fix

Sort the collected pairs before subtracting them, so every requested label is
removed regardless of argument order.

Test

Added a case to TestLabels_WithoutLabels that removes two labels passed in
unsorted order ("service", "env"). It fails before the change (the env
label is left behind) and passes after.

go test ./pkg/model/ -run TestLabels_WithoutLabels

AI-assisted; directed, reviewed and tested by the author.

… order

Labels.WithoutLabels collects the matching label pairs in the order of
the names argument and then hands them to Subtract, which performs a
merge join and assumes its argument is sorted. When the caller passes
label names in an order other than sorted (for example a group-by list
from a query), some of the requested labels were silently left in the
result.

Sort the collected pairs before subtracting them so every requested
label is removed irrespective of argument order, matching the behaviour
of the sibling WithLabels and BytesWithLabels helpers. Add a test case
covering unsorted removal names.

Signed-off-by: Amal Saidov <amalsaidov@users.noreply.github.com>
@cla-assistant

cla-assistant Bot commented Nov 15, 2026

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@github-actions

github-actions Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Signed commits report

2 of 2 commits between main and fix/labels-withoutlabels-unsorted could not be fully verified:

Commit Author Reason Message
4d2c90d7 Amal Saidov unsigned fix(model): remove all labels in WithoutLabels regardless of argument order
20fcdb86 Amal Saidov unsigned fix(model): sort WithLabels result regardless of argument order

This repository requires all commits to be signed. See GitHub docs on commit signature verification.

@bryanhuhta bryanhuhta left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Can you also apply this same fix and tests to WithLabels(...)? It looks like that function also suffers from the same bug as this one.

Also, remember to sign the CLA before we can merge this.

WithLabels collected matching pairs in the order of the names argument,
so passing an unsorted name list (for example a group-by list from a
query, as in timeseries.Builder and heatmap.Builder) produced an
unsorted label set. Sort the matched pairs before returning so the
result is a well-formed, order-independent label set, matching the fix
applied to WithoutLabels. Add a test covering unsorted keep names.

Signed-off-by: Amal Saidov <amalsaidov@users.noreply.github.com>
@amalsaidov

Copy link
Copy Markdown
Author

Good catch, thanks. WithLabels had the same order-dependence: it collected matches in the order of the names argument, so an unsorted group-by list (e.g. from timeseries.Builder/heatmap.Builder) produced an unsorted label set. Pushed a fix that sorts the matched pairs before returning, mirroring the WithoutLabels fix, plus a TestLabels_WithLabels table test including an unsorted-names case (fails before, passes after).

Will get the CLA signed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants