From 70830d07c839f2c614452fd799f4470ebf7006e7 Mon Sep 17 00:00:00 2001 From: Dominik Rosiek Date: Wed, 24 May 2023 15:30:27 +0930 Subject: [PATCH 1/2] filterprocessor: fix filtering non-string body by bodies property Signed-off-by: Dominik Rosiek --- .chloggen/drosiek-fix-bodies-filtering.yaml | 20 +++++++++++++++ internal/filter/filterlog/filterlog.go | 4 +-- internal/filter/filterlog/filterlog_test.go | 28 +++++++++++++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) create mode 100755 .chloggen/drosiek-fix-bodies-filtering.yaml diff --git a/.chloggen/drosiek-fix-bodies-filtering.yaml b/.chloggen/drosiek-fix-bodies-filtering.yaml new file mode 100755 index 000000000000..2a0a8400be09 --- /dev/null +++ b/.chloggen/drosiek-fix-bodies-filtering.yaml @@ -0,0 +1,20 @@ +# Use this changelog template to create an entry for release notes. +# If your change doesn't affect end users, such as a test fix or a tooling change, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. + +# 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: filterprocessor + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: fix filtering non-string body by bodies property + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [22736] + +# (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: diff --git a/internal/filter/filterlog/filterlog.go b/internal/filter/filterlog/filterlog.go index 4ef257d49179..42e5242a9cb4 100644 --- a/internal/filter/filterlog/filterlog.go +++ b/internal/filter/filterlog/filterlog.go @@ -7,8 +7,6 @@ import ( "context" "fmt" - "go.opentelemetry.io/collector/pdata/pcommon" - "github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/expr" "github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterconfig" "github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filtermatcher" @@ -105,7 +103,7 @@ func newExpr(mp *filterconfig.MatchProperties) (expr.BoolExpr[ottllog.TransformC // evaluate to true for a match to occur. func (mp *propertiesMatcher) Eval(_ context.Context, tCtx ottllog.TransformContext) (bool, error) { lr := tCtx.GetLogRecord() - if lr.Body().Type() == pcommon.ValueTypeStr && mp.bodyFilters != nil && !mp.bodyFilters.Matches(lr.Body().Str()) { + if mp.bodyFilters != nil && !mp.bodyFilters.Matches(lr.Body().AsString()) { return false, nil } if mp.severityTextFilters != nil && !mp.severityTextFilters.Matches(lr.SeverityText()) { diff --git a/internal/filter/filterlog/filterlog_test.go b/internal/filter/filterlog/filterlog_test.go index fff862220971..931b0653cbd7 100644 --- a/internal/filter/filterlog/filterlog_test.go +++ b/internal/filter/filterlog/filterlog_test.go @@ -124,10 +124,22 @@ func TestLogRecord_Matching_False(t *testing.T) { }, }, }, + { + name: "log_body_doesnt_match", + properties: &filterconfig.MatchProperties{ + Config: *createConfig(filterset.Regexp), + LogBodies: []string{".*TEST.*"}, + }, + }, } lr := plog.NewLogRecord() lr.SetSeverityNumber(plog.SeverityNumberTrace) + lr.Body().SetStr("AUTHENTICATION FAILED") + + lrm := plog.NewLogRecord() + lrm.Body().SetEmptyMap() + lrm.Body().Map().PutStr("message", "AUTHENTICATION FAILED") for _, tc := range testcases { t.Run(tc.name, func(t *testing.T) { @@ -138,6 +150,10 @@ func TestLogRecord_Matching_False(t *testing.T) { val, err := expr.Eval(context.Background(), ottllog.NewTransformContext(lr, pcommon.NewInstrumentationScope(), pcommon.NewResource())) require.NoError(t, err) assert.False(t, val) + + val, err = expr.Eval(context.Background(), ottllog.NewTransformContext(lrm, pcommon.NewInstrumentationScope(), pcommon.NewResource())) + require.NoError(t, err) + assert.False(t, val) }) } } @@ -194,6 +210,13 @@ func TestLogRecord_Matching_True(t *testing.T) { lr.SetSeverityText("debug") lr.SetSeverityNumber(plog.SeverityNumberDebug) + lrm := plog.NewLogRecord() + lrm.Attributes().PutStr("abc", "def") + lrm.Body().SetEmptyMap() + lrm.Body().Map().PutStr("message", "AUTHENTICATION FAILED") + lrm.SetSeverityText("debug") + lrm.SetSeverityNumber(plog.SeverityNumberDebug) + for _, tc := range testcases { t.Run(tc.name, func(t *testing.T) { expr, err := newExpr(tc.properties) @@ -204,6 +227,11 @@ func TestLogRecord_Matching_True(t *testing.T) { val, err := expr.Eval(context.Background(), ottllog.NewTransformContext(lr, pcommon.NewInstrumentationScope(), pcommon.NewResource())) require.NoError(t, err) assert.True(t, val) + + assert.NotNil(t, lrm) + val, err = expr.Eval(context.Background(), ottllog.NewTransformContext(lrm, pcommon.NewInstrumentationScope(), pcommon.NewResource())) + require.NoError(t, err) + assert.True(t, val) }) } } From 67513c32952001fab39b9336a05612a26c33287a Mon Sep 17 00:00:00 2001 From: Dominik Rosiek <58699848+sumo-drosiek@users.noreply.github.com> Date: Fri, 26 May 2023 08:11:13 +0200 Subject: [PATCH 2/2] chore: update changelog due to review Co-authored-by: Tyler Helmuth <12352919+TylerHelmuth@users.noreply.github.com> --- .chloggen/drosiek-fix-bodies-filtering.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.chloggen/drosiek-fix-bodies-filtering.yaml b/.chloggen/drosiek-fix-bodies-filtering.yaml index 2a0a8400be09..5574f2988e92 100755 --- a/.chloggen/drosiek-fix-bodies-filtering.yaml +++ b/.chloggen/drosiek-fix-bodies-filtering.yaml @@ -6,7 +6,7 @@ change_type: bug_fix # The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) -component: filterprocessor +component: internal/filter/filterlog # A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). note: fix filtering non-string body by bodies property @@ -17,4 +17,4 @@ issues: [22736] # (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: +subtext: Affects `filterprocessor` and `attributesprocessor`.