Fixes a bug where unpack would mutate log line. (#3502)

* 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>
pull/3505/head
Cyril Tovena 5 years ago committed by GitHub
parent b67a07e1fb
commit 6148794b9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      pkg/logql/log/parser.go
  2. 3
      pkg/logql/log/parser_test.go

@ -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
}

@ -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")
})
}
}

Loading…
Cancel
Save