From a060b5aee2ead41df02c5cf80d5126c68fc56dfc Mon Sep 17 00:00:00 2001 From: Ben Ye Date: Thu, 31 Oct 2024 00:30:22 -0700 Subject: [PATCH 1/2] fix round function ignoring enableDelayedNameRemoval feature flag Signed-off-by: Ben Ye --- promql/engine_test.go | 62 +++++++++++++++++++ promql/functions.go | 3 + .../testdata/name_label_dropping.test | 4 ++ 3 files changed, 69 insertions(+) diff --git a/promql/engine_test.go b/promql/engine_test.go index 7c398029f..eeb9f1f51 100644 --- a/promql/engine_test.go +++ b/promql/engine_test.go @@ -3928,3 +3928,65 @@ func (s mockSeries) Iterator(it chunkenc.Iterator) chunkenc.Iterator { } return storage.ChainSampleIteratorFromIterators(it, iterables) } + +func TestEvaluationWithDelayedNameRemovalDisabled(t *testing.T) { + opts := promql.EngineOpts{ + Logger: nil, + Reg: nil, + EnableAtModifier: true, + MaxSamples: 10000, + Timeout: 10 * time.Second, + EnableDelayedNameRemoval: false, + } + engine := promqltest.NewTestEngineWithOpts(t, opts) + + promqltest.RunTest(t, ` +load 5m + metric{env="1"} 0 60 120 + another_metric{env="1"} 60 120 180 + +# Does not drop __name__ for vector selector +eval instant at 15m metric{env="1"} + metric{env="1"} 120 + +# Drops __name__ for unary operators +eval instant at 15m -metric + {env="1"} -120 + +# Drops __name__ for binary operators +eval instant at 15m metric + another_metric + {env="1"} 300 + +# Does not drop __name__ for binary comparison operators +eval instant at 15m metric <= another_metric + metric{env="1"} 120 + +# Drops __name__ for binary comparison operators with "bool" modifier +eval instant at 15m metric <= bool another_metric + {env="1"} 1 + +# Drops __name__ for vector-scalar operations +eval instant at 15m metric * 2 + {env="1"} 240 + +# Drops __name__ for instant-vector functions +eval instant at 15m clamp(metric, 0, 100) + {env="1"} 100 + +# Drops __name__ for round function +eval instant at 15m round(metric) + {env="1"} 120 + +# Drops __name__ for range-vector functions +eval instant at 15m rate(metric{env="1"}[10m]) + {env="1"} 0.2 + +# Does not drop __name__ for last_over_time function +eval instant at 15m last_over_time(metric{env="1"}[10m]) + metric{env="1"} 120 + +# Drops name for other _over_time functions +eval instant at 15m max_over_time(metric{env="1"}[10m]) + {env="1"} 120 +`, engine) +} diff --git a/promql/functions.go b/promql/functions.go index e93a4cdc5..f9af4fbe0 100644 --- a/promql/functions.go +++ b/promql/functions.go @@ -538,6 +538,9 @@ func funcRound(vals []parser.Value, args parser.Expressions, enh *EvalNodeHelper continue } f := math.Floor(el.F*toNearestInverse+0.5) / toNearestInverse + if !enh.enableDelayedNameRemoval { + el.Metric = el.Metric.DropMetricName() + } enh.Out = append(enh.Out, Sample{ Metric: el.Metric, F: f, diff --git a/promql/promqltest/testdata/name_label_dropping.test b/promql/promqltest/testdata/name_label_dropping.test index 5f5dcd5e4..fb2576660 100644 --- a/promql/promqltest/testdata/name_label_dropping.test +++ b/promql/promqltest/testdata/name_label_dropping.test @@ -31,6 +31,10 @@ eval instant at 10m metric * 2 eval instant at 10m clamp(metric, 0, 100) {env="1"} 100 +# Drops __name__ for round function +eval instant at 15m round(metric) + {env="1"} 120 + # Drops __name__ for range-vector functions eval instant at 10m rate(metric{env="1"}[10m]) {env="1"} 0.2 From e2b46934c522c56fc94f93568b1e689183e20e1b Mon Sep 17 00:00:00 2001 From: Arve Knudsen Date: Fri, 15 Nov 2024 09:20:11 +0100 Subject: [PATCH 2/2] Fix tests Signed-off-by: Arve Knudsen --- promql/engine_test.go | 22 +++++++++---------- .../testdata/name_label_dropping.test | 2 +- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/promql/engine_test.go b/promql/engine_test.go index eeb9f1f51..6dbfde6d8 100644 --- a/promql/engine_test.go +++ b/promql/engine_test.go @@ -3946,47 +3946,47 @@ load 5m another_metric{env="1"} 60 120 180 # Does not drop __name__ for vector selector -eval instant at 15m metric{env="1"} +eval instant at 10m metric{env="1"} metric{env="1"} 120 # Drops __name__ for unary operators -eval instant at 15m -metric +eval instant at 10m -metric {env="1"} -120 # Drops __name__ for binary operators -eval instant at 15m metric + another_metric +eval instant at 10m metric + another_metric {env="1"} 300 # Does not drop __name__ for binary comparison operators -eval instant at 15m metric <= another_metric +eval instant at 10m metric <= another_metric metric{env="1"} 120 # Drops __name__ for binary comparison operators with "bool" modifier -eval instant at 15m metric <= bool another_metric +eval instant at 10m metric <= bool another_metric {env="1"} 1 # Drops __name__ for vector-scalar operations -eval instant at 15m metric * 2 +eval instant at 10m metric * 2 {env="1"} 240 # Drops __name__ for instant-vector functions -eval instant at 15m clamp(metric, 0, 100) +eval instant at 10m clamp(metric, 0, 100) {env="1"} 100 # Drops __name__ for round function -eval instant at 15m round(metric) +eval instant at 10m round(metric) {env="1"} 120 # Drops __name__ for range-vector functions -eval instant at 15m rate(metric{env="1"}[10m]) +eval instant at 10m rate(metric{env="1"}[10m]) {env="1"} 0.2 # Does not drop __name__ for last_over_time function -eval instant at 15m last_over_time(metric{env="1"}[10m]) +eval instant at 10m last_over_time(metric{env="1"}[10m]) metric{env="1"} 120 # Drops name for other _over_time functions -eval instant at 15m max_over_time(metric{env="1"}[10m]) +eval instant at 10m max_over_time(metric{env="1"}[10m]) {env="1"} 120 `, engine) } diff --git a/promql/promqltest/testdata/name_label_dropping.test b/promql/promqltest/testdata/name_label_dropping.test index fb2576660..d4a2ad257 100644 --- a/promql/promqltest/testdata/name_label_dropping.test +++ b/promql/promqltest/testdata/name_label_dropping.test @@ -32,7 +32,7 @@ eval instant at 10m clamp(metric, 0, 100) {env="1"} 100 # Drops __name__ for round function -eval instant at 15m round(metric) +eval instant at 10m round(metric) {env="1"} 120 # Drops __name__ for range-vector functions