Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filterprocessor: fix filtering non-string body by bodies property #22738

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .chloggen/drosiek-fix-bodies-filtering.yaml
Original file line number Diff line number Diff line change
@@ -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`.
4 changes: 1 addition & 3 deletions internal/filter/filterlog/filterlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()) {
Expand Down
28 changes: 28 additions & 0 deletions internal/filter/filterlog/filterlog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
})
}
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
})
}
}