Precompile log buffer regular expressions

Compile fixed patterns during monitor and log counter initialization, pass compiled expressions to the log buffer, and reject invalid log counter patterns before watching logs.
This commit is contained in:
Ciprian Hacman
2026-07-25 09:21:31 +03:00
parent 3b47d535b6
commit b02e2dcd09
6 changed files with 103 additions and 33 deletions
+16 -2
View File
@@ -61,6 +61,12 @@ func TestPush(t *testing.T) {
}
}
func TestCompilePatternRejectsInvalidExpression(t *testing.T) {
if _, err := CompilePattern(`foo\`); err == nil {
t.Fatal("expected invalid expression error")
}
}
func TestMatch(t *testing.T) {
max := 4
for c, test := range []struct {
@@ -97,7 +103,11 @@ func TestMatch(t *testing.T) {
b.Push(&types.Log{Message: log})
}
for i, expr := range test.exprs {
logs := b.Match(expr)
pattern, err := CompilePattern(expr)
if err != nil {
t.Fatalf("case %d.%d: failed to compile pattern %q: %v", c+1, i+1, expr, err)
}
logs := b.Match(pattern)
got := []string{}
for _, log := range logs {
got = append(got, log.Message)
@@ -117,8 +127,12 @@ func BenchmarkMatch(b *testing.B) {
// A pattern from the default kernel monitor configuration which does not
// match the buffered logs.
expr := `task [\S ]+:\w+ blocked for more than \w+ seconds\.`
pattern, err := CompilePattern(expr)
if err != nil {
b.Fatalf("failed to compile pattern %q: %v", expr, err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
buf.Match(expr)
buf.Match(pattern)
}
}