Skip to content

Commit

Permalink
Fixes a bug where unpack would mutate log line. (#3502)
Browse files Browse the repository at this point in the history
* Fixes a bug where unpack would mutate log line.

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>

* add reset.

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>
(cherry picked from commit 6148794)
  • Loading branch information
cyriltovena authored and slim-bean committed Apr 6, 2021
1 parent 3b30483 commit 606755b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
3 changes: 1 addition & 2 deletions pkg/logql/log/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,11 +358,10 @@ func (u *UnpackParser) unpack(it *jsoniter.Iterator, entry []byte, lbs *LabelsBu
case jsoniter.StringValue:
// we only unpack map[string]string. Anything else is skipped.
if field == PackedEntryKey {
s := iter.ReadString()
// todo(ctovena): we should just reslice the original line since the property is contiguous
// but jsoniter doesn't allow us to do this right now.
// https://github.com/buger/jsonparser might do a better job at this.
entry = append(entry[:0], []byte(s)...)
entry = []byte(iter.ReadString())
isPacked = true
return true
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/logql/log/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -721,10 +721,13 @@ func Test_unpackParser_Parse(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
b := NewBaseLabelsBuilder().ForLabels(tt.lbs, tt.lbs.Hash())
b.Reset()
copy := string(tt.line)
l, _ := j.Process(tt.line, b)
sort.Sort(tt.wantLbs)
require.Equal(t, tt.wantLbs, b.Labels())
require.Equal(t, tt.wantLine, l)
require.Equal(t, string(tt.wantLine), string(l))
require.Equal(t, copy, string(tt.line), "the original log line should not be mutated")
})
}
}

0 comments on commit 606755b

Please sign in to comment.