promtool: check metrics --extended miscalculates histogram and summary cardinality for series with differing buckets/quantiles
What did you do?
Ran promtool check metrics --extended on metric exposition data containing a HISTOGRAM or SUMMARY metric family where individual series have differing numbers of buckets or quantiles (for instance, when different endpoints, target instances, or client versions export custom bucket boundaries or unobserved summaries under the same metric name).
Example input (metrics.prom):
# HELP http_request_duration_seconds Histogram of HTTP request latencies.
# TYPE http_request_duration_seconds histogram
http_request_duration_seconds_bucket{handler="/api/v1",le="0.1"} 10
http_request_duration_seconds_bucket{handler="/api/v1",le="+Inf"} 10
http_request_duration_seconds_sum{handler="/api/v1"} 0.5
http_request_duration_seconds_count{handler="/api/v1"} 10
http_request_duration_seconds_bucket{handler="/api/v2",le="0.05"} 5
http_request_duration_seconds_bucket{handler="/api/v2",le="0.1"} 8
http_request_duration_seconds_bucket{handler="/api/v2",le="0.25"} 10
http_request_duration_seconds_bucket{handler="/api/v2",le="0.5"} 12
http_request_duration_seconds_bucket{handler="/api/v2",le="+Inf"} 12
http_request_duration_seconds_sum{handler="/api/v2"} 1.2
http_request_duration_seconds_count{handler="/api/v2"} 12
Command:
promtool check metrics --extended < metrics.prom
What did you expect to see?
promtool check metrics --extended should accurately calculate the total cardinality of each metric family by summing the actual number of time series created by all metric instances within that family:
- Series 1 (
handler="/api/v1"): 2 buckets +_sum+_count= 4 time series. - Series 2 (
handler="/api/v2"): 5 buckets +_sum+_count= 7 time series. - Expected total cardinality for
http_request_duration_seconds: 11.
Additionally:
- Cardinality results should be deterministic regardless of slice ordering.
- Empty or zero-metric input should safely yield
0.00%percentage without outputtingNaN%. - If
Metric[0].HistogramorMetric[0].Summaryis nil, it should not trigger a nil pointer panic.
What did you see instead? Under which circumstances?
promtool check metrics --extended erroneously reported:
Metric Cardinality Percentage
http_request_duration_seconds 8 100.00%
(or 14 depending on which series happens to be first in the metric family slice, neither of which is the actual series count of 11).
In cmd/promtool/main.go lines 1147–1155:
case dto.MetricType_HISTOGRAM:
// Histogram metrics includes sum, count, buckets.
buckets := len(mf.Metric[0].Histogram.Bucket)
cardinality = len(mf.Metric) * (2 + buckets)
case dto.MetricType_SUMMARY:
// Summary metrics includes sum, count, quantiles.
quantiles := len(mf.Metric[0].Summary.Quantile)
cardinality = len(mf.Metric) * (2 + quantiles)
The calculation makes the invalid assumption that all metric instances within a family share the exact same bucket or quantile count as mf.Metric[0]. Consequently:
- Inaccurate Cardinality: If metric series have differing bucket counts (e.g. 2 buckets on series A vs 5 buckets on series B),
cardinality = len(mf.Metric) * (2 + buckets)computes2 * (2 + 2) = 8or2 * (2 + 5) = 14instead of the actual4 + 7 = 11. - Panic Risk: If
mf.Metric[0].Histogramormf.Metric[0].Summaryis nil, dereferencing.Bucketor.Quantilecauses a nil pointer dereference. - Division by Zero /
NaN%: In line 1163,stats[i].percentage = float64(stats[i].cardinality) / float64(total)producesNaN%whentotal == 0(e.g., input with comments or empty metric families).
System information
Linux 6.8.0 x86_64 / Windows 11 amd64 (OS-independent)
Prometheus version
promtool, version 3.x (branch: main, revision: 620819d1cb9b6fa08ae409acd8e2a3a084243bbc)
Prometheus configuration file
No response
Alertmanager version
No response
Alertmanager configuration file
No response
Logs
No response
Proposed Solution
In cmd/promtool/main.go within checkMetricsExtended:
- Sum bucket and quantile lengths across all metric instances in
mf.Metricrather than multiplying bymf.Metric[0]:
case dto.MetricType_HISTOGRAM:
for _, m := range mf.Metric {
if m.Histogram != nil {
cardinality += 2 + len(m.Histogram.Bucket)
}
}
case dto.MetricType_SUMMARY:
for _, m := range mf.Metric {
if m.Summary != nil {
cardinality += 2 + len(m.Summary.Quantile)
}
}
- Guard against
total == 0when computing percentages:
if total > 0 {
for i := range stats {
stats[i].percentage = float64(stats[i].cardinality) / float64(total)
}
}
I'm working on a fix and a regression test for this issue with OpenCode.
I'm working on a fix and a regression test for this issue with OpenCode.
Hi @xixi-box I've already started working on this