forked from grafana/tempo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast.go
More file actions
837 lines (697 loc) · 17 KB
/
ast.go
File metadata and controls
837 lines (697 loc) · 17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
package traceql
import (
"fmt"
"math"
"regexp"
"time"
"github.com/grafana/tempo/pkg/tempopb"
)
type Element interface {
fmt.Stringer
validate() error
}
type metricsFirstStageElement interface {
Element
extractConditions(request *FetchSpansRequest)
init(*tempopb.QueryRangeRequest)
observe(Span) // TODO - batching?
result() SeriesSet
}
type pipelineElement interface {
Element
extractConditions(request *FetchSpansRequest)
evaluate([]*Spanset) ([]*Spanset, error)
}
type typedExpression interface {
impliedType() StaticType
}
type RootExpr struct {
Pipeline Pipeline
MetricsPipeline metricsFirstStageElement
Hints *Hints
}
func newRootExpr(e pipelineElement) *RootExpr {
p, ok := e.(Pipeline)
if !ok {
p = newPipeline(e)
}
return &RootExpr{
Pipeline: p,
}
}
func newRootExprWithMetrics(e pipelineElement, m metricsFirstStageElement) *RootExpr {
p, ok := e.(Pipeline)
if !ok {
p = newPipeline(e)
}
return &RootExpr{
Pipeline: p,
MetricsPipeline: m,
}
}
func (r *RootExpr) withHints(h *Hints) *RootExpr {
r.Hints = h
return r
}
// **********************
// Pipeline
// **********************
type Pipeline struct {
Elements []pipelineElement
}
// nolint: revive
func (Pipeline) __scalarExpression() {}
// nolint: revive
func (Pipeline) __spansetExpression() {}
func newPipeline(i ...pipelineElement) Pipeline {
return Pipeline{
Elements: i,
}
}
func (p Pipeline) addItem(i pipelineElement) Pipeline {
p.Elements = append(p.Elements, i)
return p
}
func (p Pipeline) impliedType() StaticType {
if len(p.Elements) == 0 {
return TypeSpanset
}
finalItem := p.Elements[len(p.Elements)-1]
aggregate, ok := finalItem.(Aggregate)
if ok {
return aggregate.impliedType()
}
return TypeSpanset
}
func (p Pipeline) extractConditions(req *FetchSpansRequest) {
for _, element := range p.Elements {
element.extractConditions(req)
}
// TODO this needs to be fine-tuned a bit, e.g. { .foo = "bar" } | by(.namespace), AllConditions can still be true
if len(p.Elements) > 1 {
req.AllConditions = false
}
}
func (p Pipeline) evaluate(input []*Spanset) (result []*Spanset, err error) {
result = input
for _, element := range p.Elements {
result, err = element.evaluate(result)
if err != nil {
return nil, err
}
if len(result) == 0 {
return []*Spanset{}, nil
}
}
return result, nil
}
type GroupOperation struct {
Expression FieldExpression
groupBuffer map[Static]*Spanset
}
func newGroupOperation(e FieldExpression) GroupOperation {
return GroupOperation{
Expression: e,
groupBuffer: make(map[Static]*Spanset),
}
}
func (o GroupOperation) extractConditions(request *FetchSpansRequest) {
o.Expression.extractConditions(request)
}
type CoalesceOperation struct{}
func newCoalesceOperation() CoalesceOperation {
return CoalesceOperation{}
}
func (o CoalesceOperation) extractConditions(*FetchSpansRequest) {
}
type SelectOperation struct {
attrs []Attribute
}
func newSelectOperation(exprs []Attribute) SelectOperation {
return SelectOperation{
attrs: exprs,
}
}
// **********************
// Scalars
// **********************
type ScalarExpression interface {
// pipelineElement
Element
typedExpression
__scalarExpression()
extractConditions(request *FetchSpansRequest)
}
type ScalarOperation struct {
Op Operator
LHS ScalarExpression
RHS ScalarExpression
}
func newScalarOperation(op Operator, lhs ScalarExpression, rhs ScalarExpression) ScalarOperation {
return ScalarOperation{
Op: op,
LHS: lhs,
RHS: rhs,
}
}
// nolint: revive
func (ScalarOperation) __scalarExpression() {}
func (o ScalarOperation) impliedType() StaticType {
if o.Op.isBoolean() {
return TypeBoolean
}
// remaining operators will be based on the operands
// opAdd, opSub, opDiv, opMod, opMult
t := o.LHS.impliedType()
if t != TypeAttribute {
return t
}
return o.RHS.impliedType()
}
func (o ScalarOperation) extractConditions(request *FetchSpansRequest) {
o.LHS.extractConditions(request)
o.RHS.extractConditions(request)
request.AllConditions = false
}
type Aggregate struct {
op AggregateOp
e FieldExpression
}
func newAggregate(agg AggregateOp, e FieldExpression) Aggregate {
return Aggregate{
op: agg,
e: e,
}
}
// nolint: revive
func (Aggregate) __scalarExpression() {}
func (a Aggregate) impliedType() StaticType {
if a.op == aggregateCount || a.e == nil {
return TypeInt
}
return a.e.impliedType()
}
func (a Aggregate) extractConditions(request *FetchSpansRequest) {
if a.e != nil {
a.e.extractConditions(request)
}
}
// **********************
// Spansets
// **********************
type SpansetExpression interface {
pipelineElement
__spansetExpression()
}
type SpansetOperation struct {
Op Operator
LHS SpansetExpression
RHS SpansetExpression
matchingSpansBuffer []Span
}
func (o SpansetOperation) extractConditions(request *FetchSpansRequest) {
switch o.Op {
case OpSpansetDescendant, OpSpansetAncestor, OpSpansetNotDescendant, OpSpansetNotAncestor:
request.Conditions = append(request.Conditions, Condition{
Attribute: NewIntrinsic(IntrinsicStructuralDescendant),
})
case OpSpansetChild, OpSpansetParent, OpSpansetNotChild, OpSpansetNotParent:
request.Conditions = append(request.Conditions, Condition{
Attribute: NewIntrinsic(IntrinsicStructuralChild),
})
case OpSpansetSibling, OpSpansetNotSibling:
request.Conditions = append(request.Conditions, Condition{
Attribute: NewIntrinsic(IntrinsicStructuralSibling),
})
}
o.LHS.extractConditions(request)
o.RHS.extractConditions(request)
request.AllConditions = false
}
func newSpansetOperation(op Operator, lhs SpansetExpression, rhs SpansetExpression) SpansetOperation {
return SpansetOperation{
Op: op,
LHS: lhs,
RHS: rhs,
}
}
// nolint: revive
func (SpansetOperation) __spansetExpression() {}
type SpansetFilter struct {
Expression FieldExpression
matchingSpansBuffer []Span
}
func newSpansetFilter(e FieldExpression) *SpansetFilter {
return &SpansetFilter{
Expression: e,
}
}
// nolint: revive
func (*SpansetFilter) __spansetExpression() {}
func (f *SpansetFilter) evaluate(input []*Spanset) ([]*Spanset, error) {
var outputBuffer []*Spanset
for _, ss := range input {
if len(ss.Spans) == 0 {
continue
}
f.matchingSpansBuffer = f.matchingSpansBuffer[:0]
for _, s := range ss.Spans {
result, err := f.Expression.execute(s)
if err != nil {
return nil, err
}
if result.Type != TypeBoolean {
continue
}
if !result.B {
continue
}
f.matchingSpansBuffer = append(f.matchingSpansBuffer, s)
}
if len(f.matchingSpansBuffer) == 0 {
continue
}
if len(f.matchingSpansBuffer) == len(ss.Spans) {
// All matched, so we return the input as-is
// and preserve the local buffer.
outputBuffer = append(outputBuffer, ss)
continue
}
matchingSpanset := ss.clone()
matchingSpanset.Spans = append([]Span(nil), f.matchingSpansBuffer...)
outputBuffer = append(outputBuffer, matchingSpanset)
}
return outputBuffer, nil
}
type ScalarFilter struct {
op Operator
lhs ScalarExpression
rhs ScalarExpression
}
func newScalarFilter(op Operator, lhs ScalarExpression, rhs ScalarExpression) ScalarFilter {
return ScalarFilter{
op: op,
lhs: lhs,
rhs: rhs,
}
}
// nolint: revive
func (ScalarFilter) __spansetExpression() {}
func (f ScalarFilter) extractConditions(request *FetchSpansRequest) {
f.lhs.extractConditions(request)
f.rhs.extractConditions(request)
request.AllConditions = false
}
// **********************
// Expressions
// **********************
type FieldExpression interface {
Element
typedExpression
// referencesSpan returns true if this field expression has any attributes or intrinsics. i.e. it references the span itself
referencesSpan() bool
__fieldExpression()
extractConditions(request *FetchSpansRequest)
execute(span Span) (Static, error)
}
type BinaryOperation struct {
Op Operator
LHS FieldExpression
RHS FieldExpression
compiledExpression *regexp.Regexp
}
func newBinaryOperation(op Operator, lhs FieldExpression, rhs FieldExpression) *BinaryOperation {
return &BinaryOperation{
Op: op,
LHS: lhs,
RHS: rhs,
}
}
// nolint: revive
func (BinaryOperation) __fieldExpression() {}
func (o *BinaryOperation) impliedType() StaticType {
if o.Op.isBoolean() {
return TypeBoolean
}
// remaining operators will be based on the operands
// opAdd, opSub, opDiv, opMod, opMult
t := o.LHS.impliedType()
if t != TypeAttribute {
return t
}
return o.RHS.impliedType()
}
func (o *BinaryOperation) referencesSpan() bool {
return o.LHS.referencesSpan() || o.RHS.referencesSpan()
}
type UnaryOperation struct {
Op Operator
Expression FieldExpression
}
func newUnaryOperation(op Operator, e FieldExpression) UnaryOperation {
return UnaryOperation{
Op: op,
Expression: e,
}
}
// nolint: revive
func (UnaryOperation) __fieldExpression() {}
func (o UnaryOperation) impliedType() StaticType {
// both operators (opPower and opNot) will just be based on the operand type
return o.Expression.impliedType()
}
func (o UnaryOperation) referencesSpan() bool {
return o.Expression.referencesSpan()
}
// **********************
// Statics
// **********************
type Static struct {
Type StaticType
N int
F float64
S string
B bool
D time.Duration
Status Status // todo: can we just use the N member for status and kind?
Kind Kind
}
// nolint: revive
func (Static) __fieldExpression() {}
// nolint: revive
func (Static) __scalarExpression() {}
func (Static) referencesSpan() bool {
return false
}
func (s Static) impliedType() StaticType {
return s.Type
}
func (s Static) Equals(other Static) bool {
// if they are different number types. compare them as floats. however, if they are the same type just fall through to
// a normal comparison which should be more efficient
differentNumberTypes := (s.Type == TypeInt || s.Type == TypeFloat || s.Type == TypeDuration) &&
(other.Type == TypeInt || other.Type == TypeFloat || other.Type == TypeDuration) &&
s.Type != other.Type
if differentNumberTypes {
return s.asFloat() == other.asFloat()
}
eitherIsTypeStatus := (s.Type == TypeStatus && other.Type == TypeInt) || (other.Type == TypeStatus && s.Type == TypeInt)
if eitherIsTypeStatus {
if s.Type == TypeStatus {
return s.Status == Status(other.N)
}
return Status(s.N) == other.Status
}
// no special cases, just compare directly
return s == other
}
func (s Static) compare(other *Static) int {
if s.Type != other.Type {
if s.asFloat() > other.asFloat() {
return 1
} else if s.asFloat() < other.asFloat() {
return -1
}
return 0
}
switch s.Type {
case TypeInt:
if s.N > other.N {
return 1
} else if s.N < other.N {
return -1
}
case TypeFloat:
if s.F > other.F {
return 1
} else if s.F < other.F {
return -1
}
case TypeDuration:
if s.D > other.D {
return 1
} else if s.D < other.D {
return -1
}
case TypeString:
if s.S > other.S {
return 1
} else if s.S < other.S {
return -1
}
case TypeBoolean:
if s.B && !other.B {
return 1
} else if !s.B && other.B {
return -1
}
case TypeStatus:
if s.Status > other.Status {
return 1
} else if s.Status < other.Status {
return -1
}
case TypeKind:
if s.Kind > other.Kind {
return 1
} else if s.Kind < other.Kind {
return -1
}
}
return 0
}
func (s *Static) sumInto(other Static) {
switch s.Type {
case TypeInt:
s.N += other.N
case TypeFloat:
s.F += other.F
case TypeDuration:
s.D += other.D
}
}
func (s Static) divideBy(f float64) Static {
switch s.Type {
case TypeInt:
return NewStaticFloat(float64(s.N) / f) // there's no integer division in traceql
case TypeFloat:
return NewStaticFloat(s.F / f)
case TypeDuration:
return NewStaticDuration(s.D / time.Duration(f))
}
return s
}
func (s Static) asFloat() float64 {
switch s.Type {
case TypeInt:
return float64(s.N)
case TypeFloat:
return s.F
case TypeDuration:
return float64(s.D.Nanoseconds())
default:
return math.NaN()
}
}
func NewStaticInt(n int) Static {
return Static{
Type: TypeInt,
N: n,
}
}
func NewStaticFloat(f float64) Static {
return Static{
Type: TypeFloat,
F: f,
}
}
func NewStaticString(s string) Static {
return Static{
Type: TypeString,
S: s,
}
}
func NewStaticBool(b bool) Static {
return Static{
Type: TypeBoolean,
B: b,
}
}
func NewStaticNil() Static {
return Static{
Type: TypeNil,
}
}
func NewStaticDuration(d time.Duration) Static {
return Static{
Type: TypeDuration,
D: d,
}
}
func NewStaticStatus(s Status) Static {
return Static{
Type: TypeStatus,
Status: s,
}
}
func NewStaticKind(k Kind) Static {
return Static{
Type: TypeKind,
Kind: k,
}
}
// **********************
// Attributes
// **********************
type Attribute struct {
Scope AttributeScope
Parent bool
Name string
Intrinsic Intrinsic
}
// NewAttribute creates a new attribute with the given identifier string.
func NewAttribute(att string) Attribute {
return Attribute{
Scope: AttributeScopeNone,
Parent: false,
Name: att,
Intrinsic: IntrinsicNone,
}
}
// nolint: revive
func (Attribute) __fieldExpression() {}
func (a Attribute) impliedType() StaticType {
switch a.Intrinsic {
case IntrinsicDuration:
return TypeDuration
case IntrinsicChildCount:
return TypeInt
case IntrinsicName:
return TypeString
case IntrinsicStatus:
return TypeStatus
case IntrinsicStatusMessage:
return TypeString
case IntrinsicKind:
return TypeKind
case IntrinsicParent:
return TypeNil
case IntrinsicTraceDuration:
return TypeDuration
case IntrinsicTraceRootService:
return TypeString
case IntrinsicTraceRootSpan:
return TypeString
case IntrinsicNestedSetLeft:
return TypeInt
case IntrinsicNestedSetRight:
return TypeInt
case IntrinsicNestedSetParent:
return TypeInt
}
return TypeAttribute
}
func (Attribute) referencesSpan() bool {
return true
}
// NewScopedAttribute creates a new scopedattribute with the given identifier string.
// this handles parent, span, and resource scopes.
func NewScopedAttribute(scope AttributeScope, parent bool, att string) Attribute {
intrinsic := IntrinsicNone
// if we are explicitly passed a resource or span scopes then we shouldn't parse for intrinsic
if scope != AttributeScopeResource && scope != AttributeScopeSpan {
intrinsic = intrinsicFromString(att)
}
return Attribute{
Scope: scope,
Parent: parent,
Name: att,
Intrinsic: intrinsic,
}
}
func NewIntrinsic(n Intrinsic) Attribute {
return Attribute{
Scope: AttributeScopeNone,
Parent: false,
Name: n.String(),
Intrinsic: n,
}
}
var (
_ pipelineElement = (*Pipeline)(nil)
_ pipelineElement = (*Aggregate)(nil)
_ pipelineElement = (*SpansetOperation)(nil)
_ pipelineElement = (*SpansetFilter)(nil)
_ pipelineElement = (*CoalesceOperation)(nil)
_ pipelineElement = (*ScalarFilter)(nil)
_ pipelineElement = (*GroupOperation)(nil)
)
// MetricsAggregate is a placeholder in the AST for a metrics aggregation
// pipeline element. It has a superset of the properties of them all, and
// builds them later via init() so that appropriate buffers can be allocated
// for the query time range and step.
type MetricsAggregate struct {
op MetricsAggregateOp
by []Attribute
attr Attribute
floats []float64
agg SpanAggregator
}
func newMetricsAggregate(agg MetricsAggregateOp, by []Attribute) *MetricsAggregate {
return &MetricsAggregate{
op: agg,
by: by,
}
}
func newMetricsAggregateQuantileOverTime(attr Attribute, qs []float64, by []Attribute) *MetricsAggregate {
return &MetricsAggregate{
op: metricsAggregateQuantileOverTime,
floats: qs,
attr: attr,
by: by,
}
}
func (a *MetricsAggregate) extractConditions(request *FetchSpansRequest) {
switch a.op {
case metricsAggregateRate, metricsAggregateCountOverTime:
// No extra conditions, start time is already enough
}
for _, b := range a.by {
if !request.HasAttribute(b) {
request.SecondPassConditions = append(request.SecondPassConditions, Condition{
Attribute: b,
})
}
}
}
func (a *MetricsAggregate) init(q *tempopb.QueryRangeRequest) {
var innerAgg func() VectorAggregator
switch a.op {
case metricsAggregateCountOverTime:
innerAgg = func() VectorAggregator { return NewCountOverTimeAggregator() }
case metricsAggregateRate:
innerAgg = func() VectorAggregator { return NewRateAggregator(1.0 / time.Duration(q.Step).Seconds()) }
}
a.agg = NewGroupingAggregator(a.op.String(), func() RangeAggregator {
return NewStepAggregator(q.Start, q.End, q.Step, innerAgg)
}, a.by)
}
func (a *MetricsAggregate) observe(span Span) {
a.agg.Observe(span)
}
func (a *MetricsAggregate) result() SeriesSet {
return a.agg.Series()
}
func (a *MetricsAggregate) validate() error {
switch a.op {
case metricsAggregateCountOverTime:
case metricsAggregateRate:
default:
return newUnsupportedError(fmt.Sprintf("metrics aggregate operation (%v)", a.op))
}
if len(a.by) > maxGroupBys {
return newUnsupportedError(fmt.Sprintf("metrics group by %v values", len(a.by)))
}
return nil
}
var _ metricsFirstStageElement = (*MetricsAggregate)(nil)