From 0746f388b047e130e6864d145bb18c47483b3ad8 Mon Sep 17 00:00:00 2001 From: beorn7 Date: Thu, 4 Sep 2025 14:36:56 +0200 Subject: [PATCH] promql: Fix HistogramStatsIterator usage for subqueries Signed-off-by: beorn7 --- promql/engine.go | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/promql/engine.go b/promql/engine.go index 2feb6c3c92..91257eae37 100644 --- a/promql/engine.go +++ b/promql/engine.go @@ -3891,20 +3891,34 @@ func detectHistogramStatsDecoding(expr parser.Expr) { return nil } + pathLoop: for i := len(path) - 1; i >= 0; i-- { // Walk backwards up the path. + if _, ok := path[i].(*parser.SubqueryExpr); ok { + // If we ever see a subquery in the path, we + // will not skip the buckets. We need the + // buckets for correct counter reset detection. + n.SkipHistogramBuckets = false + break pathLoop + } call, ok := path[i].(*parser.Call) if !ok { - continue + continue pathLoop } switch call.Func.Name { case "histogram_count", "histogram_sum", "histogram_avg": + // We allow skipping buckets preliminarily. But + // we will continue through the path to see if + // we find a subquery (or a histogram function) + // further up (the latter wouldn't make sense, + // but no harm in detecting it). n.SkipHistogramBuckets = true case "histogram_quantile", "histogram_fraction": + // If we ever see a function that needs the + // whole histogram, we will not skip the + // buckets. n.SkipHistogramBuckets = false - default: - continue + break pathLoop } - break } return errors.New("stop") })