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

feat: improve syntax parser for pattern #12489

Merged
merged 1 commit into from
Apr 11, 2024
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
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)
}
Loading