From bd788583ba00a4ba5e34f4b8f9943cc94263509e Mon Sep 17 00:00:00 2001 From: Daniel Jaglowski Date: Tue, 5 Sep 2023 10:43:09 -0600 Subject: [PATCH] [pkg/stanza] Fix issue where add operator did not correctly compile expressions (#26467) Alternate to #26374 Resolves #26373 --- .chloggen/pkg-stanza-fix-add.yaml | 27 ++++++++++++++ pkg/stanza/operator/helper/expr_string.go | 4 ++ pkg/stanza/operator/transformer/add/add.go | 3 +- .../operator/transformer/add/add_test.go | 37 +++++++++++++++++++ 4 files changed, 69 insertions(+), 2 deletions(-) create mode 100755 .chloggen/pkg-stanza-fix-add.yaml diff --git a/.chloggen/pkg-stanza-fix-add.yaml b/.chloggen/pkg-stanza-fix-add.yaml new file mode 100755 index 000000000000..a53e5d9a258c --- /dev/null +++ b/.chloggen/pkg-stanza-fix-add.yaml @@ -0,0 +1,27 @@ + # Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: bug_fix + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: filelogreceiver + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Fix the behavior of the add operator to continue to support EXPR(env("MY_ENV_VAR")) expressions + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [26373] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] diff --git a/pkg/stanza/operator/helper/expr_string.go b/pkg/stanza/operator/helper/expr_string.go index a753bc459ab7..3a8abadb49c3 100644 --- a/pkg/stanza/operator/helper/expr_string.go +++ b/pkg/stanza/operator/helper/expr_string.go @@ -91,6 +91,10 @@ func (e ExprStringConfig) Build() (*ExprString, error) { }, nil } +func ExprCompile(input string) (*vm.Program, error) { + return expr.Compile(input, expr.AllowUndefinedVariables(), expr.Patch(&patcher{})) +} + func ExprCompileBool(input string) (*vm.Program, error) { return expr.Compile(input, expr.AllowUndefinedVariables(), expr.Patch(&patcher{}), expr.AsBool()) } diff --git a/pkg/stanza/operator/transformer/add/add.go b/pkg/stanza/operator/transformer/add/add.go index 4525d7e55108..ab6bba81873a 100644 --- a/pkg/stanza/operator/transformer/add/add.go +++ b/pkg/stanza/operator/transformer/add/add.go @@ -8,7 +8,6 @@ import ( "fmt" "strings" - "github.com/antonmedv/expr" "github.com/antonmedv/expr/vm" "go.uber.org/zap" @@ -61,7 +60,7 @@ func (c Config) Build(logger *zap.SugaredLogger) (operator.Operator, error) { exprStr := strings.TrimPrefix(strVal, "EXPR(") exprStr = strings.TrimSuffix(exprStr, ")") - compiled, err := expr.Compile(exprStr, expr.AllowUndefinedVariables()) + compiled, err := helper.ExprCompile(exprStr) if err != nil { return nil, fmt.Errorf("failed to compile expression '%s': %w", c.IfExpr, err) } diff --git a/pkg/stanza/operator/transformer/add/add_test.go b/pkg/stanza/operator/transformer/add/add_test.go index d75421493c8a..c30ec16ed698 100644 --- a/pkg/stanza/operator/transformer/add/add_test.go +++ b/pkg/stanza/operator/transformer/add/add_test.go @@ -23,6 +23,7 @@ type testCase struct { } func TestProcessAndBuild(t *testing.T) { + t.Setenv("TEST_EXPR_STRING_ENV", "val") now := time.Now() newTestEntry := func() *entry.Entry { e := entry.New() @@ -297,6 +298,42 @@ func TestProcessAndBuild(t *testing.T) { }, false, }, + { + "add_expr", + func() *Config { + cfg := NewConfig() + cfg.Field = entry.NewAttributeField("fookey") + cfg.Value = "EXPR('foo_' + body.key)" + return cfg + }(), + newTestEntry, + func() *entry.Entry { + e := newTestEntry() + e.Attributes = map[string]interface{}{ + "fookey": "foo_val", + } + return e + }, + false, + }, + { + "add_expr_env", + func() *Config { + cfg := NewConfig() + cfg.Field = entry.NewAttributeField("fookey") + cfg.Value = "EXPR('foo_' + env('TEST_EXPR_STRING_ENV'))" + return cfg + }(), + newTestEntry, + func() *entry.Entry { + e := newTestEntry() + e.Attributes = map[string]interface{}{ + "fookey": "foo_val", + } + return e + }, + false, + }, } for _, tc := range cases { t.Run("BuildandProcess/"+tc.name, func(t *testing.T) {