From c88d0b0e0a4a1c4281e04bc68c86bd37a950712f Mon Sep 17 00:00:00 2001 From: Charles Korn Date: Fri, 14 Mar 2025 01:06:17 +1100 Subject: [PATCH] promql: return NaN from `irate()` if second-last sample is NaN (#16199) promql: return NaN from `irate()` if either of last two samples is NaN Signed-off-by: Charles Korn --------- Signed-off-by: Charles Korn --- promql/functions.go | 5 +++-- promql/promqltest/testdata/functions.test | 8 ++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/promql/functions.go b/promql/functions.go index 3f977af56a..3c79684b0f 100644 --- a/promql/functions.go +++ b/promql/functions.go @@ -367,10 +367,11 @@ func instantValue(vals []parser.Value, args parser.Expressions, out Vector, isRa } switch { case ss[1].H == nil && ss[0].H == nil: - if !isRate || ss[1].F >= ss[0].F { - // Gauge or counter without reset. + if !isRate || !(ss[1].F < ss[0].F) { + // Gauge, or counter without reset, or counter with NaN value. resultSample.F = ss[1].F - ss[0].F } + // In case of a counter reset, we leave resultSample at // its current value, which is already ss[1]. case ss[1].H != nil && ss[0].H != nil: diff --git a/promql/promqltest/testdata/functions.test b/promql/promqltest/testdata/functions.test index 04d844bef9..fafe2dda40 100644 --- a/promql/promqltest/testdata/functions.test +++ b/promql/promqltest/testdata/functions.test @@ -218,6 +218,7 @@ clear load 5m http_requests_total{path="/foo"} 0+10x10 http_requests_total{path="/bar"} 0+10x5 0+10x5 + http_requests_nan{} 1 NaN NaN 5 11 http_requests_histogram{path="/a"} {{sum:2 count:2}}+{{sum:3 count:3}}x5 http_requests_histogram{path="/b"} 0 0 {{sum:1 count:1}} {{sum:4 count:4}} http_requests_histogram{path="/c"} 0 0 {{sum:1 count:1}} {{sum:4 count:4 counter_reset_hint:gauge}} @@ -235,6 +236,9 @@ eval instant at 30m irate(http_requests_total[50m]) {path="/foo"} .03333333333333333333 {path="/bar"} 0 +eval range from 0 to 20m step 5m irate(http_requests_nan[15m1s]) + {} _ NaN NaN NaN 0.02 + eval instant at 20m irate(http_requests_histogram{path="/a"}[20m]) {path="/a"} {{sum:0.01 count:0.01 counter_reset_hint:gauge}} @@ -288,6 +292,7 @@ clear load 5m http_requests{path="/foo"} 0 50 100 150 http_requests{path="/bar"} 0 50 100 50 + http_requests_nan{} 1 NaN NaN 5 11 http_requests_histogram{path="/a"} {{sum:2 count:2 counter_reset_hint:gauge}}+{{sum:1 count:3 counter_reset_hint:gauge}}x5 http_requests_histogram{path="/b"} 0 0 {{sum:1 count:1 counter_reset_hint:gauge}} {{sum:2 count:2 counter_reset_hint:gauge}} http_requests_histogram{path="/c"} 0 0 {{sum:1 count:1}} {{sum:2 count:2 counter_reset_hint:gauge}} @@ -300,6 +305,9 @@ eval instant at 20m idelta(http_requests[20m]) {path="/foo"} 50 {path="/bar"} -50 +eval range from 0 to 20m step 5m idelta(http_requests_nan[15m1s]) + {} _ NaN NaN NaN 6 + eval instant at 20m idelta(http_requests_histogram{path="/a"}[20m]) {path="/a"} {{sum:1 count:3 counter_reset_hint:gauge}}