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

Removed redundant memory allocations in parsers #5311

Merged
merged 1 commit into from
Feb 3, 2022
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
7 changes: 4 additions & 3 deletions pkg/logql/log/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ var (
_ Stage = &RegexpParser{}
_ Stage = &LogfmtParser{}

errMissingCapture = errors.New("at least one named capture must be supplied")
errUnexpectedJSONObject = fmt.Errorf("expecting json object(%d), but it is not", jsoniter.ObjectValue)
errMissingCapture = errors.New("at least one named capture must be supplied")
)

type JSONParser struct {
Expand Down Expand Up @@ -69,7 +70,7 @@ func (j *JSONParser) Process(line []byte, lbs *LabelsBuilder) ([]byte, bool) {
func (j *JSONParser) readObject(it *jsoniter.Iterator) error {
// we only care about object and values.
if nextType := it.WhatIsNext(); nextType != jsoniter.ObjectValue {
return fmt.Errorf("expecting json object(%d), got %d", jsoniter.ObjectValue, nextType)
return errUnexpectedJSONObject
}
_ = it.ReadMapCB(j.parseMap(""))
if it.Error != nil && it.Error != io.EOF {
Expand Down Expand Up @@ -438,7 +439,7 @@ func (u *UnpackParser) Process(line []byte, lbs *LabelsBuilder) ([]byte, bool) {
func (u *UnpackParser) unpack(it *jsoniter.Iterator, entry []byte, lbs *LabelsBuilder) ([]byte, error) {
// we only care about object and values.
if nextType := it.WhatIsNext(); nextType != jsoniter.ObjectValue {
return nil, fmt.Errorf("expecting json object(%d), got %d", jsoniter.ObjectValue, nextType)
return nil, errUnexpectedJSONObject
}
var isPacked bool
_ = it.ReadMapCB(func(iter *jsoniter.Iterator, field string) bool {
Expand Down
2 changes: 2 additions & 0 deletions pkg/logql/log/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,9 @@ func Benchmark_Parser(b *testing.B) {
LabelParseHints []string // hints to reduce label extractions.
}{
{"json", jsonLine, NewJSONParser(), []string{"response_latency_seconds"}},
{"jsonParser-not json line", nginxline, NewJSONParser(), []string{"response_latency_seconds"}},
{"unpack", packedLike, NewUnpackParser(), []string{"pod"}},
{"unpack-not json line", nginxline, NewUnpackParser(), []string{"pod"}},
{"logfmt", logfmtLine, NewLogfmtParser(), []string{"info", "throughput", "org_id"}},
{"regex greedy", nginxline, mustStage(NewRegexpParser(`GET (?P<path>.*?)/\?`)), []string{"path"}},
{"regex status digits", nginxline, mustStage(NewRegexpParser(`HTTP/1.1" (?P<statuscode>\d{3}) `)), []string{"statuscode"}},
Expand Down