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

JSON label extraction field validation expression overly strict #6372

Merged
merged 5 commits into from
Jun 13, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Main

* [6372](https://github.com/grafana/loki/pull/6372) **splitice**: Add support for numbers in JSON fields
* [6105](https://github.com/grafana/loki/pull/6105) **rutgerke** Export metrics for the promtail journal target
* [6099](https://github.com/grafana/loki/pull/6099/files) **cstyan**: Drop lines with malformed JSON in Promtail JSON pipeline stage
* [6136](https://github.com/grafana/loki/pull/6136) **periklis**: Add support for alertmanager header authorization
Expand Down
6 changes: 6 additions & 0 deletions pkg/logql/log/jsonexpr/jsonexpr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ func TestJSONExpressionParser(t *testing.T) {
nil,
fmt.Errorf("syntax error: unexpected $end, expecting RSB"),
},
{
"identifier with number",
`utf8`,
[]interface{}{"utf8"},
nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
15 changes: 7 additions & 8 deletions pkg/logql/log/jsonexpr/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (sc *Scanner) lex(lval *JSONExprSymType) int {
return RSB
case r == '.':
return DOT
case isIdentifier(r):
case isStartIdentifier(r):
sc.unread()
lval.field = sc.scanField()
return FIELD
Expand All @@ -83,21 +83,20 @@ func (sc *Scanner) lex(lval *JSONExprSymType) int {
}
}

func isIdentifier(r rune) bool {
func isStartIdentifier(r rune) bool {
return (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || r == '_'
}

func isIdentifier(r rune) bool {
return isStartIdentifier(r) || (r >= '0' && r <= '9')
}

func (sc *Scanner) scanField() string {
var str []rune

for {
r := sc.read()
if !isIdentifier(r) {
sc.unread()
break
}

if r == '.' || isEndOfInput(r) {
if !isIdentifier(r) || isEndOfInput(r) {
sc.unread()
break
}
Expand Down
20 changes: 19 additions & 1 deletion pkg/logql/syntax/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2954,17 +2954,35 @@ func TestParse(t *testing.T) {
},
},
{
in: `{app="foo"} | json response_code, api_key="request.headers[\"X-API-KEY\"]"`,
in: `{app="foo"} | json response_code, api_key="request.headers[\"X-API-KEY\"]", layer7_something_specific="layer7_something_specific"`,
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"]`),
log.NewJSONExpr("layer7_something_specific", `layer7_something_specific`),
}),
},
},
},
{
in: `count_over_time({ foo ="bar" } | json layer7_something_specific="layer7_something_specific" [12m])`,
exp: &RangeAggregationExpr{
Left: &LogRange{
Left: &PipelineExpr{
MultiStages: MultiStageExpr{
newJSONExpressionParser([]log.JSONExpression{
log.NewJSONExpr("layer7_something_specific", `layer7_something_specific`),
}),
},
Left: &MatchersExpr{Mts: []*labels.Matcher{mustNewMatcher(labels.MatchEqual, "foo", "bar")}},
},
Interval: 12 * time.Minute,
},
Operation: "count_over_time",
},
},
} {
t.Run(tc.in, func(t *testing.T) {
ast, err := ParseExpr(tc.in)
Expand Down