From 51d7f024dd1b1753cf46e65361d8344730598b1d Mon Sep 17 00:00:00 2001 From: Julien Pivotto <291750+roidelapluie@users.noreply.github.com> Date: Mon, 11 May 2026 11:34:17 +0200 Subject: [PATCH] promql: set Schema in HistogramStatsIterator to detect schema mix Without Schema being propagated in HistogramStatsIterator, histograms served through the stats-only path (e.g. histogram_count(rate(...))) all appear as Schema=0, causing the exponential/custom-bucket mismatch detection to be silently skipped. Add a test that exercises histogram_count(rate()) over a series with mixed exponential and custom bucket schemas to verify the warning is emitted. Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com> --- promql/histogram_stats_iterator.go | 4 ++++ promql/promqltest/testdata/native_histograms.test | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/promql/histogram_stats_iterator.go b/promql/histogram_stats_iterator.go index 87cc5acfbd..f6afb50b61 100644 --- a/promql/histogram_stats_iterator.go +++ b/promql/histogram_stats_iterator.go @@ -86,10 +86,14 @@ func (*HistogramStatsIterator) AtHistogram(*histogram.Histogram) (int64, *histog // performs a counter reset detection on the fly. It will return an explicit // hint (not UnknownCounterReset) if the previous sample has been accessed with // the same iterator. +// +// The returned histogram contains only Count, Sum, CounterResetHint, and Schema. +// Bucket data is intentionally omitted. func (hsi *HistogramStatsIterator) AtFloatHistogram(fh *histogram.FloatHistogram) (int64, *histogram.FloatHistogram) { populateFH := func(src *histogram.FloatHistogram, detectReset bool) { h := histogram.FloatHistogram{ CounterResetHint: src.CounterResetHint, + Schema: src.Schema, Count: src.Count, Sum: src.Sum, } diff --git a/promql/promqltest/testdata/native_histograms.test b/promql/promqltest/testdata/native_histograms.test index 88ba36c86b..ce473dc69a 100644 --- a/promql/promqltest/testdata/native_histograms.test +++ b/promql/promqltest/testdata/native_histograms.test @@ -1118,6 +1118,13 @@ eval instant at 1m30s rate(some_metric[1m30s]) expect warn msg: PromQL warning: vector contains a mix of histograms with exponential and custom buckets schemas for metric name "some_metric" # Should produce no results. +# histogram_count(rate()) must also warn when the schema mix falls inside the stats-only +# path (HistogramStatsIterator). Without Schema being set in that iterator, all histograms +# appear as Schema=0 and the custom-bucket mismatch is silently missed. +eval instant at 1m histogram_count(rate(some_metric[1m30s])) + expect warn msg: PromQL warning: vector contains a mix of histograms with exponential and custom buckets schemas for metric name "some_metric" + # Should produce no results. + # Start with custom, end with exponential. Return the exponential histogram divided by 48. # (The 1st sample is the NHCB with count:1. It is mostly ignored with the exception of the # count, which means the rate calculation extrapolates until the count hits 0.)