Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(logql): updated JSONExpressionParser not to unescape extracted values if it is JSON object. (backport release-3.1.x) #14503

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -494,13 +494,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
Loading