-
Notifications
You must be signed in to change notification settings - Fork 536
/
Copy pathblock_traceql.go
3438 lines (2970 loc) · 114 KB
/
block_traceql.go
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
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package vparquet4
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"math"
"reflect"
"sort"
"sync"
"time"
"unsafe"
"github.com/parquet-go/parquet-go"
"github.com/grafana/tempo/pkg/parquetquery"
v1 "github.com/grafana/tempo/pkg/tempopb/trace/v1"
"github.com/grafana/tempo/pkg/traceql"
"github.com/grafana/tempo/pkg/util"
"github.com/grafana/tempo/tempodb/backend"
"github.com/grafana/tempo/tempodb/encoding/common"
)
var (
pqSpanPool = parquetquery.NewResultPool(1)
pqSpansetPool = parquetquery.NewResultPool(1)
pqTracePool = parquetquery.NewResultPool(1)
pqAttrPool = parquetquery.NewResultPool(1)
pqEventPool = parquetquery.NewResultPool(1)
pqLinkPool = parquetquery.NewResultPool(1)
pqInstrumentationPool = parquetquery.NewResultPool(1)
)
type attrVal struct {
a traceql.Attribute
s traceql.Static
}
// span implements traceql.Span
type span struct {
spanAttrs []attrVal
resourceAttrs []attrVal
traceAttrs []attrVal
eventAttrs []attrVal
linkAttrs []attrVal
instrumentationAttrs []attrVal
id []byte
startTimeUnixNanos uint64
durationNanos uint64
nestedSetParent int32
nestedSetLeft int32
nestedSetRight int32
// metadata used to track the span in the parquet file
rowNum parquetquery.RowNumber
cbSpansetFinal bool
cbSpanset *traceql.Spanset
}
func (s *span) AllAttributes() map[traceql.Attribute]traceql.Static {
atts := make(map[traceql.Attribute]traceql.Static, len(s.spanAttrs)+len(s.resourceAttrs)+len(s.traceAttrs)+len(s.eventAttrs)+len(s.linkAttrs))
for _, st := range s.traceAttrs {
if st.s.Type == traceql.TypeNil {
continue
}
atts[st.a] = st.s
}
for _, st := range s.resourceAttrs {
if st.s.Type == traceql.TypeNil {
continue
}
atts[st.a] = st.s
}
for _, st := range s.spanAttrs {
if st.s.Type == traceql.TypeNil {
continue
}
atts[st.a] = st.s
}
for _, st := range s.eventAttrs {
if st.s.Type == traceql.TypeNil {
continue
}
atts[st.a] = st.s
}
for _, st := range s.linkAttrs {
if st.s.Type == traceql.TypeNil {
continue
}
atts[st.a] = st.s
}
for _, st := range s.instrumentationAttrs {
if st.s.Type == traceql.TypeNil {
continue
}
atts[st.a] = st.s
}
return atts
}
func (s *span) AllAttributesFunc(cb func(traceql.Attribute, traceql.Static)) {
for _, a := range s.traceAttrs {
cb(a.a, a.s)
}
for _, a := range s.resourceAttrs {
cb(a.a, a.s)
}
for _, a := range s.instrumentationAttrs {
cb(a.a, a.s)
}
for _, a := range s.spanAttrs {
cb(a.a, a.s)
}
}
func (s *span) AttributeFor(a traceql.Attribute) (traceql.Static, bool) {
find := func(a traceql.Attribute, attrs []attrVal) *traceql.Static {
if len(attrs) == 1 {
if attrs[0].a == a {
return &attrs[0].s
}
return nil
}
if len(attrs) == 2 {
if attrs[0].a == a {
return &attrs[0].s
}
if attrs[1].a == a {
return &attrs[1].s
}
return nil
}
for i := range attrs {
if attrs[i].a == a {
return &attrs[i].s
}
}
return nil
}
findName := func(s string, attrs []attrVal) *traceql.Static {
if len(attrs) == 1 {
if attrs[0].a.Name == s {
return &attrs[0].s
}
return nil
}
if len(attrs) == 2 {
if attrs[0].a.Name == s {
return &attrs[0].s
}
if attrs[1].a.Name == s {
return &attrs[1].s
}
return nil
}
for i := range attrs {
if attrs[i].a.Name == s {
return &attrs[i].s
}
}
return nil
}
switch a.Scope {
case traceql.AttributeScopeResource:
if len(s.resourceAttrs) > 0 {
if attr := find(a, s.resourceAttrs); attr != nil {
return *attr, true
}
}
return traceql.StaticNil, false
case traceql.AttributeScopeSpan:
if len(s.spanAttrs) > 0 {
if attr := find(a, s.spanAttrs); attr != nil {
return *attr, true
}
}
return traceql.StaticNil, false
case traceql.AttributeScopeEvent:
if len(s.eventAttrs) > 0 {
if attr := find(a, s.eventAttrs); attr != nil {
return *attr, true
}
}
return traceql.StaticNil, false
case traceql.AttributeScopeLink:
if len(s.linkAttrs) > 0 {
if attr := find(a, s.linkAttrs); attr != nil {
return *attr, true
}
}
return traceql.StaticNil, false
case traceql.AttributeScopeInstrumentation:
if len(s.instrumentationAttrs) > 0 {
if attr := find(a, s.instrumentationAttrs); attr != nil {
return *attr, true
}
}
return traceql.StaticNil, false
}
if a.Intrinsic != traceql.IntrinsicNone {
if a.Intrinsic == traceql.IntrinsicNestedSetLeft {
return traceql.NewStaticInt(int(s.nestedSetLeft)), true
}
if a.Intrinsic == traceql.IntrinsicNestedSetRight {
return traceql.NewStaticInt(int(s.nestedSetRight)), true
}
if a.Intrinsic == traceql.IntrinsicNestedSetParent {
return traceql.NewStaticInt(int(s.nestedSetParent)), true
}
// intrinsics are always on the span, trace, event, or link ... for now
if attr := find(a, s.spanAttrs); attr != nil {
return *attr, true
}
if attr := find(a, s.traceAttrs); attr != nil {
return *attr, true
}
if attr := find(a, s.eventAttrs); attr != nil {
return *attr, true
}
if attr := find(a, s.linkAttrs); attr != nil {
return *attr, true
}
if attr := find(a, s.instrumentationAttrs); attr != nil {
return *attr, true
}
}
// name search in span, resource, link, and event to give precedence to span
// we don't need to do a name search at the trace level b/c it is intrinsics only
if len(s.spanAttrs) > 0 {
if attr := findName(a.Name, s.spanAttrs); attr != nil {
return *attr, true
}
}
if len(s.resourceAttrs) > 0 {
if attr := findName(a.Name, s.resourceAttrs); attr != nil {
return *attr, true
}
}
if len(s.eventAttrs) > 0 {
if attr := findName(a.Name, s.eventAttrs); attr != nil {
return *attr, true
}
}
if len(s.linkAttrs) > 0 {
if attr := findName(a.Name, s.linkAttrs); attr != nil {
return *attr, true
}
}
if len(s.instrumentationAttrs) > 0 {
if attr := findName(a.Name, s.instrumentationAttrs); attr != nil {
return *attr, true
}
}
return traceql.StaticNil, false
}
func (s *span) ID() []byte {
return s.id
}
func (s *span) StartTimeUnixNanos() uint64 {
return s.startTimeUnixNanos
}
func (s *span) DurationNanos() uint64 {
return s.durationNanos
}
func (s *span) DescendantOf(lhs, rhs []traceql.Span, falseForAll, invert, union bool, buffer []traceql.Span) []traceql.Span {
if len(lhs) == 0 && len(rhs) == 0 {
return nil
}
if union {
return descendantOfUnion(lhs, rhs, invert, buffer)
}
// sort by nested set left. the goal is to quickly be able to find the first entry in the lhs slice that
// potentially matches the rhs. after we find this first potential match we just check every single lhs
// entry til the end of the slice.
// it might be even better to clone the lhs slice. sort one by left and one by right and search the one that
// requires less seeking after the search. this would be faster but cloning the slice would be costly in mem
sortFn := func(i, j int) bool { return lhs[i].(*span).nestedSetLeft > lhs[j].(*span).nestedSetLeft } // sort asc b/c we are interested in lhs nestedSetLeft > rhs nestedSetLeft
if invert {
sortFn = func(i, j int) bool { return lhs[i].(*span).nestedSetLeft < lhs[j].(*span).nestedSetLeft } // sort desc b/c we want the inverse relationship. see descendantOf func
}
sort.Slice(lhs, sortFn)
descendantOf := func(a, b *span) bool {
if a.nestedSetLeft == 0 ||
b.nestedSetLeft == 0 ||
a.nestedSetRight == 0 ||
b.nestedSetRight == 0 {
// Spans with missing data, never a match.
return false
}
return a.nestedSetLeft > b.nestedSetLeft && a.nestedSetRight < b.nestedSetRight
}
for _, r := range rhs {
matches := false
findFn := func(i int) bool { return lhs[i].(*span).nestedSetLeft <= r.(*span).nestedSetLeft }
if invert {
findFn = func(i int) bool { return lhs[i].(*span).nestedSetLeft >= r.(*span).nestedSetLeft }
}
// let's find the first index we need to bother with.
found := sort.Search(len(lhs), findFn)
if found == -1 { // if we are less then the entire slice we have to search the entire slice
found = 0
}
for ; found < len(lhs); found++ {
a := lhs[found].(*span)
b := r.(*span)
if invert {
a, b = b, a
}
if descendantOf(b, a) {
// Returns RHS
matches = true
break
}
}
if matches && !falseForAll || // return RHS if there are any matches on the LHS
!matches && falseForAll { // return RHS if there are no matches on the LHS
buffer = append(buffer, r)
}
}
return buffer
}
// descendantOfUnion is a special loop designed to handle union descendantOf. technically nestedSetManyManyLoop can logically do this
// but it contains a pathological case where it devolves to O(n^2) in the worst case (a trace with a single series of nested spans).
// this loop more directly handles union descendantof.
// - iterate the rhs checking to see if it is a descendant of lhs
// - break out of the rhs loop when the next span will be in a different branch
// - at this point rSpan has the narrowest span (the leaf span) of the branch
// - iterate the lhs as long as its in the same branch as rSpan checking for ancestors
// - go back to rhs iteration and repeat until slices are exhausted
func descendantOfUnion(lhs, rhs []traceql.Span, invert bool, buffer []traceql.Span) []traceql.Span {
// union is harder b/c we have to find all matches on both the left and rhs
sort.Slice(lhs, func(i, j int) bool { return lhs[i].(*span).nestedSetLeft < lhs[j].(*span).nestedSetLeft })
if unsafe.SliceData(lhs) != unsafe.SliceData(rhs) { // if these are pointing to the same slice, no reason to sort again
sort.Slice(rhs, func(i, j int) bool { return rhs[i].(*span).nestedSetLeft < rhs[j].(*span).nestedSetLeft })
}
isAfter := func(a, b *span) bool {
return b.nestedSetLeft > a.nestedSetRight
}
isMatch := func(a, d *span) bool {
if d.nestedSetLeft == 0 ||
a.nestedSetLeft == 0 ||
d.nestedSetRight == 0 ||
a.nestedSetRight == 0 {
return false
}
return d.nestedSetLeft > a.nestedSetLeft && d.nestedSetRight < a.nestedSetRight
}
if invert {
lhs, rhs = rhs, lhs
}
uniqueSpans := make(map[*span]struct{}) // todo: consider a reusable map, like our buffer slice
addToBuffer := func(s *span) {
if _, ok := uniqueSpans[s]; !ok {
buffer = append(buffer, s)
uniqueSpans[s] = struct{}{}
}
}
lidx := 0
ridx := 0
for lidx < len(lhs) && ridx < len(rhs) {
lSpan := lhs[lidx].(*span)
rSpan := rhs[ridx].(*span)
// rhs
for ; ridx < len(rhs); ridx++ {
rSpan = rhs[ridx].(*span)
if isMatch(lSpan, rSpan) {
addToBuffer(rSpan)
}
// test the next span to see if it is still in the tree of lhs. if not bail!
if ridx+1 < len(rhs) && isAfter(rSpan, rhs[ridx+1].(*span)) {
ridx++
break
}
}
// lhs
// rSpan contains the narrowest span that is in tree for lhs. advance and add lhs until we're out of tree
for ; lidx < len(lhs); lidx++ {
lSpan = lhs[lidx].(*span)
// advance LHS until out of tree of RHS
if isAfter(rSpan, lSpan) {
break
}
if isMatch(lSpan, rSpan) {
addToBuffer(lSpan)
// if rSpan is in tree of lSpan keep on keeping on
if ridx < len(rhs) && isMatch(lSpan, rhs[ridx].(*span)) {
break
}
}
}
}
return buffer
}
// SiblingOf
func (s *span) SiblingOf(lhs, rhs []traceql.Span, falseForAll, union bool, buffer []traceql.Span) []traceql.Span {
if len(lhs) == 0 && len(rhs) == 0 {
return nil
}
if union {
// union is more difficult b/c we have to find all matches on both the left and rhs
sort.Slice(lhs, func(i, j int) bool { return lhs[i].(*span).nestedSetParent < lhs[j].(*span).nestedSetParent })
if unsafe.SliceData(lhs) != unsafe.SliceData(rhs) { // if these are pointing to the same slice, no reason to sort again
sort.Slice(rhs, func(i, j int) bool { return rhs[i].(*span).nestedSetParent < rhs[j].(*span).nestedSetParent })
}
siblingOf := func(a, b *span) bool {
return a.nestedSetParent == b.nestedSetParent &&
a.nestedSetParent != 0 &&
b.nestedSetParent != 0 &&
a != b // a span cannot be its own sibling. note that this only works due to implementation details in the engine. if we ever pipeline structural operators then we would need to use something else for identity. rownumber?
}
isValid := func(s *span) bool { return s.nestedSetParent != 0 }
isAfter := func(a, b *span) bool { return b.nestedSetParent > a.nestedSetParent }
return nestedSetManyManyLoop(lhs, rhs, isValid, siblingOf, isAfter, falseForAll, false, union, buffer)
}
// this is easy. we're just looking for anything on the lhs side with the same nested set parent as the rhs
sort.Slice(lhs, func(i, j int) bool {
return lhs[i].(*span).nestedSetParent < lhs[j].(*span).nestedSetParent
})
siblingOf := func(a, b *span) bool {
return a.nestedSetParent == b.nestedSetParent &&
a.nestedSetParent != 0 &&
b.nestedSetParent != 0
}
for _, r := range rhs {
matches := false
if r.(*span).nestedSetParent != 0 {
// search for nested set parent
found := sort.Search(len(lhs), func(i int) bool {
return lhs[i].(*span).nestedSetParent >= r.(*span).nestedSetParent
})
if found >= 0 && found < len(lhs) {
matches = siblingOf(r.(*span), lhs[found].(*span))
// if we found a match BUT this is the same span as the match we need to check the very next span (if it exists).
// this works b/c Search method returns the first match for nestedSetParent
if matches && r.(*span) == lhs[found].(*span) {
matches = false
if found+1 < len(lhs) {
matches = siblingOf(r.(*span), lhs[found+1].(*span))
}
}
}
}
if matches && !falseForAll || // return RHS if there are any matches on the LHS
!matches && falseForAll { // return RHS if there are no matches on the LHS
buffer = append(buffer, r)
}
}
return buffer
}
// {} > {}
func (s *span) ChildOf(lhs, rhs []traceql.Span, falseForAll, invert, union bool, buffer []traceql.Span) []traceql.Span {
if len(lhs) == 0 && len(rhs) == 0 {
return nil
}
if union {
childOf := func(p, c *span) bool {
return p.nestedSetLeft == c.nestedSetParent &&
p.nestedSetLeft != 0 &&
c.nestedSetParent != 0
}
isValid := func(s *span) bool { return s.nestedSetLeft != 0 }
isAfter := func(p, c *span) bool { return c.nestedSetParent > p.nestedSetLeft }
// the engine will sometimes pass the same slice for both lhs and rhs. this occurs for {} > {}.
// if lhs is the same slice as rhs we need to make a copy of the slice to sort them by different values
if unsafe.SliceData(lhs) == unsafe.SliceData(rhs) {
rhs = append([]traceql.Span{}, rhs...)
}
parents := lhs
children := rhs
if invert {
parents, children = children, parents
}
sort.Slice(parents, func(i, j int) bool { return parents[i].(*span).nestedSetLeft < parents[j].(*span).nestedSetLeft })
sort.Slice(children, func(i, j int) bool { return children[i].(*span).nestedSetParent < children[j].(*span).nestedSetParent })
return nestedSetOneManyLoop(parents, children, isValid, childOf, isAfter, falseForAll, invert, union, buffer)
}
// we will search the LHS by either nestedSetLeft or nestedSetParent. if we are doing child we sort by nestedSetLeft
// so we can quickly find children. if the invert flag is set we are looking for parents and so we sort appropriately
sortFn := func(i, j int) bool { return lhs[i].(*span).nestedSetLeft < lhs[j].(*span).nestedSetLeft }
if invert {
sortFn = func(i, j int) bool { return lhs[i].(*span).nestedSetParent < lhs[j].(*span).nestedSetParent }
}
childOf := func(a, b *span) bool {
return a.nestedSetLeft == b.nestedSetParent &&
a.nestedSetLeft != 0 &&
b.nestedSetParent != 0
}
sort.Slice(lhs, sortFn)
for _, r := range rhs {
findFn := func(i int) bool { return lhs[i].(*span).nestedSetLeft >= r.(*span).nestedSetParent }
if invert {
findFn = func(i int) bool { return lhs[i].(*span).nestedSetParent >= r.(*span).nestedSetLeft }
}
// search for nested set parent
matches := false
found := sort.Search(len(lhs), findFn)
if found >= 0 && found < len(lhs) {
if invert {
matches = childOf(r.(*span), lhs[found].(*span)) // is the rhs a child of the lhs?
} else {
matches = childOf(lhs[found].(*span), r.(*span)) // is the lhs a child of the rhs?
}
}
if matches && !falseForAll || // return RHS if there are any matches on the LHS
!matches && falseForAll { // return RHS if there are no matches on the LHS
buffer = append(buffer, r)
}
}
return buffer
}
// nestedSetOneManyLoop runs a standard one -> many loop to calculate nested set relationships. It handles all nested set relationships except
// siblingOf and unioned descendantOf. It forward iterates the one and many slices and applies.
func nestedSetOneManyLoop(one, many []traceql.Span, isValid func(*span) bool, isMatch, isAfter func(*span, *span) bool, falseForAll, invert, union bool, buffer []traceql.Span) []traceql.Span {
var uniqueSpans map[*span]struct{}
if union {
uniqueSpans = make(map[*span]struct{}) // todo: consider a reusable map, like our buffer slice
}
addToBuffer := func(s *span) {
if union {
if _, ok := uniqueSpans[s]; !ok {
buffer = append(buffer, s)
uniqueSpans[s] = struct{}{}
}
} else {
buffer = append(buffer, s)
}
}
// note the small differences between this and the !invert loop. technically we could write these both in one piece of code,
// but this feels better for clarity
if invert {
manyIdx := 0
for _, o := range one {
oSpan := o.(*span)
if !isValid(oSpan) {
continue
}
matches := false
for ; manyIdx < len(many); manyIdx++ {
mSpan := many[manyIdx].(*span)
// if the many loop is ahead of the one loop break back to allow the one loop to let it catch up
if isAfter(oSpan, mSpan) {
break
}
if isMatch(oSpan, mSpan) {
matches = true
if union {
addToBuffer(mSpan)
} else {
break
}
}
}
if (matches && !falseForAll) || (!matches && falseForAll) {
addToBuffer(oSpan)
}
}
return buffer
}
// !invert
manyIdx := 0
for _, o := range one {
oSpan := o.(*span)
if !isValid(oSpan) {
continue
}
matches := false
for ; manyIdx < len(many); manyIdx++ {
mSpan := many[manyIdx].(*span)
// if the many loop is ahead of the one loop break back to the allow one loop to let it catch up
if isAfter(oSpan, mSpan) {
break
}
match := isMatch(oSpan, mSpan)
if (match && !falseForAll) || (!match && falseForAll) {
matches = true
addToBuffer(mSpan)
}
}
if matches && union {
addToBuffer(oSpan)
}
}
// drain the rest of the children if falseForAll
if falseForAll {
for ; manyIdx < len(many); manyIdx++ {
addToBuffer(many[manyIdx].(*span))
}
}
return buffer
}
// nestedSetManyManyLoop handles the generic case when the lhs must be checked multiple times for each rhs. it is currently only
// used for siblingOf
func nestedSetManyManyLoop(lhs, rhs []traceql.Span, isValid func(*span) bool, isMatch, isAfter func(*span, *span) bool, falseForAll, invert, union bool, buffer []traceql.Span) []traceql.Span {
var uniqueSpans map[*span]struct{}
if union {
uniqueSpans = make(map[*span]struct{}) // todo: consider a reusable map, like our buffer slice
}
addToBuffer := func(s *span) {
if union {
if _, ok := uniqueSpans[s]; !ok {
buffer = append(buffer, s)
uniqueSpans[s] = struct{}{}
}
} else {
buffer = append(buffer, s)
}
}
rescanIdx := 0
lidx := 0
for _, r := range rhs {
rSpan := r.(*span)
if !isValid(rSpan) {
continue
}
// rescan whatever amount of rhs we need to
lidx = rescanIdx
matches := false
for ; lidx < len(lhs); lidx++ {
lSpan := lhs[lidx].(*span)
// if left is after right, swap back to right
if isAfter(rSpan, lSpan) {
break
}
// if we transition forward (trees branches or parents or whatever) store current lidx to rescan
if isAfter(lhs[rescanIdx].(*span), lSpan) {
rescanIdx = lidx
}
if (!invert && isMatch(rSpan, lSpan)) || (invert && isMatch(lSpan, rSpan)) {
matches = true
if union {
addToBuffer(lSpan)
}
}
}
if (matches && !falseForAll) || (!matches && falseForAll) {
addToBuffer(rSpan)
}
}
return buffer
}
func (s *span) addSpanAttr(a traceql.Attribute, st traceql.Static) {
s.spanAttrs = append(s.spanAttrs, attrVal{a: a, s: st})
}
func (s *span) setInstrumentationAttrs(attrs []attrVal) {
s.instrumentationAttrs = append(s.instrumentationAttrs, attrs...)
}
func (s *span) setResourceAttrs(attrs []attrVal) {
s.resourceAttrs = append(s.resourceAttrs, attrs...)
}
func (s *span) setTraceAttrs(attrs []attrVal) {
s.traceAttrs = append(s.traceAttrs, attrs...)
}
func (s *span) setEventAttrs(attrs []attrVal) {
s.eventAttrs = append(s.eventAttrs, attrs...)
}
func (s *span) setLinkAttrs(attrs []attrVal) {
s.linkAttrs = append(s.linkAttrs, attrs...)
}
// attributesMatched counts all attributes in the map as well as metadata fields like start/end/id
func (s *span) attributesMatched() int {
count := 0
// todo: attributesMatced is called a lot. we could cache this count on set
for _, st := range s.spanAttrs {
if st.s.Type != traceql.TypeNil {
count++
}
}
for _, st := range s.resourceAttrs {
if st.s.Type != traceql.TypeNil {
count++
}
}
for _, st := range s.traceAttrs {
if st.s.Type != traceql.TypeNil {
count++
}
}
for _, st := range s.eventAttrs {
if st.s.Type != traceql.TypeNil {
count++
}
}
for _, st := range s.linkAttrs {
if st.s.Type != traceql.TypeNil {
count++
}
}
for _, st := range s.instrumentationAttrs {
if st.s.Type != traceql.TypeNil {
count++
}
}
if s.startTimeUnixNanos != 0 {
count++
}
// don't count duration nanos b/c it is added to the attributes as well as the span struct
// if s.durationNanos != 0 {
// count++
// }
if len(s.id) > 0 {
count++
}
if s.nestedSetLeft > 0 || s.nestedSetRight > 0 || s.nestedSetParent != 0 { // nestedSetParent can be -1 meaning it is a root span
count++
}
return count
}
// todo: this sync pool currently massively reduces allocations by pooling spans for certain queries.
// it currently catches spans discarded:
// - in the span collector
// - in the batch collector
// - while converting to spanmeta
// to be fully effective it needs to catch spans thrown away in the query engine. perhaps filter spans
// can return a slice of dropped and kept spansets?
var spanPool = sync.Pool{
New: func() interface{} {
return &span{}
},
}
func putSpan(s *span) {
s.id = nil
s.startTimeUnixNanos = 0
s.durationNanos = 0
s.rowNum = parquetquery.EmptyRowNumber()
s.cbSpansetFinal = false
s.cbSpanset = nil
s.nestedSetParent = 0
s.nestedSetLeft = 0
s.nestedSetRight = 0
s.spanAttrs = s.spanAttrs[:0]
s.resourceAttrs = s.resourceAttrs[:0]
s.traceAttrs = s.traceAttrs[:0]
s.eventAttrs = s.eventAttrs[:0]
s.linkAttrs = s.linkAttrs[:0]
s.instrumentationAttrs = s.instrumentationAttrs[:0]
spanPool.Put(s)
}
func getSpan() *span {
return spanPool.Get().(*span)
}
var spansetPool = sync.Pool{}
func getSpanset() *traceql.Spanset {
ss := spansetPool.Get()
if ss == nil {
return &traceql.Spanset{
ReleaseFn: putSpansetAndSpans,
}
}
return ss.(*traceql.Spanset)
}
// putSpanset back into the pool. Does not repool the spans.
func putSpanset(ss *traceql.Spanset) {
ss.Attributes = ss.Attributes[:0]
ss.DurationNanos = 0
ss.RootServiceName = ""
ss.RootSpanName = ""
ss.Scalar = traceql.NewStaticNil()
ss.StartTimeUnixNanos = 0
ss.TraceID = nil
clear(ss.ServiceStats)
ss.Spans = ss.Spans[:0]
spansetPool.Put(ss)
}
func putSpansetAndSpans(ss *traceql.Spanset) {
if ss != nil {
for _, s := range ss.Spans {
if span, ok := s.(*span); ok {
putSpan(span)
}
}
putSpanset(ss)
}
}
const (
columnPathTraceID = "TraceID"
columnPathStartTimeUnixNano = "StartTimeUnixNano"
columnPathEndTimeUnixNano = "EndTimeUnixNano"
columnPathDurationNanos = "DurationNano"
columnPathRootSpanName = "RootSpanName"
columnPathRootServiceName = "RootServiceName"
columnPathServiceStatsServiceName = "ServiceStats.key_value.key"
columnPathServiceStatsSpanCount = "ServiceStats.key_value.value.SpanCount"
columnPathServiceStatsErrorCount = "ServiceStats.key_value.value.ErrorCount"
columnPathResourceAttrKey = "rs.list.element.Resource.Attrs.list.element.Key"
columnPathResourceAttrString = "rs.list.element.Resource.Attrs.list.element.Value.list.element"
columnPathResourceAttrInt = "rs.list.element.Resource.Attrs.list.element.ValueInt.list.element"
columnPathResourceAttrDouble = "rs.list.element.Resource.Attrs.list.element.ValueDouble.list.element"
columnPathResourceAttrBool = "rs.list.element.Resource.Attrs.list.element.ValueBool.list.element"
columnPathResourceServiceName = "rs.list.element.Resource.ServiceName"
columnPathResourceCluster = "rs.list.element.Resource.Cluster"
columnPathResourceNamespace = "rs.list.element.Resource.Namespace"
columnPathResourcePod = "rs.list.element.Resource.Pod"
columnPathResourceContainer = "rs.list.element.Resource.Container"
columnPathResourceK8sClusterName = "rs.list.element.Resource.K8sClusterName"
columnPathResourceK8sNamespaceName = "rs.list.element.Resource.K8sNamespaceName"
columnPathResourceK8sPodName = "rs.list.element.Resource.K8sPodName"
columnPathResourceK8sContainerName = "rs.list.element.Resource.K8sContainerName"
columnPathInstrumentationName = "rs.list.element.ss.list.element.Scope.Name"
columnPathInstrumentationVersion = "rs.list.element.ss.list.element.Scope.Version"
columnPathInstrumentationAttrKey = "rs.list.element.ss.list.element.Scope.Attrs.list.element.Key"
columnPathInstrumentationAttrString = "rs.list.element.ss.list.element.Scope.Attrs.list.element.Value.list.element"
columnPathInstrumentationAttrInt = "rs.list.element.ss.list.element.Scope.Attrs.list.element.ValueInt.list.element"
columnPathInstrumentationAttrDouble = "rs.list.element.ss.list.element.Scope.Attrs.list.element.ValueDouble.list.element"
columnPathInstrumentationAttrBool = "rs.list.element.ss.list.element.Scope.Attrs.list.element.ValueBool.list.element"
columnPathSpanID = "rs.list.element.ss.list.element.Spans.list.element.SpanID"
columnPathSpanName = "rs.list.element.ss.list.element.Spans.list.element.Name"
columnPathSpanStartTime = "rs.list.element.ss.list.element.Spans.list.element.StartTimeUnixNano"
columnPathSpanDuration = "rs.list.element.ss.list.element.Spans.list.element.DurationNano"
columnPathSpanKind = "rs.list.element.ss.list.element.Spans.list.element.Kind"
columnPathSpanStatusCode = "rs.list.element.ss.list.element.Spans.list.element.StatusCode"
columnPathSpanStatusMessage = "rs.list.element.ss.list.element.Spans.list.element.StatusMessage"
columnPathSpanAttrKey = "rs.list.element.ss.list.element.Spans.list.element.Attrs.list.element.Key"
columnPathSpanAttrString = "rs.list.element.ss.list.element.Spans.list.element.Attrs.list.element.Value.list.element"
columnPathSpanAttrInt = "rs.list.element.ss.list.element.Spans.list.element.Attrs.list.element.ValueInt.list.element"
columnPathSpanAttrDouble = "rs.list.element.ss.list.element.Spans.list.element.Attrs.list.element.ValueDouble.list.element"
columnPathSpanAttrBool = "rs.list.element.ss.list.element.Spans.list.element.Attrs.list.element.ValueBool.list.element"
columnPathSpanHTTPStatusCode = "rs.list.element.ss.list.element.Spans.list.element.HttpStatusCode"
columnPathSpanHTTPMethod = "rs.list.element.ss.list.element.Spans.list.element.HttpMethod"
columnPathSpanHTTPURL = "rs.list.element.ss.list.element.Spans.list.element.HttpUrl"
columnPathSpanNestedSetLeft = "rs.list.element.ss.list.element.Spans.list.element.NestedSetLeft"
columnPathSpanNestedSetRight = "rs.list.element.ss.list.element.Spans.list.element.NestedSetRight"
columnPathSpanParentID = "rs.list.element.ss.list.element.Spans.list.element.ParentID"
columnPathEventName = "rs.list.element.ss.list.element.Spans.list.element.Events.list.element.Name"
columnPathEventTimeSinceStart = "rs.list.element.ss.list.element.Spans.list.element.Events.list.element.TimeSinceStartNano"
columnPathLinkTraceID = "rs.list.element.ss.list.element.Spans.list.element.Links.list.element.TraceID"
columnPathLinkSpanID = "rs.list.element.ss.list.element.Spans.list.element.Links.list.element.SpanID"
columnPathEventAttrKey = "rs.list.element.ss.list.element.Spans.list.element.Events.list.element.Attrs.list.element.Key"
columnPathEventAttrString = "rs.list.element.ss.list.element.Spans.list.element.Events.list.element.Attrs.list.element.Value.list.element"
columnPathEventAttrInt = "rs.list.element.ss.list.element.Spans.list.element.Events.list.element.Attrs.list.element.ValueInt.list.element"
columnPathEventAttrDouble = "rs.list.element.ss.list.element.Spans.list.element.Events.list.element.Attrs.list.element.ValueDouble.list.element"
columnPathEventAttrBool = "rs.list.element.ss.list.element.Spans.list.element.Events.list.element.Attrs.list.element.ValueBool.list.element"
columnPathLinkAttrKey = "rs.list.element.ss.list.element.Spans.list.element.Links.list.element.Attrs.list.element.Key"
columnPathLinkAttrString = "rs.list.element.ss.list.element.Spans.list.element.Links.list.element.Attrs.list.element.Value.list.element"
columnPathLinkAttrInt = "rs.list.element.ss.list.element.Spans.list.element.Links.list.element.Attrs.list.element.ValueInt.list.element"
columnPathLinkAttrDouble = "rs.list.element.ss.list.element.Spans.list.element.Links.list.element.Attrs.list.element.ValueDouble.list.element"
columnPathLinkAttrBool = "rs.list.element.ss.list.element.Spans.list.element.Links.list.element.Attrs.list.element.ValueBool.list.element"
otherEntrySpansetKey = "spanset"
otherEntrySpanKey = "span"
otherEntryEventKey = "event"
otherEntryLinkKey = "link"
otherEntryInstrumentationKey = "instrumentation"
// a fake intrinsic scope at the trace lvl
intrinsicScopeTrace = -1
intrinsicScopeSpan = -2
intrinsicScopeEvent = -3
intrinsicScopeLink = -4
intrinsicScopeInstrumentation = -5
)
// todo: scope is the only field used here. either remove the other fields or use them.
var intrinsicColumnLookups = map[traceql.Intrinsic]struct {
scope traceql.AttributeScope
typ traceql.StaticType
columnPath string
}{
traceql.IntrinsicName: {intrinsicScopeSpan, traceql.TypeString, columnPathSpanName},
traceql.IntrinsicStatus: {intrinsicScopeSpan, traceql.TypeStatus, columnPathSpanStatusCode},
traceql.IntrinsicStatusMessage: {intrinsicScopeSpan, traceql.TypeString, columnPathSpanStatusMessage},
traceql.IntrinsicDuration: {intrinsicScopeSpan, traceql.TypeDuration, columnPathSpanDuration},
traceql.IntrinsicKind: {intrinsicScopeSpan, traceql.TypeKind, columnPathSpanKind},
traceql.IntrinsicSpanID: {intrinsicScopeSpan, traceql.TypeString, columnPathSpanID},
traceql.IntrinsicSpanStartTime: {intrinsicScopeSpan, traceql.TypeString, columnPathSpanStartTime},
traceql.IntrinsicStructuralDescendant: {intrinsicScopeSpan, traceql.TypeNil, ""}, // Not a real column, this entry is only used to assign default scope.
traceql.IntrinsicStructuralChild: {intrinsicScopeSpan, traceql.TypeNil, ""}, // Not a real column, this entry is only used to assign default scope.
traceql.IntrinsicStructuralSibling: {intrinsicScopeSpan, traceql.TypeNil, ""}, // Not a real column, this entry is only used to assign default scope.
traceql.IntrinsicNestedSetLeft: {intrinsicScopeSpan, traceql.TypeInt, columnPathSpanNestedSetLeft},
traceql.IntrinsicNestedSetRight: {intrinsicScopeSpan, traceql.TypeInt, columnPathSpanNestedSetRight},
traceql.IntrinsicNestedSetParent: {intrinsicScopeSpan, traceql.TypeInt, columnPathSpanParentID},
traceql.IntrinsicTraceRootService: {intrinsicScopeTrace, traceql.TypeString, columnPathRootServiceName},
traceql.IntrinsicTraceRootSpan: {intrinsicScopeTrace, traceql.TypeString, columnPathRootSpanName},
traceql.IntrinsicTraceDuration: {intrinsicScopeTrace, traceql.TypeString, columnPathDurationNanos},
traceql.IntrinsicTraceID: {intrinsicScopeTrace, traceql.TypeString, columnPathTraceID},
traceql.IntrinsicTraceStartTime: {intrinsicScopeTrace, traceql.TypeDuration, columnPathStartTimeUnixNano},
traceql.IntrinsicEventName: {intrinsicScopeEvent, traceql.TypeString, columnPathEventName},
traceql.IntrinsicEventTimeSinceStart: {intrinsicScopeEvent, traceql.TypeDuration, columnPathEventTimeSinceStart},
traceql.IntrinsicLinkTraceID: {intrinsicScopeLink, traceql.TypeString, columnPathLinkTraceID},
traceql.IntrinsicLinkSpanID: {intrinsicScopeLink, traceql.TypeString, columnPathLinkSpanID},
traceql.IntrinsicInstrumentationName: {intrinsicScopeInstrumentation, traceql.TypeString, columnPathInstrumentationName},
traceql.IntrinsicInstrumentationVersion: {intrinsicScopeInstrumentation, traceql.TypeString, columnPathInstrumentationVersion},
traceql.IntrinsicServiceStats: {intrinsicScopeTrace, traceql.TypeNil, ""}, // Not a real column, this entry is only used to assign default scope.