Skip to content

[metrics.payload] Sort labels when processing json metrics - #1233

Merged
manugarg merged 1 commit into
mainfrom
json_metrics_labels
Feb 21, 2026
Merged

manugarg merged 1 commit into
mainfrom
json_metrics_labels

Conversation

@manugarg

@manugarg manugarg commented Feb 21, 2026 •

Copy link
Copy Markdown
Contributor
  • 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 to verify this behavior.

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.

@manugarg
manugarg merged commit adc115c into main Feb 21, 2026
11 of 12 checks passed
@manugarg
manugarg deleted the json_metrics_labels branch February 21, 2026 05:09
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.
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.

1 participant