diff --git a/.chloggen/drosiek-fix-bodies-filtering.yaml b/.chloggen/drosiek-fix-bodies-filtering.yaml new file mode 100755 index 000000000000..5574f2988e92 --- /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: 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 + +# 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: Affects `filterprocessor` and `attributesprocessor`. 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) }) } }