Skip to content

Commit

Permalink
Move some tests to traceql
Browse files Browse the repository at this point in the history
Signed-off-by: Ruben Vargas <[email protected]>
  • Loading branch information
rubenvp8510 committed Nov 30, 2023
1 parent b6716f1 commit 766bdea
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 114 deletions.
114 changes: 0 additions & 114 deletions modules/ingester/instance_search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
118 changes: 118 additions & 0 deletions pkg/traceql/extractmatcher_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}

0 comments on commit 766bdea

Please sign in to comment.