From 6f9e7ff7500ddcf395861f86e153eb593ad46bcf Mon Sep 17 00:00:00 2001 From: Julien Pivotto Date: Sat, 22 Aug 2020 21:04:03 +0200 Subject: [PATCH] Drop metric name in bool comparison between two instant vectors (#7819) * Drop metric name in bool comparison between two instant vectors Signed-off-by: Julien Pivotto --- docs/querying/operators.md | 6 ++++-- promql/engine.go | 3 +++ promql/testdata/operators.test | 17 +++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/docs/querying/operators.md b/docs/querying/operators.md index 7ce79c8b2..d998d7326 100644 --- a/docs/querying/operators.md +++ b/docs/querying/operators.md @@ -31,7 +31,7 @@ scalar that is the result of the operator applied to both scalar operands. **Between an instant vector and a scalar**, the operator is applied to the value of every data sample in the vector. E.g. if a time series instant vector is multiplied by 2, the result is another vector in which every sample value of -the original vector is multiplied by 2. +the original vector is multiplied by 2. The metric name is dropped. **Between two instant vectors**, a binary arithmetic operator is applied to each entry in the left-hand side vector and its [matching element](#vector-matching) @@ -64,7 +64,8 @@ operators result in another scalar that is either `0` (`false`) or `1` value of every data sample in the vector, and vector elements between which the comparison result is `false` get dropped from the result vector. If the `bool` modifier is provided, vector elements that would be dropped instead have the value -`0` and vector elements that would be kept have the value `1`. +`0` and vector elements that would be kept have the value `1`. The metric name +is dropped if the `bool` modifier is provided. **Between two instant vectors**, these operators behave as a filter by default, applied to matching entries. Vector elements for which the expression is not @@ -74,6 +75,7 @@ with the grouping labels becoming the output label set. If the `bool` modifier is provided, vector elements that would have been dropped instead have the value `0` and vector elements that would be kept have the value `1`, with the grouping labels again becoming the output label set. +The metric name is dropped if the `bool` modifier is provided. ### Logical/set binary operators diff --git a/promql/engine.go b/promql/engine.go index 5e5eb65b7..2e0e8629c 100644 --- a/promql/engine.go +++ b/promql/engine.go @@ -1656,6 +1656,9 @@ func (ev *evaluator) VectorBinop(op parser.ItemType, lhs, rhs Vector, matching * continue } metric := resultMetric(ls.Metric, rs.Metric, op, matching, enh) + if returnBool { + metric = enh.dropMetricName(metric) + } insertedSigs, exists := matchedSigs[sig] if matching.Card == parser.CardOneToOne { if exists { diff --git a/promql/testdata/operators.test b/promql/testdata/operators.test index 440729338..b160e3bbc 100644 --- a/promql/testdata/operators.test +++ b/promql/testdata/operators.test @@ -438,3 +438,20 @@ load 5m testmetric2{src="a",dst="b"} 1 eval_fail instant at 0m -{__name__=~'testmetric1|testmetric2'} + +clear + +load 5m + test_total{instance="localhost"} 50 + test_smaller{instance="localhost"} 10 + +eval instant at 5m test_total > bool test_smaller + {instance="localhost"} 1 + +eval instant at 5m test_total > test_smaller + test_total{instance="localhost"} 50 + +eval instant at 5m test_total < bool test_smaller + {instance="localhost"} 0 + +eval instant at 5m test_total < test_smaller