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(traceql): fix extract matcher regexes to work with regexp-type matchers #3641

Merged
merged 3 commits into from
May 8, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## main / unreleased

* [BUGFIX] Fix handling of regex matchers in autocomplete endpoints [#3641](https://github.com/grafana/tempo/pull/3641) (@sd2k)
* [ENHANCEMENT] Surface new labels for uninstrumented services and systems [#3543](https://github.com/grafana/tempo/pull/3543) (@t00mas)
* [FEATURE] Add TLS support for Memcached Client [#3585](https://github.com/grafana/tempo/pull/3585) (@sonisr)
* [FEATURE] TraceQL metrics queries: add quantile_over_time [#3605](https://github.com/grafana/tempo/pull/3605) [#3633](https://github.com/grafana/tempo/pull/3633) (@mdisibio)
Expand Down
5 changes: 3 additions & 2 deletions pkg/traceql/extractmatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
// 3. The boolean values "true" or "false".
//
// Example: "http.status_code = 200" from the query "{ .http.status_code = 200 && .http.method = }"
var matchersRegexp = regexp.MustCompile(`[\p{L}\p{N}._\-" ]+\s*[=|<=|>=|=~|!=|>|<|!~]\s*(?:"[\p{L}\p{N}\p{P}\p{M}\p{S}]+"|true|false|[a-z]+|[0-9smh]+)`)
var matchersRegexp = regexp.MustCompile(`[\p{L}\p{N}._\-" ]+\s*(=|<=|>=|=~|!=|>|<|!~)\s*(?:"[\p{L}\p{N}\p{P}\p{M}\p{S}]+"|true|false|[a-z]+|[0-9smh]+)`)

// TODO: Merge into a single regular expression

Expand All @@ -29,11 +29,12 @@ var matchersRegexp = regexp.MustCompile(`[\p{L}\p{N}._\-" ]+\s*[=|<=|>=|=~|!=|>|
// Query | Match
//
// { .bar = "foo" } | Yes
// { .bar =~ "foo|bar" } | Yes
// { .bar = "foo" && .foo = "bar" } | Yes
// { .bar = "foo" || .foo = "bar" } | No
// { .bar = "foo" } && { .foo = "bar" } | No
// { .bar = "foo" } || { .foo = "bar" } | No
var singleFilterRegexp = regexp.MustCompile(`^\{[^|{}]*[^|{}]}?$`)
var singleFilterRegexp = regexp.MustCompile(`^(\{[^|{}]*[^|{}]}?|\{[^|{}]*=~[^{}]*})$`)

const emptyQuery = "{}"

Expand Down
20 changes: 20 additions & 0 deletions pkg/traceql/extractmatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,26 @@ func TestExtractMatchers(t *testing.T) {
query: `{ span."foo bar" = "baz" }`,
expected: `{span."foo bar" = "baz"}`,
},
{
name: "query with trivial regex matcher",
query: `{ .foo =~ "a" }`,
expected: `{.foo =~ "a"}`,
},
{
name: "query with regex matcher",
query: `{ .foo =~ "(a|b)" }`,
expected: `{.foo =~ "(a|b)"}`,
},
{
name: "query with multiple regex matchers",
query: `{ .foo =~ "(a|b)" && .bar =~ "(c|d)" }`,
expected: `{.foo =~ "(a|b)" && .bar =~ "(c|d)"}`,
},
{
name: "query with mixed equal and regex matchers",
query: `{ .foo = "a" && .bar =~ "(c|d)" }`,
expected: `{.foo = "a" && .bar =~ "(c|d)"}`,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
Expand Down
Loading