feat: support simple selection of labels in json expr parser (#6151)

pull/6138/head
Aditya C S 3 years ago committed by GitHub
parent c6b7752bbb
commit c6af640798
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      docs/sources/logql/log_queries.md
  2. 1
      pkg/logql/syntax/expr.y
  3. 539
      pkg/logql/syntax/expr.y.go
  4. 12
      pkg/logql/syntax/parser_test.go

@ -364,6 +364,18 @@ The **json** parser operates in two modes:
"server_list" => `["129.0.1.1","10.2.1.3"]`
"headers" => `{"Accept": "*/*", "User-Agent": "curl/7.68.0"}`
```
If the label to be extracted is same as the original JSON field, expression can be written as just `| json <label>`
For example, to extract `servers` fields as label, expression can be written as following
`| json servers` will extract:
```kv
"servers" => `["129.0.1.1","10.2.1.3"]`
```
Note that `| json servers` is same as `| json servers="servers"`
#### logfmt

@ -301,6 +301,7 @@ labelFilter:
jsonExpression:
IDENTIFIER EQ STRING { $$ = log.NewJSONExpr($1, $3) }
| IDENTIFIER { $$ = log.NewJSONExpr($1, $1) }
jsonExpressionList:
jsonExpression { $$ = []log.JSONExpression{$1} }

File diff suppressed because it is too large Load Diff

@ -2943,6 +2943,18 @@ func TestParse(t *testing.T) {
},
},
},
{
in: `{app="foo"} | json response_code, api_key="request.headers[\"X-API-KEY\"]"`,
exp: &PipelineExpr{
Left: newMatcherExpr([]*labels.Matcher{{Type: labels.MatchEqual, Name: "app", Value: "foo"}}),
MultiStages: MultiStageExpr{
newJSONExpressionParser([]log.JSONExpression{
log.NewJSONExpr("response_code", `response_code`),
log.NewJSONExpr("api_key", `request.headers["X-API-KEY"]`),
}),
},
},
},
} {
t.Run(tc.in, func(t *testing.T) {
ast, err := ParseExpr(tc.in)

Loading…
Cancel
Save