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

feat: add list of comparators in the selector page #3888

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions docs/docs/concepts/selectors.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1381,3 +1381,18 @@ flowchart TD
class F parentSpan
class G selectedSpan
```

## Supported comparisons

In previous examples, It was always used the equality comparator (`=`), however, the selector language supports a number of comparisons:

| Operation | Description |
| :------------ | :--------------------------------------------------------------- |
| `=` | both values are equal |
| `!=` | both values are different |
| `<` | value on the left is less than the value on the right |
| `<=` | value on the left is less or equal to the value on the right |
| `>` | value on the left is greater than the value on the right |
| `>=` | value on the left is greater or equal to the value on the right |
| `contains` | string on the left contains the string on the right |
| `not-contains`| string on the left does not contain the string on the right |
5 changes: 5 additions & 0 deletions server/assertions/selectors/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ func TestSimpleSelectorBuilder(t *testing.T) {
Expression: "span.tracetest.span.type=\"http\"",
ShouldSucceed: false,
},
{
Name: "Selector with invalid syntax",
Expression: "span[attr.bool=true]",
ShouldSucceed: true,
},
}

for _, testCase := range testCases {
Expand Down
20 changes: 17 additions & 3 deletions server/assertions/selectors/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/alecthomas/participle/v2"
"github.com/alecthomas/participle/v2/lexer"
)

type ParserSelector struct {
Expand All @@ -18,14 +19,14 @@ type parserSpanSelector struct {

type parserFilter struct {
Property string `( @Ident ( @"." @Ident )*)`
Operator string `@("=" | "contains" )`
Operator string `@Comparator`
Value *parserValue `@@*`
}

type parserValue struct {
String *string ` @String`
Int *int64 ` | @Int`
Float *float64 ` | @Float`
Int *int64 ` | @Int`
Boolean *bool ` | @("true" | "false")`
}

Expand All @@ -34,8 +35,21 @@ type parserPseudoClass struct {
Value *parserValue `("(" @@* ")")*`
}

var languageLexer = lexer.MustStateful(lexer.Rules{
"Root": {
{Name: "whitespace", Pattern: `\s+`, Action: nil},
{Name: "Punc", Pattern: `[(),|\[\]:]`, Action: nil},

{Name: "Comparator", Pattern: `!=|<=|>=|=|<|>|contains|not-contains`},
{Name: "Ident", Pattern: `[a-zA-Z][a-zA-Z0-9_\.]*`, Action: nil},
{Name: "String", Pattern: `"(\\"|[^"])*"`, Action: nil},
{Name: "Float", Pattern: `[0-9]+\.[0-9]+`},
{Name: "Int", Pattern: `[0-9]+`},
},
})

func CreateParser() (*participle.Parser, error) {
parser, err := participle.Build(&ParserSelector{})
parser, err := participle.Build(&ParserSelector{}, participle.Lexer(languageLexer), participle.UseLookahead(2))
if err != nil {
return nil, fmt.Errorf("could not create parser: %w", err)
}
Expand Down
10 changes: 10 additions & 0 deletions server/assertions/selectors/selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ func TestSelector(t *testing.T) {
Expression: `span[kind="db"]`,
ExpectedSpanIds: []trace.SpanID{insertPokemonDatabaseSpanID, updatePokemonDatabaseSpanID},
},
{
Name: "SelectorShouldMatchAllButDB",
Expression: `span[kind!="db"]`,
ExpectedSpanIds: []trace.SpanID{postImportSpanID, getPokemonFromExternalAPISpanID},
},
{
Name: "SelectorShouldGetSpansWithAttribute",
Expression: `span[kind!=""]`,
ExpectedSpanIds: []trace.SpanID{postImportSpanID, insertPokemonDatabaseSpanID, getPokemonFromExternalAPISpanID, updatePokemonDatabaseSpanID},
},
}

for _, testCase := range testCases {
Expand Down
Loading