fix(metricpass): Remove python logic compatibility (#13791)

This commit is contained in:
Joshua Powers 2023-08-28 14:47:29 -06:00 committed by GitHub
parent ca2295e1a4
commit 8dbc177d3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 14 deletions

View File

@ -1,6 +1,15 @@
<!-- markdownlint-disable MD024 -->
# 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

View File

@ -1,2 +1,2 @@
[[processors.processor]]
metricpass = '("state" in tags and tags.state == "on") or time > timestamp("2023-04-24T00:00:00Z")'
metricpass = '("state" in tags && tags.state == "on") || time > timestamp("2023-04-24T00:00:00Z")'

View File

@ -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

View File

@ -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,
},
}