From bc1c7f1809eb9d8cbe0dc52425c17e5572febdd8 Mon Sep 17 00:00:00 2001 From: Julius Volz Date: Thu, 11 Apr 2019 11:42:16 +0200 Subject: [PATCH] Fix scalar-vector comparisons (#5454) * Fix scalar-vector comparisons Fixes https://github.com/prometheus/prometheus/issues/5452 Signed-off-by: Julius Volz --- CHANGELOG.md | 5 +++-- promql/engine.go | 5 +++++ promql/testdata/operators.test | 6 +++--- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 24c19d2f3..ce80acc3c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ * [BUGFIX] Set TLSHandshakeTimeout in HTTP transport. common#179 * [BUGFIX] Use fsync to be more resilient to machine crashes. tsdb#573 tsdb#578 * [BUGFIX] Keep series that are still in WAL in checkpoints. tsdb#577 +* [BUGFIX] Fix output sample values for scalar-to-vector comparison operations. #5454 ## 2.8.1 / 2019-03-28 @@ -195,8 +196,8 @@ This release includes multiple bugfixes and features. Further, the WAL implement * [FEATURE] Persist alert 'for' state across restarts #4061 * [FEATURE] Add API providing per target metric metadata #4183 * [FEATURE] Add API providing recording and alerting rules #4318 #4501 -* [ENHANCEMENT] Brand new WAL implementation for TSDB. Forwards incompatible with previous WAL. -* [ENHANCEMENT] Show rule evaluation errors in UI #4457 +* [ENHANCEMENT] Brand new WAL implementation for TSDB. Forwards incompatible with previous WAL. +* [ENHANCEMENT] Show rule evaluation errors in UI #4457 * [ENHANCEMENT] Throttle resends of alerts to Alertmanager #4538 * [ENHANCEMENT] Send EndsAt along with the alert to Alertmanager #4550 * [ENHANCEMENT] Limit the samples returned by remote read endpoint #4532 diff --git a/promql/engine.go b/promql/engine.go index de615259b..046b97c21 100644 --- a/promql/engine.go +++ b/promql/engine.go @@ -1581,6 +1581,11 @@ func (ev *evaluator) VectorscalarBinop(op ItemType, lhs Vector, rhs Scalar, swap lv, rv = rv, lv } value, keep := vectorElemBinop(op, lv, rv) + // Catch cases where the scalar is the LHS in a scalar-vector comparison operation. + // We want to always keep the vector element value as the output value, even if it's on the RHS. + if op.isComparisonOperator() && swap { + value = rv + } if returnBool { if keep { value = 1.0 diff --git a/promql/testdata/operators.test b/promql/testdata/operators.test index e074197b3..29f8d8fa2 100644 --- a/promql/testdata/operators.test +++ b/promql/testdata/operators.test @@ -220,7 +220,7 @@ eval instant at 50m SUM(http_requests) BY (job) > 1000 {job="app-server"} 2600 eval instant at 50m 1000 < SUM(http_requests) BY (job) - {job="app-server"} 1000 + {job="app-server"} 2600 eval instant at 50m SUM(http_requests) BY (job) <= 1000 {job="api-server"} 1000 @@ -388,7 +388,7 @@ eval instant at 5m node_cpu % 2 {instance="def",job="node",mode="idle"} 0 {instance="def",job="node",mode="user"} 0 - + clear load 5m @@ -414,4 +414,4 @@ load 5m testmetric1{src="a",dst="b"} 0 testmetric2{src="a",dst="b"} 1 -eval_fail instant at 0m -{__name__=~'testmetric1|testmetric2'} \ No newline at end of file +eval_fail instant at 0m -{__name__=~'testmetric1|testmetric2'}