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. (#14499)

Signed-off-by: Vladyslav Diachenko <[email protected]>
(cherry picked from commit 08b1a90)
  • Loading branch information
vlad-diachenko authored and grafana-delivery-bot[bot] committed Oct 16, 2024
1 parent 711fc20 commit 2f1e1f7
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 2f1e1f7

Please sign in to comment.