diff --git a/pkg/logql/ast.go b/pkg/logql/ast.go index b619f2738b97..e5d55eb0d7f5 100644 --- a/pkg/logql/ast.go +++ b/pkg/logql/ast.go @@ -138,13 +138,15 @@ func (e *filterExpr) Filter() (Filter, error) { } case labels.MatchEqual: + mb := []byte(e.match) f = func(line []byte) bool { - return bytes.Contains(line, []byte(e.match)) + return bytes.Contains(line, mb) } case labels.MatchNotEqual: + mb := []byte(e.match) f = func(line []byte) bool { - return !bytes.Contains(line, []byte(e.match)) + return !bytes.Contains(line, mb) } default: diff --git a/pkg/logql/ast_test.go b/pkg/logql/ast_test.go index db321b300db5..f26e6d77cbb7 100644 --- a/pkg/logql/ast_test.go +++ b/pkg/logql/ast_test.go @@ -111,3 +111,25 @@ func Test_FilterMatcher(t *testing.T) { }) } } + +func BenchmarkContainsFilter(b *testing.B) { + expr, err := ParseLogSelector(`{app="foo"} |= "foo"`) + if err != nil { + b.Fatal(err) + } + + f, err := expr.Filter() + if err != nil { + b.Fatal(err) + } + + line := []byte("hello world foo bar") + + b.ResetTimer() + + for i := 0; i < b.N; i++ { + if !f(line) { + b.Fatal("doesn't match") + } + } +}