fix(logql): updated JSONExpressionParser not to unescape extracted values if it is JSON object. (#14499)

Signed-off-by: Vladyslav Diachenko <vlad.diachenko@grafana.com>
pull/14505/head
Vladyslav Diachenko 7 months ago committed by GitHub
parent 5d57a035e3
commit 08b1a9080b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 2
      pkg/logql/log/parser.go
  2. 30
      pkg/logql/log/parser_test.go

@ -625,6 +625,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))
}

@ -542,13 +542,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)

Loading…
Cancel
Save