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

Add tracql support for ancestor and parent operator #2877

Merged
merged 9 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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,4 +1,5 @@
## main / unreleased
* [FEATURE] New TraceQL structural operators ancestor (<<), parent (<) [#2877](https://github.com/grafana/tempo/pull/2877) (@kousikmitra)
* [ENHANCEMENT] Add support for searching by span status message using `statusMessage` keyword [#2848](https://github.com/grafana/tempo/pull/2848) (@kousikmitra)
* [FEATURE] Add the `/api/status/buildinfo` endpoint [#2702](https://github.com/grafana/tempo/pull/2702) (@fabrizio-grafana)
* [FEATURE] New encoding vParquet3 with support for dedicated attribute columns (@mapno, @stoewer) [#2649](https://github.com/grafana/tempo/pull/2649)
Expand Down
2 changes: 2 additions & 0 deletions docs/sources/tempo/traceql/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,9 @@ The second expression returns no traces because it's impossible for a single spa
These spanset operators look at the structure of a trace and the relationship between the spans.

- `{condA} >> {condB}` - The descendant operator (`>>`) looks for spans matching `{condB}` that are descendants of a span matching `{condA}`
- `{condA} << {condB}` - The ancestor operator (`<<`) looks for spans matching `{condB}` that are ancestor of a span matching `{condA}`
- `{condA} > {condB}` - The child operator (`>`) looks for spans matching `{condB}` that are direct child spans of a parent matching `{condA}`
- `{condA} > {condB}` - The parent operator (`<`) looks for spans matching `{condB}` that are direct parent spans of a child matching `{condA}`
mdisibio marked this conversation as resolved.
Show resolved Hide resolved
- `{condA} ~ {condB}` - The sibling operator (`~`) checks that spans matching `{condA}` and `{condB}` are siblings of the same parent span.

For example, to find a trace where a specific HTTP API interacted with a specific database:
Expand Down
8 changes: 8 additions & 0 deletions pkg/traceql/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,18 @@ func (o SpansetOperation) extractConditions(request *FetchSpansRequest) {
request.Conditions = append(request.Conditions, Condition{
Attribute: NewIntrinsic(IntrinsicStructuralDescendant),
})
case OpSpansetAncestor:
request.Conditions = append(request.Conditions, Condition{
Attribute: NewIntrinsic(IntrinsicStructuralDescendant),
})
case OpSpansetChild:
request.Conditions = append(request.Conditions, Condition{
Attribute: NewIntrinsic(IntrinsicStructuralChild),
})
case OpSpansetParent:
request.Conditions = append(request.Conditions, Condition{
Attribute: NewIntrinsic(IntrinsicStructuralParent),
})
case OpSpansetSibling:
request.Conditions = append(request.Conditions, Condition{
Attribute: NewIntrinsic(IntrinsicStructuralSibling),
Expand Down
34 changes: 34 additions & 0 deletions pkg/traceql/ast_execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,23 @@ func (o SpansetOperation) evaluate(input []*Spanset) (output []*Spanset, err err
output = append(output, matchingSpanset)
}

case OpSpansetAncestor:
spans, err := o.joinSpansets(lhs, rhs, func(l, r Span) bool {
// In case of ancestor the lhs becomes descendant of rhs
return l.DescendantOf(r)
})
if err != nil {
return nil, err
}

if len(spans) > 0 {
// Clone here to capture previously computed aggregates, grouped attrs, etc.
// Copy spans to new slice because of internal buffering.
matchingSpanset := input[i].clone()
matchingSpanset.Spans = append([]Span(nil), spans...)
output = append(output, matchingSpanset)
}

case OpSpansetChild:
spans, err := o.joinSpansets(lhs, rhs, func(l, r Span) bool {
return r.ChildOf(l)
Expand All @@ -132,6 +149,23 @@ func (o SpansetOperation) evaluate(input []*Spanset) (output []*Spanset, err err
output = append(output, matchingSpanset)
}

case OpSpansetParent:
spans, err := o.joinSpansets(lhs, rhs, func(l, r Span) bool {
// In case of parent the lhs becomes child of rhs
return l.ChildOf(r)
})
if err != nil {
return nil, err
}

if len(spans) > 0 {
// Clone here to capture previously computed aggregates, grouped attrs, etc.
// Copy spans to new slice because of internal buffering.
matchingSpanset := input[i].clone()
matchingSpanset.Spans = append([]Span(nil), spans...)
output = append(output, matchingSpanset)
}

case OpSpansetSibling:
spans, err := o.joinSpansets(lhs, rhs, func(l, r Span) bool {
return r.SiblingOf(l)
Expand Down
38 changes: 38 additions & 0 deletions pkg/traceql/ast_execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,20 @@ func TestSpansetOperationEvaluate(t *testing.T) {
}},
},
},
{
"{ .child } << { .parent }",
[]*Spanset{
{Spans: []Span{
newMockSpan([]byte{1}).WithAttrBool("parent", true).WithNestedSetInfo(0, 1, 4),
newMockSpan([]byte{1}).WithAttrBool("child", true).WithNestedSetInfo(1, 2, 3),
}},
},
[]*Spanset{
{Spans: []Span{
newMockSpan([]byte{1}).WithAttrBool("parent", true).WithNestedSetInfo(0, 1, 4),
}},
},
},
{
"{ .parent } > { .child }",
[]*Spanset{
Expand All @@ -373,6 +387,20 @@ func TestSpansetOperationEvaluate(t *testing.T) {
}},
},
},
{
"{ .child } < { .parent }",
[]*Spanset{
{Spans: []Span{
newMockSpan([]byte{1}).WithAttrBool("parent", true).WithNestedSetInfo(0, 1, 4),
newMockSpan([]byte{1}).WithAttrBool("child", true).WithNestedSetInfo(1, 2, 3),
}},
},
[]*Spanset{
{Spans: []Span{
newMockSpan([]byte{1}).WithAttrBool("parent", true).WithNestedSetInfo(0, 1, 4),
}},
},
},
{
"{ .child1 } ~ { .child2 }",
[]*Spanset{
Expand All @@ -397,6 +425,16 @@ func TestSpansetOperationEvaluate(t *testing.T) {
},
[]*Spanset{},
},
{ // tests that parent operators do not modify the spanset
"{ } < { } < { } < { }",
[]*Spanset{
{Spans: []Span{
newMockSpan([]byte{1}).WithAttrBool("parent1", true).WithNestedSetInfo(1, 2, 3),
newMockSpan([]byte{1}).WithAttrBool("parent2", true).WithNestedSetInfo(1, 4, 5),
}},
},
[]*Spanset{},
},
}

for _, tc := range testCases {
Expand Down
2 changes: 2 additions & 0 deletions pkg/traceql/ast_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ func (o BinaryOperation) validate() error {

switch o.Op {
case OpSpansetChild,
OpSpansetParent,
OpSpansetDescendant,
OpSpansetAncestor,
OpSpansetSibling:
return newUnsupportedError(fmt.Sprintf("binary operation (%v)", o.Op))
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/traceql/enum_attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@ const (

// These intrinsics do not map to specific data points, but are used to
// indicate that Spans must be able to answer the structural methods
// DescdendantOf, SiblingOf, and ChildOf. The details of those methods
// DescdendantOf, AncestorOf, SiblingOf, ChildOf and ParentOf. The details of those methods
mdisibio marked this conversation as resolved.
Show resolved Hide resolved
// and how these intrinsics are handled is left to the implementation.
IntrinsicStructuralDescendant
IntrinsicStructuralAncestor
mdisibio marked this conversation as resolved.
Show resolved Hide resolved
IntrinsicStructuralSibling
IntrinsicStructuralChild
IntrinsicStructuralParent

// not yet implemented in traceql and may never be. these exist so that we can retrieve
// these fields from the fetch layer
Expand Down
6 changes: 6 additions & 0 deletions pkg/traceql/enum_operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ const (
OpOr
OpNot
OpSpansetChild
OpSpansetParent
OpSpansetDescendant
OpSpansetAncestor
OpSpansetAnd
OpSpansetUnion
OpSpansetSibling
Expand Down Expand Up @@ -149,8 +151,12 @@ func (op Operator) String() string {
return "!"
case OpSpansetChild:
return ">"
case OpSpansetParent:
return "<"
case OpSpansetDescendant:
return ">>"
case OpSpansetAncestor:
return "<<"
case OpSpansetAnd:
return "&&"
case OpSpansetSibling:
Expand Down
4 changes: 4 additions & 0 deletions pkg/traceql/enum_operators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ func TestOperatorIsBoolean(t *testing.T) {
{OpOr, true},
{OpNot, true},
{OpSpansetChild, false},
{OpSpansetParent, false},
{OpSpansetDescendant, false},
{OpSpansetAncestor, false},
{OpSpansetAnd, false},
{OpSpansetUnion, false},
{OpSpansetSibling, false},
Expand Down Expand Up @@ -140,7 +142,9 @@ func TestOperatorUnaryTypesValid(t *testing.T) {
{OpAnd, TypeInt, false},
{OpOr, TypeInt, false},
{OpSpansetChild, TypeInt, false},
{OpSpansetParent, TypeInt, false},
{OpSpansetDescendant, TypeInt, false},
{OpSpansetAncestor, TypeInt, false},
{OpSpansetAnd, TypeInt, false},
{OpSpansetUnion, TypeInt, false},
{OpSpansetSibling, TypeInt, false},
Expand Down
6 changes: 5 additions & 1 deletion pkg/traceql/expr.y
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ import (
// Operators are listed with increasing precedence.
%left <binOp> PIPE
%left <binOp> AND OR
%left <binOp> EQ NEQ LT LTE GT GTE NRE RE DESC TILDE
%left <binOp> EQ NEQ LT LTE GT GTE NRE RE DESC ANCE TILDE
%left <binOp> ADD SUB
%left <binOp> NOT
%left <binOp> MUL DIV MOD
Expand All @@ -107,7 +107,9 @@ spansetPipelineExpression: // shares the same operators as spansetExpression. sp
OPEN_PARENS spansetPipelineExpression CLOSE_PARENS { $$ = $2 }
| spansetPipelineExpression AND spansetPipelineExpression { $$ = newSpansetOperation(OpSpansetAnd, $1, $3) }
| spansetPipelineExpression GT spansetPipelineExpression { $$ = newSpansetOperation(OpSpansetChild, $1, $3) }
| spansetPipelineExpression LT spansetPipelineExpression { $$ = newSpansetOperation(OpSpansetParent, $1, $3) }
| spansetPipelineExpression DESC spansetPipelineExpression { $$ = newSpansetOperation(OpSpansetDescendant, $1, $3) }
| spansetPipelineExpression ANCE spansetPipelineExpression { $$ = newSpansetOperation(OpSpansetAncestor, $1, $3) }
| spansetPipelineExpression OR spansetPipelineExpression { $$ = newSpansetOperation(OpSpansetUnion, $1, $3) }
| spansetPipelineExpression TILDE spansetPipelineExpression { $$ = newSpansetOperation(OpSpansetSibling, $1, $3) }
| wrappedSpansetPipeline { $$ = $1 }
Expand Down Expand Up @@ -149,7 +151,9 @@ spansetExpression: // shares the same operators as scalarPipelineExpression. spl
OPEN_PARENS spansetExpression CLOSE_PARENS { $$ = $2 }
| spansetExpression AND spansetExpression { $$ = newSpansetOperation(OpSpansetAnd, $1, $3) }
| spansetExpression GT spansetExpression { $$ = newSpansetOperation(OpSpansetChild, $1, $3) }
| spansetExpression LT spansetExpression { $$ = newSpansetOperation(OpSpansetParent, $1, $3) }
| spansetExpression DESC spansetExpression { $$ = newSpansetOperation(OpSpansetDescendant, $1, $3) }
| spansetExpression ANCE spansetExpression { $$ = newSpansetOperation(OpSpansetAncestor, $1, $3) }
| spansetExpression OR spansetExpression { $$ = newSpansetOperation(OpSpansetUnion, $1, $3) }
| spansetExpression TILDE spansetExpression { $$ = newSpansetOperation(OpSpansetSibling, $1, $3) }
| spansetFilter { $$ = $1 }
Expand Down
Loading