prometheus icon indicating copy to clipboard operation
prometheus copied to clipboard

promtool: check metrics --extended miscalculates histogram and summary cardinality for series with differing buckets/quantiles

Open vishalmore90 opened this issue 2 weeks ago • 2 comments

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 outputting NaN%.
  • If Metric[0].Histogram or Metric[0].Summary is 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:

  1. 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) computes 2 * (2 + 2) = 8 or 2 * (2 + 5) = 14 instead of the actual 4 + 7 = 11.
  2. Panic Risk: If mf.Metric[0].Histogram or mf.Metric[0].Summary is nil, dereferencing .Bucket or .Quantile causes a nil pointer dereference.
  3. Division by Zero / NaN%: In line 1163, stats[i].percentage = float64(stats[i].cardinality) / float64(total) produces NaN% when total == 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:

  1. Sum bucket and quantile lengths across all metric instances in mf.Metric rather than multiplying by mf.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)
				}
			}
  1. Guard against total == 0 when computing percentages:
	if total > 0 {
		for i := range stats {
			stats[i].percentage = float64(stats[i].cardinality) / float64(total)
		}
	}

vishalmore90 avatar Nov 17 '26 17:11 vishalmore90

I'm working on a fix and a regression test for this issue with OpenCode.

xixi-box avatar Nov 17 '26 17:11 xixi-box

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

vishalmore90 avatar Nov 17 '26 17:11 vishalmore90