Skip to content

Commit

Permalink
fix(logql): updated JSONExpressionParser not to unescape extracted va…
Browse files Browse the repository at this point in the history
…lues if it is JSON object. (backport release-3.2.x) (#14502)

Co-authored-by: Vladyslav Diachenko <82767850+vlad-diachenko@users.noreply.github.com>
  • Loading branch information
loki-gh-app[bot] and vlad-diachenko authored Oct 16, 2024
1 parent 711fc20 commit e9bbaf3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
2 changes: 2 additions & 0 deletions pkg/logql/log/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,8 @@ func (j *JSONExpressionParser) Process(_ int64, line []byte, lbs *LabelsBuilder)
switch typ {
case jsonparser.Null:
lbs.Set(ParsedLabel, key, "")
case jsonparser.Object:
lbs.Set(ParsedLabel, key, string(data))
default:
lbs.Set(ParsedLabel, key, unescapeJSONString(data))
}
Expand Down
30 changes: 26 additions & 4 deletions pkg/logql/log/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,13 +522,35 @@ func TestJSONExpressionParser(t *testing.T) {
),
NoParserHints(),
},
{
"nested object with escaped value",
[]byte(`{"app":{"name":"great \"loki\""}`),
[]LabelExtractionExpr{
NewLabelExtractionExpr("app", `app`),
},
labels.FromStrings("foo", "bar"),
labels.FromStrings("foo", "bar",
"app", `{"name":"great \"loki\""}`,
),
NoParserHints(),
},
{
"field with escaped value inside the json string",
[]byte(`{"app":"{\"name\":\"great \\\"loki\\\"\"}"}`),
[]LabelExtractionExpr{
NewLabelExtractionExpr("app", `app`),
},
labels.FromStrings("foo", "bar"),
labels.FromStrings("foo", "bar",
"app", `{"name":"great \"loki\""}`,
),
NoParserHints(),
},
}
for _, tt := range tests {
j, err := NewJSONExpressionParser(tt.expressions)
if err != nil {
t.Fatalf("cannot create JSON expression parser: %s", err.Error())
}
t.Run(tt.name, func(t *testing.T) {
j, err := NewJSONExpressionParser(tt.expressions)
require.NoError(t, err, "cannot create JSON expression parser")
b := NewBaseLabelsBuilderWithGrouping(nil, tt.hints, false, false).ForLabels(tt.lbs, tt.lbs.Hash())
b.Reset()
_, _ = j.Process(0, tt.line, b)
Expand Down

0 comments on commit e9bbaf3

Please sign in to comment.