Skip to content

Commit

Permalink
feat: improve syntax parser for pattern (#12489)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyriltovena committed Apr 11, 2024
1 parent 3e6b946 commit 48dae44
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
24 changes: 22 additions & 2 deletions pkg/logql/log/pattern/parser.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package pattern

import "fmt"
import (
"fmt"
"sync"
)

const underscore = "_"

Expand All @@ -10,6 +13,19 @@ var tokens = map[int]string{
UNDERSCORE: underscore,
}

type parser struct {
p *exprParserImpl
}

var parserPool = sync.Pool{
New: func() interface{} {
p := &parser{
p: &exprParserImpl{},
}
return p
},
}

func init() {
// Improve the error messages coming out of yacc.
exprErrorVerbose = true
Expand All @@ -23,9 +39,13 @@ func parseExpr(input string) (expr, error) {
}

func parseExprBytes(input []byte) (expr, error) {
p := parserPool.Get().(*parser)
defer parserPool.Put(p)

l := newLexer()
l.setData(input)
e := exprNewParser().Parse(l)

e := p.p.Parse(l)
if e != 0 || len(l.errs) > 0 {
return nil, l.errs[0]
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/logql/log/pattern/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,14 @@ func Test_Parse(t *testing.T) {
require.Equal(t, tc.expected, actual)
}
}

var result expr

func BenchmarkParseExpr(b *testing.B) {
var err error
b.ReportAllocs()
for i := 0; i < b.N; i++ {
result, err = parseExpr(`level=info <_> caller=main.go:107 msg="Starting Grafana Enterprise Traces" version="version=weekly-r138-f1920489, branch=weekly-r138, revision=f1920489"`)
}
require.NoError(b, err)
}

0 comments on commit 48dae44

Please sign in to comment.