From 89760dd77d93fd6db5a3d685e5e2923c395dcf9f Mon Sep 17 00:00:00 2001 From: Brian Brazil Date: Wed, 6 Jan 2016 12:41:40 +0000 Subject: [PATCH] Handle NaN for min/max. Similar to topk and sort, prefer not returning NaN where possible. --- promql/engine.go | 4 ++-- promql/testdata/operators.test | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 promql/testdata/operators.test diff --git a/promql/engine.go b/promql/engine.go index 96aa0eb0a..c1cceb048 100644 --- a/promql/engine.go +++ b/promql/engine.go @@ -1084,11 +1084,11 @@ func (ev *evaluator) aggregation(op itemType, grouping model.LabelNames, keepExt groupedResult.value += sample.Value groupedResult.groupCount++ case itemMax: - if groupedResult.value < sample.Value { + if groupedResult.value < sample.Value || math.IsNaN(float64(groupedResult.value)) { groupedResult.value = sample.Value } case itemMin: - if groupedResult.value > sample.Value { + if groupedResult.value > sample.Value || math.IsNaN(float64(groupedResult.value)) { groupedResult.value = sample.Value } case itemCount: diff --git a/promql/testdata/operators.test b/promql/testdata/operators.test new file mode 100644 index 000000000..fbef5030c --- /dev/null +++ b/promql/testdata/operators.test @@ -0,0 +1,22 @@ +# Tests for min/max. +clear +load 5m + http_requests{job="api-server", instance="0", group="production"} 1 + http_requests{job="api-server", instance="1", group="production"} 2 + http_requests{job="api-server", instance="0", group="canary"} NaN + http_requests{job="api-server", instance="1", group="canary"} 3 + http_requests{job="api-server", instance="2", group="canary"} 4 + +eval instant at 0m max(http_requests) + {} 4 + +eval instant at 0m min(http_requests) + {} 1 + +eval instant at 0m max by (group) (http_requests) + {group="production"} 2 + {group="canary"} 4 + +eval instant at 0m min by (group) (http_requests) + {group="production"} 1 + {group="canary"} 3