From 8dbc177d3fbb74f8207de646bcb1e045c920e2e8 Mon Sep 17 00:00:00 2001 From: Joshua Powers Date: Mon, 28 Aug 2023 14:47:29 -0600 Subject: [PATCH] fix(metricpass): Remove python logic compatibility (#13791) --- CHANGELOG.md | 9 +++++++++ config/testdata/filter_metricpass.toml | 2 +- models/filter.go | 6 ------ models/filter_test.go | 9 ++------- 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 652a30c10..578ba30ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,15 @@ # Changelog +## Next + +### BREAKING CHANGES + +- [#13791](https://github.com/influxdata/telegraf/pull/11493) `metricpass` +Removed the Python compatibility support for "not", "and", and "or" keywords. +This support was incorrectly removing these keywords from actual data. Users +should instead use the standard "!", "&&", and "||" operators. + ## v1.27.4 [2023-08-21] ### Bugfixes diff --git a/config/testdata/filter_metricpass.toml b/config/testdata/filter_metricpass.toml index 92d600d1d..a604c8d77 100644 --- a/config/testdata/filter_metricpass.toml +++ b/config/testdata/filter_metricpass.toml @@ -1,2 +1,2 @@ [[processors.processor]] - metricpass = '("state" in tags and tags.state == "on") or time > timestamp("2023-04-24T00:00:00Z")' \ No newline at end of file + metricpass = '("state" in tags && tags.state == "on") || time > timestamp("2023-04-24T00:00:00Z")' diff --git a/models/filter.go b/models/filter.go index d02778906..e595e5be4 100644 --- a/models/filter.go +++ b/models/filter.go @@ -3,7 +3,6 @@ package models import ( "errors" "fmt" - "regexp" "time" "github.com/google/cel-go/cel" @@ -257,11 +256,6 @@ func (f *Filter) compileMetricFilter() error { // Initialize the expression expression := f.MetricPass - // Replace python-like logic-operators - expression = regexp.MustCompile(`\bnot\b`).ReplaceAllString(expression, "!") - expression = regexp.MustCompile(`\band\b`).ReplaceAllString(expression, "&&") - expression = regexp.MustCompile(`\bor\b`).ReplaceAllString(expression, "||") - // Check if we need to call into CEL at all and quit early if expression == "" { return nil diff --git a/models/filter_test.go b/models/filter_test.go index f2adfd183..eab9ccf27 100644 --- a/models/filter_test.go +++ b/models/filter_test.go @@ -557,11 +557,6 @@ func TestFilter_MetricPass(t *testing.T) { expression: `(name.startsWith("t") || fields.on) && "id" in fields && fields.id.contains("nwr")`, expected: true, }, - { - name: "python-style logical expression", - expression: `(name.startsWith("t") or fields.on) and "id" in fields and fields.id.contains("nwr")`, - expected: true, - }, { name: "time arithmetics", expression: `time >= timestamp("2023-04-25T00:00:00Z") - duration("24h")`, @@ -569,12 +564,12 @@ func TestFilter_MetricPass(t *testing.T) { }, { name: "complex field filtering", - expression: `fields.exists(f, type(fields[f]) in [int, uint, double] and fields[f] > 20.0)`, + expression: `fields.exists(f, type(fields[f]) in [int, uint, double] && fields[f] > 20.0)`, expected: true, }, { name: "complex field filtering (exactly one)", - expression: `fields.exists_one(f, type(fields[f]) in [int, uint, double] and fields[f] > 20.0)`, + expression: `fields.exists_one(f, type(fields[f]) in [int, uint, double] && fields[f] > 20.0)`, expected: false, }, }