From bccb1d645954880c70eae1eeb703186c1f92c697 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gy=C3=B6rgy=20Krajcsovits?= Date: Wed, 4 Jun 2025 10:24:50 +0200 Subject: [PATCH] fix(promql): do not loose information about buckets when doing the detect MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: György Krajcsovits --- promql/histogram_stats_iterator.go | 33 +++++++++++++------------ promql/histogram_stats_iterator_test.go | 2 +- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/promql/histogram_stats_iterator.go b/promql/histogram_stats_iterator.go index 7b749edd71..59814d1b35 100644 --- a/promql/histogram_stats_iterator.go +++ b/promql/histogram_stats_iterator.go @@ -126,17 +126,15 @@ func (f *histogramStatsIterator) getFloatResetHint(hint histogram.CounterResetHi if hint != histogram.UnknownCounterReset { return hint } - if f.lastFH == nil { - // If there was no previous histogram, this will not be used, - // and if there was a previous integer histogram, this will - // force a reset detection. The later can happen if we used the - // iterator to read integer histograms before, but switched to - // float histograms for whatever reason. - // https://github.com/prometheus/prometheus/issues/16681 - return histogram.UnknownCounterReset + prevFH := f.lastFH + if prevFH == nil { + if f.lastH == nil { + // We don't know if there's a counter reset. + return histogram.UnknownCounterReset + } + prevFH = f.lastH.ToFloat(nil) } - - if f.currentFH.DetectReset(f.lastFH) { + if f.currentFH.DetectReset(prevFH) { return histogram.CounterReset } return histogram.NotCounterReset @@ -146,14 +144,17 @@ func (f *histogramStatsIterator) getResetHint(h *histogram.Histogram) histogram. if h.CounterResetHint != histogram.UnknownCounterReset { return h.CounterResetHint } + var prevFH *histogram.FloatHistogram if f.lastH == nil { - // If there was no previous histogram, this will not be used, - // and if there was a previous float histogram, this will - // force a reset detection. - return histogram.UnknownCounterReset + if f.lastFH == nil { + // We don't know if there's a counter reset. + return histogram.UnknownCounterReset + } + prevFH = f.lastFH + } else { + prevFH = f.lastH.ToFloat(nil) } - - fh, prevFH := h.ToFloat(nil), f.lastH.ToFloat(nil) + fh := h.ToFloat(nil) if fh.DetectReset(prevFH) { return histogram.CounterReset } diff --git a/promql/histogram_stats_iterator_test.go b/promql/histogram_stats_iterator_test.go index 1460da60f9..abe3436a5a 100644 --- a/promql/histogram_stats_iterator_test.go +++ b/promql/histogram_stats_iterator_test.go @@ -156,7 +156,7 @@ func TestHistogramStatsMixedUse(t *testing.T) { expectedHints := []histogram.CounterResetHint{ histogram.UnknownCounterReset, histogram.NotCounterReset, - histogram.UnknownCounterReset, + histogram.CounterReset, } actualHints := make([]histogram.CounterResetHint, 3) typ := statsIterator.Next()