From 93ec767fed82fb947ef890d138016f2872e60ef8 Mon Sep 17 00:00:00 2001 From: Julien Pivotto <291750+roidelapluie@users.noreply.github.com> Date: Wed, 15 Apr 2026 16:25:58 +0200 Subject: [PATCH] promql: fix smoothed rate returning zero for data only after range For smoothed rate/increase, a result should only be returned when there is data available to interpolate across the range. If the range has a single data point only on one side, the result is meaningless and should be empty. The "data only before" case was already handled: if the last fetched sample is at or before rangeStart, extendedRate returns nothing. Add the symmetric guard for the "data only after" case: if the first fetched sample is strictly after rangeEnd, return nothing as well. This mirrors the behaviour described in prometheus/prometheus#18295, where a smoothed rate that has no data before the range should not return zero. Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com> --- promql/functions.go | 3 +++ .../promqltest/testdata/extended_vectors.test | 18 +++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/promql/functions.go b/promql/functions.go index 546f94df12..2ab7c275e2 100644 --- a/promql/functions.go +++ b/promql/functions.go @@ -142,6 +142,9 @@ func extendedRate(vals Matrix, args parser.Expressions, enh *EvalNodeHelper, isC if f[lastSampleIndex].T <= rangeStart { return enh.Out, annos } + if smoothed && f[firstSampleIndex].T > rangeEnd { + return enh.Out, annos + } left := pickOrInterpolateLeft(f, firstSampleIndex, rangeStart, smoothed, isCounter) right := pickOrInterpolateRight(f, lastSampleIndex, rangeEnd, smoothed, isCounter) diff --git a/promql/promqltest/testdata/extended_vectors.test b/promql/promqltest/testdata/extended_vectors.test index 0bc1140522..06f70913b7 100644 --- a/promql/promqltest/testdata/extended_vectors.test +++ b/promql/promqltest/testdata/extended_vectors.test @@ -417,4 +417,20 @@ eval instant at 45s withreset smoothed withreset 3 eval instant at 30s notregular smoothed - notregular 2 \ No newline at end of file + notregular 2 + +# Smoothed rate returns empty when data is only before or only after the range. +clear +load 10s + metric _ 5+1x4 + +eval instant at 5s rate(metric[5s] smoothed) + +eval instant at 5s increase(metric[5s] smoothed) + +eval instant at 15s increase(metric[10s] smoothed) + {} 0.5 + +eval instant at 60s rate(metric[5s] smoothed) + +eval instant at 60s increase(metric[5s] smoothed)