From 606755be520d6561f8fa9ede70f5546a1b7009f7 Mon Sep 17 00:00:00 2001 From: Cyril Tovena Date: Wed, 17 Mar 2021 10:19:42 -0400 Subject: [PATCH] Fixes a bug where unpack would mutate log line. (#3502) * Fixes a bug where unpack would mutate log line. Signed-off-by: Cyril Tovena * add reset. Signed-off-by: Cyril Tovena (cherry picked from commit 6148794b9bcd18b67b26254703ce9f75fd0d070d) --- pkg/logql/log/parser.go | 3 +-- pkg/logql/log/parser_test.go | 3 +++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/pkg/logql/log/parser.go b/pkg/logql/log/parser.go index 64136f0e6276..cf23b01f9946 100644 --- a/pkg/logql/log/parser.go +++ b/pkg/logql/log/parser.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 } diff --git a/pkg/logql/log/parser_test.go b/pkg/logql/log/parser_test.go index c2055eb60fbc..6e57604c9b93 100644 --- a/pkg/logql/log/parser_test.go +++ b/pkg/logql/log/parser_test.go @@ -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") }) } }