Repository navigation
[metrics.payload] Sort labels when processing json metrics - #1233
Merged
Merged
Conversation
conallob
pushed a commit
to conallob/cloudprober
that referenced
this pull request
Mar 10, 2026
- Ensure that labels extracted from JSON objects are always sorted alphabetically by key. - In Go, map iteration order is random. Without sorting, evaluating labels derived from a JSON object (like `labels_jq_filter`) leads to unpredictable output strings since the labels append in varying orders. - Sorting the keys matches the behavior used for `metricsMap` and guarantees consistent, predictable text formatting. - Added a new test case to [json_metrics_test.go](cci:7://file:///home/manugarg/code/cloudprober-wt/main/metrics/payload/json_metrics_test.go:0:0-0:0) to verify this behavior.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
labels_jq_filter) leads to unpredictable output strings since the labels append in varying orders.metricsMapand guarantees consistent, predictable text formatting.Note:
Cloudprober mostly assumes deterministic labels order – to avoid sorting them while determining caching key or exporting metrics. For native metrics like success/latency etc, it’s not problem as labels are always added by us and order remains stable. For payload based metrics, we explicitly sort line metrics. But, we were not doing any sorting for JSON based metrics.
Out of order labels can affect prometheus surfacer in a subtle way. For prometheus metrics we do caching and use labels as the caching key. Now if label order had changes, new EventMetric will become a different timeseries (instead of just updating the existing timeseries). In such a case, prometheus scraper will just accept the last scraped value for the same set of metric and labels. In such a case it's possible that the old value will continue to win.