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

Use un-escaped regexp literal for string matching. #9252

Merged
merged 1 commit into from
Apr 24, 2023
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
Expand Up @@ -35,6 +35,7 @@
* [9130](https://github.com/grafana/loki/pull/9130) **salvacorts**: Pass LogQL engine options down to the _split by range_, _sharding_, and _query size limiter_ middlewares.
* [9156](https://github.com/grafana/loki/pull/9156) **ashwanthgoli**: Expiration: do not drop index if period is a zero value
* [9185](https://github.com/grafana/loki/pull/9185) **dannykopping**: Prevent redis client from incorrectly choosing cluster mode with local address.
* [9252](https://github.com/grafana/loki/pull/9252) **jeschkies**: Use un-escaped regex literal for string matching.

#### Promtail

Expand Down
6 changes: 3 additions & 3 deletions pkg/logql/syntax/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ func simplifyRegexMatcher(typ labels.MatchType, name, value string) *labels.Matc
}
reg = reg.Simplify()

m, ok := simplify(typ, name, value, reg)
m, ok := simplify(typ, name, reg)
if !ok {
util.AllNonGreedy(reg)
return labels.MustNewMatcher(typ, name, reg.String())
Expand All @@ -702,15 +702,15 @@ func simplifyRegexMatcher(typ labels.MatchType, name, value string) *labels.Matc
}

// simplify will return an equals matcher if there is a regex matching a literal
func simplify(typ labels.MatchType, name, value string, reg *syntax.Regexp) (*labels.Matcher, bool) {
func simplify(typ labels.MatchType, name string, reg *syntax.Regexp) (*labels.Matcher, bool) {
switch reg.Op {
case syntax.OpLiteral:
if !util.IsCaseInsensitive(reg) {
t := labels.MatchEqual
if typ == labels.MatchNotRegexp {
t = labels.MatchNotEqual
}
return labels.MustNewMatcher(t, name, value), true
return labels.MustNewMatcher(t, name, string(reg.Rune)), true
}
return nil, false
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/logql/syntax/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,20 @@ func Test_FilterMatcher(t *testing.T) {
},
[]linecheck{{"foo", true}, {"bar", false}, {"foobar", true}},
},
{
`{app="foo"} |~ "foo\\.bar\\.baz"`,
[]*labels.Matcher{
mustNewMatcher(labels.MatchEqual, "app", "foo"),
},
[]linecheck{{"foo", false}, {"bar", false}, {"foo.bar.baz", true}},
},
{
"{app=\"foo\"} | logfmt | field =~ `foo\\.bar`",
[]*labels.Matcher{
mustNewMatcher(labels.MatchEqual, "app", "foo"),
},
[]linecheck{{"field=foo", false}, {"field=bar", false}, {"field=foo.bar", true}},
},
{
`{app="foo"} | logfmt | duration > 1s and total_bytes < 1GB`,
[]*labels.Matcher{
Expand Down