From 766bdea448b1b70500c0eb2aee2eb7aae43be65b Mon Sep 17 00:00:00 2001 From: Ruben Vargas Date: Wed, 29 Nov 2023 18:43:58 -0600 Subject: [PATCH] Move some tests to traceql Signed-off-by: Ruben Vargas --- modules/ingester/instance_search_test.go | 114 ---------------------- pkg/traceql/extractmatcher_test.go | 118 +++++++++++++++++++++++ 2 files changed, 118 insertions(+), 114 deletions(-) create mode 100644 pkg/traceql/extractmatcher_test.go diff --git a/modules/ingester/instance_search_test.go b/modules/ingester/instance_search_test.go index 7c7227bf1c9..68b210ee847 100644 --- a/modules/ingester/instance_search_test.go +++ b/modules/ingester/instance_search_test.go @@ -11,8 +11,6 @@ import ( "testing" "time" - "github.com/grafana/tempo/pkg/traceql" - "github.com/google/uuid" "github.com/grafana/dskit/user" "github.com/stretchr/testify/assert" @@ -866,118 +864,6 @@ func BenchmarkInstanceSearchUnderLoad(b *testing.B) { time.Sleep(1 * time.Second) } -func TestExtractMatchers(t *testing.T) { - testCases := []struct { - name, query, expected string - }{ - { - name: "empty query", - query: "", - expected: "{}", - }, - { - name: "empty query with spaces", - query: " { } ", - expected: "{}", - }, - { - name: "simple query", - query: `{.service_name = "foo"}`, - expected: `{.service_name = "foo"}`, - }, - { - name: "incomplete query", - query: `{ .http.status_code = 200 && .http.method = }`, - expected: "{.http.status_code = 200}", - }, - { - name: "invalid query", - query: "{ 2 = .b ", - expected: "{}", - }, - { - name: "long query", - query: `{.service_name = "foo" && .http.status_code = 200 && .http.method = "GET" && .cluster = }`, - expected: `{.service_name = "foo" && .http.status_code = 200 && .http.method = "GET"}`, - }, - { - name: "query with duration a boolean", - query: `{ duration > 5s && .success = true && .cluster = }`, - expected: `{duration > 5s && .success = true}`, - }, - { - name: "query with three selectors with AND", - query: `{ .foo = "bar" && .baz = "qux" } && { duration > 1s } || { .foo = "bar" && .baz = "qux" }`, - expected: "{}", - }, - { - name: "query with OR conditions", - query: `{ (.foo = "bar" || .baz = "qux") && duration > 1s }`, - expected: "{}", - }, - { - name: "query with multiple selectors and pipelines", - query: `{ .foo = "bar" && .baz = "qux" } && { duration > 1s } || { .foo = "bar" && .baz = "qux" } | count() > 4`, - expected: "{}", - }, - { - name: "query with slash in value", - query: `{ span.http.target = "/api/v1/users" }`, - expected: `{span.http.target = "/api/v1/users"}`, - }, - { - name: "intrinsics", - query: `{ name = "foo" }`, - expected: `{name = "foo"}`, - }, - { - name: "incomplete intrinsics", - query: `{ statusMessage = }`, - expected: "{}", - }, - { - name: "query with missing closing bracket", - query: `{resource.service_name = "foo" && span.http.target=`, - expected: `{resource.service_name = "foo"}`, - }, - { - name: "uncommon characters", - query: `{ span.foo = "<>:b5[]" && resource.service.name = }`, - expected: `{span.foo = "<>:b5[]"}`, - }, - { - name: "kind", - query: `{ kind = server }`, - expected: `{kind = server}`, - }, - } - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - assert.Equal(t, tc.expected, traceql.ExtractMatchers(tc.query)) - }) - } -} - -func BenchmarkExtractMatchers(b *testing.B) { - queries := []string{ - `{.service_name = "foo"}`, - `{.service_name = "foo" && .http.status_code = 200}`, - `{.service_name = "foo" && .http.status_code = 200 && .http.method = "GET"}`, - `{.service_name = "foo" && .http.status_code = 200 && .http.method = "GET" && .http.url = "/foo"}`, - `{.service_name = "foo" && .cluster = }`, - `{.service_name = "foo" && .http.status_code = 200 && .cluster = }`, - `{.service_name = "foo" && .http.status_code = 200 && .http.method = "GET" && .cluster = }`, - `{.service_name = "foo" && .http.status_code = 200 && .http.method = "GET" && .http.url = "/foo" && .cluster = }`, - } - for _, query := range queries { - b.Run(query, func(b *testing.B) { - for i := 0; i < b.N; i++ { - _ = traceql.ExtractMatchers(query) - } - }) - } -} - func TestIncludeBlock(t *testing.T) { tests := []struct { blocKStart int64 diff --git a/pkg/traceql/extractmatcher_test.go b/pkg/traceql/extractmatcher_test.go new file mode 100644 index 00000000000..3414df6e45b --- /dev/null +++ b/pkg/traceql/extractmatcher_test.go @@ -0,0 +1,118 @@ +package traceql + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestExtractMatchers(t *testing.T) { + testCases := []struct { + name, query, expected string + }{ + { + name: "empty query", + query: "", + expected: "{}", + }, + { + name: "empty query with spaces", + query: " { } ", + expected: "{}", + }, + { + name: "simple query", + query: `{.service_name = "foo"}`, + expected: `{.service_name = "foo"}`, + }, + { + name: "incomplete query", + query: `{ .http.status_code = 200 && .http.method = }`, + expected: "{.http.status_code = 200}", + }, + { + name: "invalid query", + query: "{ 2 = .b ", + expected: "{}", + }, + { + name: "long query", + query: `{.service_name = "foo" && .http.status_code = 200 && .http.method = "GET" && .cluster = }`, + expected: `{.service_name = "foo" && .http.status_code = 200 && .http.method = "GET"}`, + }, + { + name: "query with duration a boolean", + query: `{ duration > 5s && .success = true && .cluster = }`, + expected: `{duration > 5s && .success = true}`, + }, + { + name: "query with three selectors with AND", + query: `{ .foo = "bar" && .baz = "qux" } && { duration > 1s } || { .foo = "bar" && .baz = "qux" }`, + expected: "{}", + }, + { + name: "query with OR conditions", + query: `{ (.foo = "bar" || .baz = "qux") && duration > 1s }`, + expected: "{}", + }, + { + name: "query with multiple selectors and pipelines", + query: `{ .foo = "bar" && .baz = "qux" } && { duration > 1s } || { .foo = "bar" && .baz = "qux" } | count() > 4`, + expected: "{}", + }, + { + name: "query with slash in value", + query: `{ span.http.target = "/api/v1/users" }`, + expected: `{span.http.target = "/api/v1/users"}`, + }, + { + name: "intrinsics", + query: `{ name = "foo" }`, + expected: `{name = "foo"}`, + }, + { + name: "incomplete intrinsics", + query: `{ statusMessage = }`, + expected: "{}", + }, + { + name: "query with missing closing bracket", + query: `{resource.service_name = "foo" && span.http.target=`, + expected: `{resource.service_name = "foo"}`, + }, + { + name: "uncommon characters", + query: `{ span.foo = "<>:b5[]" && resource.service.name = }`, + expected: `{span.foo = "<>:b5[]"}`, + }, + { + name: "kind", + query: `{ kind = server }`, + expected: `{kind = server}`, + }, + } + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + assert.Equal(t, tc.expected, ExtractMatchers(tc.query)) + }) + } +} + +func BenchmarkExtractMatchers(b *testing.B) { + queries := []string{ + `{.service_name = "foo"}`, + `{.service_name = "foo" && .http.status_code = 200}`, + `{.service_name = "foo" && .http.status_code = 200 && .http.method = "GET"}`, + `{.service_name = "foo" && .http.status_code = 200 && .http.method = "GET" && .http.url = "/foo"}`, + `{.service_name = "foo" && .cluster = }`, + `{.service_name = "foo" && .http.status_code = 200 && .cluster = }`, + `{.service_name = "foo" && .http.status_code = 200 && .http.method = "GET" && .cluster = }`, + `{.service_name = "foo" && .http.status_code = 200 && .http.method = "GET" && .http.url = "/foo" && .cluster = }`, + } + for _, query := range queries { + b.Run(query, func(b *testing.B) { + for i := 0; i < b.N; i++ { + _ = ExtractMatchers(query) + } + }) + } +}