-
Notifications
You must be signed in to change notification settings - Fork 536
/
Copy pathblock_traceql_test.go
2070 lines (1894 loc) · 74.8 KB
/
block_traceql_test.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"
"fmt"
"math/rand"
"os"
"path"
"sort"
"strconv"
"strings"
"testing"
"time"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"github.com/grafana/tempo/pkg/parquetquery"
"github.com/grafana/tempo/pkg/tempopb"
v1 "github.com/grafana/tempo/pkg/tempopb/trace/v1"
"github.com/grafana/tempo/pkg/traceql"
"github.com/grafana/tempo/pkg/traceqlmetrics"
"github.com/grafana/tempo/pkg/util"
"github.com/grafana/tempo/pkg/util/test"
"github.com/grafana/tempo/tempodb/backend"
"github.com/grafana/tempo/tempodb/backend/local"
"github.com/grafana/tempo/tempodb/encoding/common"
)
func TestOne(t *testing.T) {
wantTr := fullyPopulatedTestTrace(nil)
b := makeBackendBlockWithTraces(t, []*Trace{wantTr})
ctx := context.Background()
q := `{ resource.region != nil && resource.service.name = "bar" }`
// q := `{ resource.str-array =~ "value.*" }`
req := traceql.MustExtractFetchSpansRequestWithMetadata(q)
req.StartTimeUnixNanos = uint64(1000 * time.Second)
req.EndTimeUnixNanos = uint64(1001 * time.Second)
resp, err := b.Fetch(ctx, req, common.DefaultSearchOptions())
require.NoError(t, err, "search request:", req)
spanSet, err := resp.Results.Next(ctx)
require.NoError(t, err, "search request:", req)
fmt.Println(q)
fmt.Println("-----------")
fmt.Println(resp.Results.(*spansetIterator).iter)
fmt.Println("-----------")
fmt.Println(spanSet)
}
func TestBackendBlockSearchTraceQL(t *testing.T) {
numTraces := 250
traces := make([]*Trace, 0, numTraces)
wantTraceIdx := rand.Intn(numTraces)
wantTraceID := test.ValidTraceID(nil)
for i := 0; i < numTraces; i++ {
if i == wantTraceIdx {
traces = append(traces, fullyPopulatedTestTrace(wantTraceID))
continue
}
id := test.ValidTraceID(nil)
tr, _ := traceToParquet(&backend.BlockMeta{}, id, test.MakeTrace(1, id), nil)
traces = append(traces, tr)
}
b := makeBackendBlockWithTraces(t, traces)
ctx := context.Background()
traceIDText := util.TraceIDToHexString(wantTraceID)
searchesThatMatch := []struct {
name string
req traceql.FetchSpansRequest
}{
{"empty request", traceql.FetchSpansRequest{}},
{
"Time range inside trace",
traceql.FetchSpansRequest{
StartTimeUnixNanos: uint64(1100 * time.Second),
EndTimeUnixNanos: uint64(1200 * time.Second),
},
},
{
"Time range overlap start",
traceql.FetchSpansRequest{
StartTimeUnixNanos: uint64(900 * time.Second),
EndTimeUnixNanos: uint64(1100 * time.Second),
},
},
{
"Time range overlap end",
traceql.FetchSpansRequest{
StartTimeUnixNanos: uint64(1900 * time.Second),
EndTimeUnixNanos: uint64(2100 * time.Second),
},
},
// Intrinsics
{"Intrinsic: name", traceql.MustExtractFetchSpansRequestWithMetadata(`{` + LabelName + ` = "hello"}`)},
{"Intrinsic: duration = 100s", traceql.MustExtractFetchSpansRequestWithMetadata(`{` + LabelDuration + ` = 100s}`)},
{"Intrinsic: duration > 99s", traceql.MustExtractFetchSpansRequestWithMetadata(`{` + LabelDuration + ` > 99s}`)},
{"Intrinsic: duration >= 100s", traceql.MustExtractFetchSpansRequestWithMetadata(`{` + LabelDuration + ` >= 100s}`)},
{"Intrinsic: duration < 101s", traceql.MustExtractFetchSpansRequestWithMetadata(`{` + LabelDuration + ` < 101s}`)},
{"Intrinsic: duration <= 100s", traceql.MustExtractFetchSpansRequestWithMetadata(`{` + LabelDuration + ` <= 100s}`)},
{"Intrinsic: status = error", traceql.MustExtractFetchSpansRequestWithMetadata(`{` + LabelStatus + ` = error}`)},
{"Intrinsic: status = 2", traceql.MustExtractFetchSpansRequestWithMetadata(`{` + LabelStatus + ` = 2}`)},
{"Intrinsic: statusMessage = STATUS_CODE_ERROR", traceql.MustExtractFetchSpansRequestWithMetadata(`{` + "statusMessage" + ` = "STATUS_CODE_ERROR"}`)},
{"Intrinsic: kind = client", traceql.MustExtractFetchSpansRequestWithMetadata(`{` + LabelKind + ` = client }`)},
{"Intrinsic: trace:id", traceql.MustExtractFetchSpansRequestWithMetadata(`{ trace:id = "` + traceIDText + `" }`)},
// Resource well-known attributes
{".service.name", traceql.MustExtractFetchSpansRequestWithMetadata(`{.` + LabelServiceName + ` = "spanservicename"}`)}, // Overridden at span},
{".cluster", traceql.MustExtractFetchSpansRequestWithMetadata(`{.` + LabelCluster + ` = "cluster"}`)},
{".namespace", traceql.MustExtractFetchSpansRequestWithMetadata(`{.` + LabelNamespace + ` = "namespace"}`)},
{".pod", traceql.MustExtractFetchSpansRequestWithMetadata(`{.` + LabelPod + ` = "pod"}`)},
{".container", traceql.MustExtractFetchSpansRequestWithMetadata(`{.` + LabelContainer + ` = "container"}`)},
{".k8s.namespace.name", traceql.MustExtractFetchSpansRequestWithMetadata(`{.` + LabelK8sNamespaceName + ` = "k8snamespace"}`)},
{".k8s.cluster.name", traceql.MustExtractFetchSpansRequestWithMetadata(`{.` + LabelK8sClusterName + ` = "k8scluster"}`)},
{".k8s.pod.name", traceql.MustExtractFetchSpansRequestWithMetadata(`{.` + LabelK8sPodName + ` = "k8spod"}`)},
{".k8s.container.name", traceql.MustExtractFetchSpansRequestWithMetadata(`{.` + LabelK8sContainerName + ` = "k8scontainer"}`)},
{"resource.service.name", traceql.MustExtractFetchSpansRequestWithMetadata(`{resource.` + LabelServiceName + ` = "myservice"}`)},
{"resource.cluster", traceql.MustExtractFetchSpansRequestWithMetadata(`{resource.` + LabelCluster + ` = "cluster"}`)},
{"resource.namespace", traceql.MustExtractFetchSpansRequestWithMetadata(`{resource.` + LabelNamespace + ` = "namespace"}`)},
{"resource.pod", traceql.MustExtractFetchSpansRequestWithMetadata(`{resource.` + LabelPod + ` = "pod"}`)},
{"resource.container", traceql.MustExtractFetchSpansRequestWithMetadata(`{resource.` + LabelContainer + ` = "container"}`)},
{"resource.k8s.namespace.name", traceql.MustExtractFetchSpansRequestWithMetadata(`{resource.` + LabelK8sNamespaceName + ` = "k8snamespace"}`)},
{"resource.k8s.cluster.name", traceql.MustExtractFetchSpansRequestWithMetadata(`{resource.` + LabelK8sClusterName + ` = "k8scluster"}`)},
{"resource.k8s.pod.name", traceql.MustExtractFetchSpansRequestWithMetadata(`{resource.` + LabelK8sPodName + ` = "k8spod"}`)},
{"resource.k8s.container.name", traceql.MustExtractFetchSpansRequestWithMetadata(`{resource.` + LabelK8sContainerName + ` = "k8scontainer"}`)},
// Resource dedicated attributes
{"resource.dedicated.resource.3", traceql.MustExtractFetchSpansRequestWithMetadata(`{resource.dedicated.resource.3 = "dedicated-resource-attr-value-3"}`)},
{"resource.dedicated.resource.5", traceql.MustExtractFetchSpansRequestWithMetadata(`{resource.dedicated.resource.5 = "dedicated-resource-attr-value-5"}`)},
// Comparing strings
{"resource.service.name > myservice", traceql.MustExtractFetchSpansRequestWithMetadata(`{resource.` + LabelServiceName + ` > "myservic"}`)},
{"resource.service.name >= myservice", traceql.MustExtractFetchSpansRequestWithMetadata(`{resource.` + LabelServiceName + ` >= "myservic"}`)},
{"resource.service.name < myservice1", traceql.MustExtractFetchSpansRequestWithMetadata(`{resource.` + LabelServiceName + ` < "myservice1"}`)},
{"resource.service.name <= myservice1", traceql.MustExtractFetchSpansRequestWithMetadata(`{resource.` + LabelServiceName + ` <= "myservice1"}`)},
// Span well-known attributes
{".http.status_code", traceql.MustExtractFetchSpansRequestWithMetadata(`{.` + LabelHTTPStatusCode + ` = 500}`)},
{".http.method", traceql.MustExtractFetchSpansRequestWithMetadata(`{.` + LabelHTTPMethod + ` = "get"}`)},
{".http.url", traceql.MustExtractFetchSpansRequestWithMetadata(`{.` + LabelHTTPUrl + ` = "url/hello/world"}`)},
{"span.http.status_code", traceql.MustExtractFetchSpansRequestWithMetadata(`{span.` + LabelHTTPStatusCode + ` = 500}`)},
{"span.http.method", traceql.MustExtractFetchSpansRequestWithMetadata(`{span.` + LabelHTTPMethod + ` = "get"}`)},
{"span.http.url", traceql.MustExtractFetchSpansRequestWithMetadata(`{span.` + LabelHTTPUrl + ` = "url/hello/world"}`)},
// Span dedicated attributes
{"span.dedicated.span.2", traceql.MustExtractFetchSpansRequestWithMetadata(`{span.dedicated.span.2 = "dedicated-span-attr-value-2"}`)},
{"span.dedicated.span.4", traceql.MustExtractFetchSpansRequestWithMetadata(`{span.dedicated.span.4 = "dedicated-span-attr-value-4"}`)},
// Arrays
{"resource.str-array", traceql.MustExtractFetchSpansRequestWithMetadata(`{resource.str-array = "value-three"}`)},
{"resource.int-array", traceql.MustExtractFetchSpansRequestWithMetadata(`{resource.int-array = 11}`)},
{"span.str-array", traceql.MustExtractFetchSpansRequestWithMetadata(`{span.str-array = "value-two"}`)},
{"span.int-array", traceql.MustExtractFetchSpansRequestWithMetadata(`{span.int-array = 222}`)},
// Events
{"event:name", traceql.MustExtractFetchSpansRequestWithMetadata(`{event:name = "e1"}`)},
{"event:timeSinceStart", traceql.MustExtractFetchSpansRequestWithMetadata(`{event:timeSinceStart > 2ms}`)},
{"event.message", traceql.MustExtractFetchSpansRequestWithMetadata(`{event.message =~ "exception"}`)},
// Links
{"link:spanID", traceql.MustExtractFetchSpansRequestWithMetadata(`{link:spanID = "1234567890abcdef"}`)},
{"link:traceID", traceql.MustExtractFetchSpansRequestWithMetadata(`{link:traceID = "1234567890abcdef1234567890abcdef"}`)},
{"link.opentracing.ref_type", traceql.MustExtractFetchSpansRequestWithMetadata(`{link.opentracing.ref_type = "child-of"}`)},
// Instrumentation Scope
{"instrumentation:name", traceql.MustExtractFetchSpansRequestWithMetadata(`{instrumentation:name = "scope-1"}`)},
{"instrumentation:version", traceql.MustExtractFetchSpansRequestWithMetadata(`{instrumentation:version = "version-1"}`)},
{"instrumentation.attr-str", traceql.MustExtractFetchSpansRequestWithMetadata(`{instrumentation.scope-attr-str = "scope-attr-1"}`)},
// Basic data types and operations
{".float = 456.78", traceql.MustExtractFetchSpansRequestWithMetadata(`{.float = 456.78}`)}, // Float ==
{".float != 456.79", traceql.MustExtractFetchSpansRequestWithMetadata(`{.float != 456.79}`)}, // Float !=
{".float > 456.7", traceql.MustExtractFetchSpansRequestWithMetadata(`{.float > 456.7}`)}, // Float >
{".float >= 456.78", traceql.MustExtractFetchSpansRequestWithMetadata(`{.float >= 456.78}`)}, // Float >=
{".float < 456.781", traceql.MustExtractFetchSpansRequestWithMetadata(`{.float < 456.781}`)}, // Float <
{".bool = false", traceql.MustExtractFetchSpansRequestWithMetadata(`{.bool = false}`)}, // Bool ==
{".bool != true", traceql.MustExtractFetchSpansRequestWithMetadata(`{.bool != true}`)}, // Bool !=
{".bar = 123", traceql.MustExtractFetchSpansRequestWithMetadata(`{.bar = 123}`)}, // Int ==
{".bar != 124", traceql.MustExtractFetchSpansRequestWithMetadata(`{.bar != 124}`)}, // Int !=
{".bar > 122", traceql.MustExtractFetchSpansRequestWithMetadata(`{.bar > 122}`)}, // Int >
{".bar >= 123", traceql.MustExtractFetchSpansRequestWithMetadata(`{.bar >= 123}`)}, // Int >=
{".bar < 124", traceql.MustExtractFetchSpansRequestWithMetadata(`{.bar < 124}`)}, // Int <
{".bar <= 123", traceql.MustExtractFetchSpansRequestWithMetadata(`{.bar <= 123}`)}, // Int <=
{".foo = \"def\"", traceql.MustExtractFetchSpansRequestWithMetadata(`{.foo = "def"}`)}, // String ==
{".foo != \"deg\"", traceql.MustExtractFetchSpansRequestWithMetadata(`{.foo != "deg"}`)}, // String !=
{".foo =~ \"d.*\"", traceql.MustExtractFetchSpansRequestWithMetadata(`{.foo =~ "d.*"}`)}, // String Regex
{".foo !~ \"x.*\"", traceql.MustExtractFetchSpansRequestWithMetadata(`{.foo !~ "x.*"}`)}, // String Not Regex
{"resource.foo = \"abc\"", traceql.MustExtractFetchSpansRequestWithMetadata(`{resource.foo = "abc"}`)}, // Resource-level only
{"span.foo = \"def\"", traceql.MustExtractFetchSpansRequestWithMetadata(`{span.foo = "def"}`)}, // Span-level only
{".foo", traceql.MustExtractFetchSpansRequestWithMetadata(`{.foo}`)}, // Projection only
{"Matches either condition", makeReq(
parse(t, `{.foo = "baz"}`),
parse(t, `{.`+LabelHTTPStatusCode+` > 100}`),
)},
{"Same as above but reversed order", makeReq(
parse(t, `{.`+LabelHTTPStatusCode+` > 100}`),
parse(t, `{.foo = "baz"}`),
)},
{"Same attribute with mixed types", makeReq(
parse(t, `{.foo > 100}`),
parse(t, `{.foo = "def"}`),
)},
{"Multiple conditions on same well-known attribute, matches either", makeReq(
parse(t, `{.`+LabelHTTPStatusCode+` = 500}`),
parse(t, `{.`+LabelHTTPStatusCode+` > 500}`),
)},
{
"Mix of duration with other conditions", makeReq(
parse(t, `{`+LabelName+` = "hello"}`), // Match
parse(t, `{`+LabelDuration+` < 100s }`), // No match
),
},
// Edge cases
{"Almost conflicts with intrinsic but still works", traceql.MustExtractFetchSpansRequestWithMetadata(`{.name = "Bob"}`)},
{"service.name doesn't match type of dedicated column", traceql.MustExtractFetchSpansRequestWithMetadata(`{resource.` + LabelServiceName + ` = 123}`)},
{"service.name present on span", traceql.MustExtractFetchSpansRequestWithMetadata(`{.` + LabelServiceName + ` = "spanservicename"}`)},
{"http.status_code doesn't match type of dedicated column", traceql.MustExtractFetchSpansRequestWithMetadata(`{.` + LabelHTTPStatusCode + ` = "500ouch"}`)},
{`.foo = "def"`, traceql.MustExtractFetchSpansRequestWithMetadata(`{.foo = "def"}`)},
{
name: "Range at unscoped",
req: traceql.FetchSpansRequest{
AllConditions: true,
Conditions: []traceql.Condition{
parse(t, `{.`+LabelHTTPStatusCode+` >= 500}`),
parse(t, `{.`+LabelHTTPStatusCode+` <= 600}`),
},
},
},
{
name: "Range at span scope",
req: traceql.FetchSpansRequest{
AllConditions: true,
Conditions: []traceql.Condition{
parse(t, `{span.`+LabelHTTPStatusCode+` >= 500}`),
parse(t, `{span.`+LabelHTTPStatusCode+` <= 600}`),
},
},
},
{
name: "Range at resource scope",
req: traceql.FetchSpansRequest{
AllConditions: true,
Conditions: []traceql.Condition{
parse(t, `{resource.`+LabelServiceName+` >= 122}`),
parse(t, `{resource.`+LabelServiceName+` <= 124}`),
},
},
},
}
for _, tc := range searchesThatMatch {
t.Run(tc.name, func(t *testing.T) {
req := tc.req
if req.SecondPass == nil {
req.SecondPass = func(s *traceql.Spanset) ([]*traceql.Spanset, error) { return []*traceql.Spanset{s}, nil }
req.SecondPassConditions = traceql.SearchMetaConditions()
}
resp, err := b.Fetch(ctx, req, common.DefaultSearchOptions())
require.NoError(t, err, "search request:%v", req)
found := false
for {
spanSet, err := resp.Results.Next(ctx)
require.NoError(t, err, "search request:%v", req)
if spanSet == nil {
break
}
found = bytes.Equal(spanSet.TraceID, wantTraceID)
if found {
break
}
}
require.True(t, found, "search request:%v", req)
})
}
searchesThatDontMatch := []struct {
name string
req traceql.FetchSpansRequest
}{
// TODO - Should the below query return data or not? It does match the resource
// makeReq(parse(t, `{.foo = "abc"}`)), // This should not return results because the span has overridden this attribute to "def".
{"Regex IN", traceql.MustExtractFetchSpansRequestWithMetadata(`{.foo =~ "xyz.*"}`)},
{"String Not Regex", traceql.MustExtractFetchSpansRequestWithMetadata(`{.foo !~ ".*"}`)},
{"Bool not match", traceql.MustExtractFetchSpansRequestWithMetadata(`{span.bool = true && name = "hello"}`)}, // name = "hello" only matches the first span
{"Intrinsic: duration", traceql.MustExtractFetchSpansRequestWithMetadata(`{` + LabelDuration + ` > 1000s}`)},
{"Intrinsic: status", traceql.MustExtractFetchSpansRequestWithMetadata(`{` + LabelStatus + ` = unset}`)},
{"Intrinsic: statusMessage", traceql.MustExtractFetchSpansRequestWithMetadata(`{` + "statusMessage" + ` = "abc"}`)},
{"Intrinsic: name", traceql.MustExtractFetchSpansRequestWithMetadata(`{` + LabelName + ` = "nothello"}`)},
{"Intrinsic: kind", traceql.MustExtractFetchSpansRequestWithMetadata(`{` + LabelKind + ` = producer }`)},
{"Intrinsic: event:name", traceql.MustExtractFetchSpansRequestWithMetadata(`{event:name = "x2"}`)},
{"Intrinsic: link:spanID", traceql.MustExtractFetchSpansRequestWithMetadata(`{link:spanID = "ffffffffffffffff"}`)},
{"Intrinsic: link:traceID", traceql.MustExtractFetchSpansRequestWithMetadata(`{link:traceID = "ffffffffffffffffffffffffffffffff"}`)},
{"Well-known attribute: service.name not match", traceql.MustExtractFetchSpansRequestWithMetadata(`{.` + LabelServiceName + ` = "notmyservice"}`)},
{"Well-known attribute: http.status_code not match", traceql.MustExtractFetchSpansRequestWithMetadata(`{.` + LabelHTTPStatusCode + ` = 200}`)},
{"Well-known attribute: http.status_code not match", traceql.MustExtractFetchSpansRequestWithMetadata(`{.` + LabelHTTPStatusCode + ` > 600}`)},
{"Matches neither condition", traceql.MustExtractFetchSpansRequestWithMetadata(`{.foo = "xyz" || .` + LabelHTTPStatusCode + " = 1000}")},
{"Resource dedicated attributes does not match", traceql.MustExtractFetchSpansRequestWithMetadata(`{resource.dedicated.resource.3 = "dedicated-resource-attr-value-4"}`)},
{"Resource dedicated attributes does not match", traceql.MustExtractFetchSpansRequestWithMetadata(`{span.dedicated.span.2 = "dedicated-span-attr-value-5"}`)},
{
name: "Time range after trace",
req: traceql.FetchSpansRequest{
StartTimeUnixNanos: uint64(20000 * time.Second),
EndTimeUnixNanos: uint64(30000 * time.Second),
},
},
{
name: "Time range before trace",
req: traceql.FetchSpansRequest{
StartTimeUnixNanos: uint64(600 * time.Second),
EndTimeUnixNanos: uint64(700 * time.Second),
},
},
{
name: "Matches some conditions but not all. Mix of span-level columns",
req: traceql.FetchSpansRequest{
AllConditions: true,
Conditions: []traceql.Condition{
parse(t, `{span.foo = "baz"}`), // no match
parse(t, `{span.`+LabelHTTPStatusCode+` > 100}`), // match
parse(t, `{name = "hello"}`), // match
},
},
},
{
name: "Matches some conditions but not all. Only span generic attr lookups",
req: traceql.FetchSpansRequest{
AllConditions: true,
Conditions: []traceql.Condition{
parse(t, `{span.foo = "baz"}`), // no match
parse(t, `{span.bar = 123}`), // match
},
},
},
{
name: "Matches some conditions but not all. Mix of span and resource columns",
req: traceql.FetchSpansRequest{
AllConditions: true,
Conditions: []traceql.Condition{
parse(t, `{resource.cluster = "cluster"}`), // match
parse(t, `{resource.namespace = "namespace"}`), // match
parse(t, `{span.foo = "baz"}`), // no match
},
},
},
{
name: "Matches some conditions but not all. Mix of resource columns",
req: traceql.FetchSpansRequest{
AllConditions: true,
Conditions: []traceql.Condition{
parse(t, `{resource.cluster = "notcluster"}`), // no match
parse(t, `{resource.namespace = "namespace"}`), // match
parse(t, `{resource.foo = "abc"}`), // match
},
},
},
{
name: "Matches some conditions but not all. Only resource generic attr lookups",
req: traceql.FetchSpansRequest{
AllConditions: true,
Conditions: []traceql.Condition{
parse(t, `{resource.foo = "abc"}`), // match
parse(t, `{resource.bar = 123}`), // no match
},
},
},
{
name: "Mix of duration with other conditions",
req: traceql.FetchSpansRequest{
AllConditions: true,
Conditions: []traceql.Condition{
parse(t, `{`+LabelName+` = "nothello"}`), // No match
parse(t, `{`+LabelDuration+` = 100s }`), // Match
},
},
},
}
for _, tc := range searchesThatDontMatch {
t.Run(tc.name, func(t *testing.T) {
req := tc.req
if req.SecondPass == nil {
req.SecondPass = func(s *traceql.Spanset) ([]*traceql.Spanset, error) { return []*traceql.Spanset{s}, nil }
req.SecondPassConditions = traceql.SearchMetaConditions()
}
resp, err := b.Fetch(ctx, req, common.DefaultSearchOptions())
require.NoError(t, err, "search request:", req)
for {
spanSet, err := resp.Results.Next(ctx)
require.NoError(t, err, "search request:", req)
if spanSet == nil {
break
}
require.NotEqual(t, wantTraceID, spanSet.TraceID, "search request:", req)
}
})
}
}
func TestBackendBlockSearchTraceQLEvents(t *testing.T) {
numTraces := 50
traces := make([]*Trace, 0, numTraces)
wantTraceIdx := rand.Intn(numTraces)
wantTraceID := test.ValidTraceID(nil)
for i := 0; i < numTraces; i++ {
if i == wantTraceIdx {
// this trace has one span with two identical events
traces = append(traces, fullyPopulatedTestTrace(wantTraceID))
continue
}
id := test.ValidTraceID(nil)
tr, _ := traceToParquet(&backend.BlockMeta{}, id, test.MakeTrace(1, id), nil)
traces = append(traces, tr)
}
b := makeBackendBlockWithTraces(t, traces)
ctx := context.Background()
requests := []string{
`{event.message =~ "exception"}`,
`{event:name = "e1"}`,
`{event:timeSinceStart > 2ms}`,
}
for _, request := range requests {
t.Run(request, func(t *testing.T) {
req := traceql.MustExtractFetchSpansRequestWithMetadata(request)
if req.SecondPass == nil {
req.SecondPass = func(s *traceql.Spanset) ([]*traceql.Spanset, error) { return []*traceql.Spanset{s}, nil }
req.SecondPassConditions = traceql.SearchMetaConditions()
}
resp, err := b.Fetch(ctx, req, common.DefaultSearchOptions())
require.NoError(t, err, "search request:%v", req)
found := false
count := 0
for {
spanSet, err := resp.Results.Next(ctx)
require.NoError(t, err, "search request:%v", req)
if spanSet == nil {
break
}
found = bytes.Equal(spanSet.TraceID, wantTraceID)
if found {
count++
}
}
require.True(t, found, "search request:%v", req)
// two events in the same span should still return just one span
require.Equal(t, 1, count, "search request:%v", req)
})
}
}
func makeReq(conditions ...traceql.Condition) traceql.FetchSpansRequest {
return traceql.FetchSpansRequest{
Conditions: conditions,
SecondPass: func(s *traceql.Spanset) ([]*traceql.Spanset, error) {
return []*traceql.Spanset{s}, nil
},
SecondPassConditions: traceql.SearchMetaConditions(),
}
}
func parse(t *testing.T, q string) traceql.Condition {
req, err := traceql.ExtractFetchSpansRequest(q)
require.NoError(t, err, "query:", q)
return req.Conditions[0]
}
func fullyPopulatedTestTrace(id common.ID) *Trace {
linkTraceID, _ := util.HexStringToTraceID("1234567890abcdef1234567890abcdef")
linkSpanID, _ := util.HexStringToSpanID("1234567890abcdef")
links := []Link{
{
TraceID: linkTraceID,
SpanID: linkSpanID,
TraceState: "state",
DroppedAttributesCount: 3,
Attrs: []Attribute{
attr("opentracing.ref_type", "child-of"),
},
},
}
mixedArrayAttrValue := "{\"arrayValue\":{\"values\":[{\"stringValue\":\"value-one\"},{\"intValue\":\"100\"}]}}"
kvListValue := "{\"kvlistValue\":{\"values\":[{\"key\":\"key-one\",\"value\":{\"stringValue\":\"value-one\"}},{\"key\":\"key-two\",\"value\":{\"stringValue\":\"value-two\"}}]}}"
return &Trace{
TraceID: test.ValidTraceID(id),
TraceIDText: util.TraceIDToHexString(id),
StartTimeUnixNano: uint64(1000 * time.Second),
EndTimeUnixNano: uint64(2000 * time.Second),
DurationNano: uint64((100 * time.Millisecond).Nanoseconds()),
RootServiceName: "RootService",
RootSpanName: "RootSpan",
ServiceStats: map[string]ServiceStats{
"myservice": {
SpanCount: 1,
ErrorCount: 0,
},
"service2": {
SpanCount: 1,
ErrorCount: 0,
},
},
ResourceSpans: []ResourceSpans{
{
Resource: Resource{
ServiceName: "myservice",
Cluster: ptr("cluster"),
Namespace: ptr("namespace"),
Pod: ptr("pod"),
Container: ptr("container"),
K8sClusterName: ptr("k8scluster"),
K8sNamespaceName: ptr("k8snamespace"),
K8sPodName: ptr("k8spod"),
K8sContainerName: ptr("k8scontainer"),
Attrs: []Attribute{
attr("foo", "abc"),
attr("str-array", []string{"value-one", "value-two", "value-three", "value-four"}),
attr("int-array", []int64{11, 22, 33}),
attr(LabelServiceName, 123), // Different type than dedicated column
// Unsupported attributes
{Key: "unsupported-mixed-array", ValueUnsupported: &mixedArrayAttrValue, IsArray: false},
{Key: "unsupported-kv-list", ValueUnsupported: &kvListValue, IsArray: false},
},
DroppedAttributesCount: 22,
DedicatedAttributes: DedicatedAttributes{
String01: ptr("dedicated-resource-attr-value-1"),
String02: ptr("dedicated-resource-attr-value-2"),
String03: ptr("dedicated-resource-attr-value-3"),
String04: ptr("dedicated-resource-attr-value-4"),
String05: ptr("dedicated-resource-attr-value-5"),
},
},
ScopeSpans: []ScopeSpans{
{
Scope: InstrumentationScope{
Name: "scope-1",
Version: "version-1",
DroppedAttributesCount: 1,
Attrs: []Attribute{
attr("scope-attr-str", "scope-attr-1"),
attr("scope-attr-int", 101),
attr("scope-attr-float", 3.14),
attr("scope-attr-bool", true),
},
},
Spans: []Span{
{
SpanID: []byte("spanid"),
Name: "hello",
StartTimeUnixNano: uint64(100 * time.Second),
DurationNano: uint64(100 * time.Second),
HttpMethod: ptr("get"),
HttpUrl: ptr("url/hello/world"),
HttpStatusCode: ptr(int64(500)),
ParentSpanID: []byte{},
StatusCode: int(v1.Status_STATUS_CODE_ERROR),
StatusMessage: v1.Status_STATUS_CODE_ERROR.String(),
TraceState: "tracestate",
Kind: int(v1.Span_SPAN_KIND_CLIENT),
DroppedAttributesCount: 42,
DroppedEventsCount: 43,
Attrs: []Attribute{
attr("foo", "def"),
attr("bar", 123),
attr("float", 456.78),
attr("bool", false),
attr("str-array", []string{"value-one", "value-two"}),
attr("int-array", []int64{111, 222, 333, 444}),
attr("double-array", []float64{1.1, 2.2, 3.3}),
attr("bool-array", []bool{true, false, true, false}),
// Edge-cases
attr(LabelName, "Bob"), // Conflicts with intrinsic but still looked up by .name
attr(LabelServiceName, "spanservicename"), // Overrides resource-level dedicated column
attr(LabelHTTPStatusCode, "500ouch"), // Different type than dedicated column
// Unsupported attributes
{Key: "unsupported-mixed-array", ValueUnsupported: &mixedArrayAttrValue, IsArray: false},
{Key: "unsupported-kv-list", ValueUnsupported: &kvListValue, IsArray: false},
},
Events: []Event{
{
TimeSinceStartNano: 3 * 1000 * 1000, // 3ms
Name: "e1",
Attrs: []Attribute{
attr("event-attr-key-1", "event-value-1"),
attr("event-attr-key-2", "event-value-2"),
attr("message", "exception"),
},
},
{TimeSinceStartNano: 2, Name: "e2", Attrs: []Attribute{}},
{
TimeSinceStartNano: 3 * 1000 * 1000, // 3ms
Name: "e1",
Attrs: []Attribute{
attr("event-attr-key-1", "event-value-1"),
attr("event-attr-key-2", "event-value-2"),
attr("message", "exception"),
},
},
},
Links: links,
DedicatedAttributes: DedicatedAttributes{
String01: ptr("dedicated-span-attr-value-1"),
String02: ptr("dedicated-span-attr-value-2"),
String03: ptr("dedicated-span-attr-value-3"),
String04: ptr("dedicated-span-attr-value-4"),
String05: ptr("dedicated-span-attr-value-5"),
},
},
},
},
},
},
{
Resource: Resource{
ServiceName: "service2",
Cluster: ptr("cluster2"),
Namespace: ptr("namespace2"),
Pod: ptr("pod2"),
Container: ptr("container2"),
K8sClusterName: ptr("k8scluster2"),
K8sNamespaceName: ptr("k8snamespace2"),
K8sPodName: ptr("k8spod2"),
K8sContainerName: ptr("k8scontainer2"),
Attrs: []Attribute{
attr("foo", "abc2"),
attr(LabelServiceName, 1234), // Different type than dedicated column
},
DedicatedAttributes: DedicatedAttributes{
String01: ptr("dedicated-resource-attr-value-6"),
String02: ptr("dedicated-resource-attr-value-7"),
String03: ptr("dedicated-resource-attr-value-8"),
String04: ptr("dedicated-resource-attr-value-9"),
String05: ptr("dedicated-resource-attr-value-10"),
},
},
ScopeSpans: []ScopeSpans{
{
Scope: InstrumentationScope{
Name: "scope-2",
Version: "version-2",
Attrs: []Attribute{
attr("scope-attr-str", "scope-attr-2"),
},
},
Spans: []Span{
{
SpanID: []byte("spanid2"),
Name: "world",
StartTimeUnixNano: uint64(200 * time.Second),
DurationNano: uint64(200 * time.Second),
HttpMethod: ptr("PUT"),
HttpUrl: ptr("url/hello/world/2"),
HttpStatusCode: ptr(int64(501)),
StatusCode: int(v1.Status_STATUS_CODE_OK),
StatusMessage: v1.Status_STATUS_CODE_OK.String(),
TraceState: "tracestate2",
Kind: int(v1.Span_SPAN_KIND_SERVER),
DroppedAttributesCount: 45,
DroppedEventsCount: 46,
Attrs: []Attribute{
attr("foo", "ghi"),
attr("bar", 1234),
attr("float", 456.789),
attr("bool", true),
// Edge-cases
attr(LabelName, "Bob2"), // Conflicts with intrinsic but still looked up by .name
attr(LabelServiceName, "spanservicename2"), // Overrides resource-level dedicated column
attr(LabelHTTPStatusCode, "500ouch2"), // Different type than dedicated column
},
},
},
},
},
},
},
}
}
func TestBackendBlockSelectAll(t *testing.T) {
var (
ctx = context.Background()
numTraces = 250
traces = make([]*Trace, 0, numTraces)
wantTraceIdx = rand.Intn(numTraces)
wantTraceID = test.ValidTraceID(nil)
wantTrace = fullyPopulatedTestTrace(wantTraceID)
dc = test.MakeDedicatedColumns()
dcm = dedicatedColumnsToColumnMapping(dc)
)
// TODO - This strips unsupported attributes types for now. Revisit when
// add support for arrays/kvlists in the fetch layer.
trimForSelectAll(wantTrace)
for i := 0; i < numTraces; i++ {
if i == wantTraceIdx {
traces = append(traces, wantTrace)
continue
}
id := test.ValidTraceID(nil)
tr, _ := traceToParquet(&backend.BlockMeta{}, id, test.MakeTrace(1, id), nil)
traces = append(traces, tr)
}
b := makeBackendBlockWithTraces(t, traces)
_, _, _, req, err := traceql.Compile("{}")
require.NoError(t, err)
req.SecondPass = func(inSS *traceql.Spanset) ([]*traceql.Spanset, error) { return []*traceql.Spanset{inSS}, nil }
req.SecondPassSelectAll = true
resp, err := b.Fetch(ctx, *req, common.DefaultSearchOptions())
require.NoError(t, err)
defer resp.Results.Close()
// This is a dump of all spans in the fully-populated test trace
wantSS := flattenForSelectAll(wantTrace, dcm)
for {
// Seek to our desired trace
ss, err := resp.Results.Next(ctx)
require.NoError(t, err)
if ss == nil {
break
}
if !bytes.Equal(ss.TraceID, wantTraceID) {
continue
}
// Cleanup found data for comparison
// equal will fail on the rownum mismatches. this is an internal detail to the
// fetch layer. just wipe them out here
ss.ReleaseFn = nil
ss.ServiceStats = nil
for _, sp := range ss.Spans {
s := sp.(*span)
s.cbSpanset = nil
s.cbSpansetFinal = false
s.rowNum = parquetquery.RowNumber{}
s.startTimeUnixNanos = 0 // selectall doesn't imply start time
sortAttrs(s.traceAttrs)
sortAttrs(s.resourceAttrs)
sortAttrs(s.spanAttrs)
sortAttrs(s.instrumentationAttrs)
}
require.Equal(t, wantSS, ss)
}
}
func sortAttrs(attrs []attrVal) {
sort.SliceStable(attrs, func(i, j int) bool {
is := attrs[i].a.String()
js := attrs[j].a.String()
if is == js {
// Compare by value
return attrs[i].s.String() < attrs[j].s.String()
}
return is < js
})
}
func trimArrayAttrs(in []Attribute) []Attribute {
out := []Attribute{}
for _, a := range in {
if a.IsArray || a.ValueUnsupported != nil {
continue
}
out = append(out, a)
}
return out
}
func trimForSelectAll(tr *Trace) {
for i, rs := range tr.ResourceSpans {
tr.ResourceSpans[i].Resource.Attrs = trimArrayAttrs(rs.Resource.Attrs)
for j, ss := range rs.ScopeSpans {
for k, s := range ss.Spans {
tr.ResourceSpans[i].ScopeSpans[j].Spans[k].Attrs = trimArrayAttrs(s.Attrs)
}
}
}
}
func flattenForSelectAll(tr *Trace, dcm dedicatedColumnMapping) *traceql.Spanset {
var traceAttrs []attrVal
newSS := &traceql.Spanset{
RootServiceName: tr.RootServiceName,
RootSpanName: tr.RootSpanName,
TraceID: tr.TraceID,
DurationNanos: tr.DurationNano,
}
traceAttrs = append(traceAttrs, attrVal{traceql.IntrinsicTraceIDAttribute, traceql.NewStaticString(tr.TraceIDText)})
traceAttrs = append(traceAttrs, attrVal{traceql.IntrinsicTraceDurationAttribute, traceql.NewStaticDuration(time.Duration(tr.DurationNano))})
traceAttrs = append(traceAttrs, attrVal{traceql.IntrinsicTraceRootServiceAttribute, traceql.NewStaticString(tr.RootServiceName)})
traceAttrs = append(traceAttrs, attrVal{traceql.IntrinsicTraceRootSpanAttribute, traceql.NewStaticString(tr.RootSpanName)})
sortAttrs(traceAttrs)
for _, rs := range tr.ResourceSpans {
var rsAttrs []attrVal
rsAttrs = append(rsAttrs, attrVal{traceql.NewScopedAttribute(traceql.AttributeScopeResource, false, LabelServiceName), traceql.NewStaticString(rs.Resource.ServiceName)})
rsAttrs = append(rsAttrs, attrVal{traceql.NewScopedAttribute(traceql.AttributeScopeResource, false, LabelCluster), traceql.NewStaticString(*rs.Resource.Cluster)})
rsAttrs = append(rsAttrs, attrVal{traceql.NewScopedAttribute(traceql.AttributeScopeResource, false, LabelNamespace), traceql.NewStaticString(*rs.Resource.Namespace)})
rsAttrs = append(rsAttrs, attrVal{traceql.NewScopedAttribute(traceql.AttributeScopeResource, false, LabelPod), traceql.NewStaticString(*rs.Resource.Pod)})
rsAttrs = append(rsAttrs, attrVal{traceql.NewScopedAttribute(traceql.AttributeScopeResource, false, LabelContainer), traceql.NewStaticString(*rs.Resource.Container)})
rsAttrs = append(rsAttrs, attrVal{traceql.NewScopedAttribute(traceql.AttributeScopeResource, false, LabelK8sClusterName), traceql.NewStaticString(*rs.Resource.K8sClusterName)})
rsAttrs = append(rsAttrs, attrVal{traceql.NewScopedAttribute(traceql.AttributeScopeResource, false, LabelK8sNamespaceName), traceql.NewStaticString(*rs.Resource.K8sNamespaceName)})
rsAttrs = append(rsAttrs, attrVal{traceql.NewScopedAttribute(traceql.AttributeScopeResource, false, LabelK8sPodName), traceql.NewStaticString(*rs.Resource.K8sPodName)})
rsAttrs = append(rsAttrs, attrVal{traceql.NewScopedAttribute(traceql.AttributeScopeResource, false, LabelK8sContainerName), traceql.NewStaticString(*rs.Resource.K8sContainerName)})
for _, a := range parquetToProtoAttrs(rs.Resource.Attrs) {
if arr := a.Value.GetArrayValue(); arr != nil {
for _, v := range arr.Values {
rsAttrs = append(rsAttrs, attrVal{traceql.NewScopedAttribute(traceql.AttributeScopeResource, false, a.Key), traceql.StaticFromAnyValue(v)})
}
continue
}
rsAttrs = append(rsAttrs, attrVal{traceql.NewScopedAttribute(traceql.AttributeScopeResource, false, a.Key), traceql.StaticFromAnyValue(a.Value)})
}
dcm.forEach(func(attr string, column dedicatedColumn) {
if strings.Contains(column.ColumnPath, "Resource") {
v := column.readValue(&rs.Resource.DedicatedAttributes)
if v == nil {
return
}
a := traceql.NewScopedAttribute(traceql.AttributeScopeResource, false, attr)
s := traceql.StaticFromAnyValue(v)
rsAttrs = append(rsAttrs, attrVal{a, s})
}
})
sortAttrs(rsAttrs)
for _, ss := range rs.ScopeSpans {
var instrumentationAttrs []attrVal
instrumentationAttrs = append(instrumentationAttrs, attrVal{traceql.IntrinsicInstrumentationNameAttribute, traceql.NewStaticString(ss.Scope.Name)})
instrumentationAttrs = append(instrumentationAttrs, attrVal{traceql.IntrinsicInstrumentationVersionAttribute, traceql.NewStaticString(ss.Scope.Version)})
for _, a := range parquetToProtoAttrs(ss.Scope.Attrs) {
if arr := a.Value.GetArrayValue(); arr != nil {
for _, v := range arr.Values {
instrumentationAttrs = append(instrumentationAttrs, attrVal{traceql.NewScopedAttribute(traceql.AttributeScopeInstrumentation, false, a.Key), traceql.StaticFromAnyValue(v)})
}
continue
}
instrumentationAttrs = append(instrumentationAttrs, attrVal{traceql.NewScopedAttribute(traceql.AttributeScopeInstrumentation, false, a.Key), traceql.StaticFromAnyValue(a.Value)})
}
sortAttrs(instrumentationAttrs)
for _, s := range ss.Spans {
newS := &span{}
// newS.id = s.SpanID SpanID isn't implied by SelectAll
// newS.startTimeUnixNanos = s.StartTimeUnixNano Span StartTime isn't implied by selectAll
newS.durationNanos = s.DurationNano
newS.setTraceAttrs(traceAttrs)
newS.setResourceAttrs(rsAttrs)
newS.setInstrumentationAttrs(instrumentationAttrs)
newS.addSpanAttr(traceql.IntrinsicDurationAttribute, traceql.NewStaticDuration(time.Duration(s.DurationNano)))
newS.addSpanAttr(traceql.IntrinsicKindAttribute, traceql.NewStaticKind(otlpKindToTraceqlKind(uint64(s.Kind))))
newS.addSpanAttr(traceql.IntrinsicNameAttribute, traceql.NewStaticString(s.Name))
newS.addSpanAttr(traceql.IntrinsicStatusAttribute, traceql.NewStaticStatus(otlpStatusToTraceqlStatus(uint64(s.StatusCode))))
newS.addSpanAttr(traceql.IntrinsicStatusMessageAttribute, traceql.NewStaticString(s.StatusMessage))
if s.HttpStatusCode != nil {
newS.addSpanAttr(traceql.NewScopedAttribute(traceql.AttributeScopeSpan, false, LabelHTTPStatusCode), traceql.NewStaticInt(int(*s.HttpStatusCode)))
}
if s.HttpMethod != nil {
newS.addSpanAttr(traceql.NewScopedAttribute(traceql.AttributeScopeSpan, false, LabelHTTPMethod), traceql.NewStaticString(*s.HttpMethod))
}
if s.HttpUrl != nil {
newS.addSpanAttr(traceql.NewScopedAttribute(traceql.AttributeScopeSpan, false, LabelHTTPUrl), traceql.NewStaticString(*s.HttpUrl))
}
dcm.forEach(func(attr string, column dedicatedColumn) {
if strings.Contains(column.ColumnPath, "Span") {
v := column.readValue(&s.DedicatedAttributes)
if v == nil {
return
}
a := traceql.NewScopedAttribute(traceql.AttributeScopeSpan, false, attr)
s := traceql.StaticFromAnyValue(v)
newS.addSpanAttr(a, s)
}
})
for _, a := range parquetToProtoAttrs(s.Attrs) {
if arr := a.Value.GetArrayValue(); arr != nil {
for _, v := range arr.Values {
newS.addSpanAttr(traceql.NewScopedAttribute(traceql.AttributeScopeSpan, false, a.Key), traceql.StaticFromAnyValue(v))
}
continue
}
newS.addSpanAttr(traceql.NewScopedAttribute(traceql.AttributeScopeSpan, false, a.Key), traceql.StaticFromAnyValue(a.Value))
}
sortAttrs(newS.spanAttrs)
newSS.Spans = append(newSS.Spans, newS)
}
}
}
return newSS
}
func BenchmarkBackendBlockTraceQL(b *testing.B) {
testCases := []struct {
name string
query string
}{
// span
{"spanAttValMatch", "{ span.component = `net/http` }"},
{"spanAttValNoMatch", "{ span.bloom = `does-not-exit-6c2408325a45` }"},
{"spanAttIntrinsicMatch", "{ name = `/cortex.Ingester/Push` }"},
{"spanAttIntrinsicNoMatch", "{ name = `does-not-exit-6c2408325a45` }"},
// resource
{"resourceAttValMatch", "{ resource.opencensus.exporterversion = `Jaeger-Go-2.30.0` }"},
{"resourceAttValNoMatch", "{ resource.module.path = `does-not-exit-6c2408325a45` }"},
{"resourceAttIntrinsicMatch", "{ resource.service.name = `tempo-gateway` }"},
{"resourceAttIntrinsicMatch", "{ resource.service.name = `does-not-exit-6c2408325a45` }"},
// trace
{"traceOrMatch", "{ rootServiceName = `tempo-gateway` && (status = error || span.http.status_code = 500)}"},
{"traceOrNoMatch", "{ rootServiceName = `doesntexist` && (status = error || span.http.status_code = 500)}"},
// mixed
{"mixedValNoMatch", "{ .bloom = `does-not-exit-6c2408325a45` }"},
{"mixedValMixedMatchAnd", "{ resource.foo = `bar` && name = `gcs.ReadRange` }"},
{"mixedValMixedMatchOr", "{ resource.foo = `bar` || name = `gcs.ReadRange` }"},
{"count", "{ } | count() > 1"},
{"struct", "{ resource.service.name != `loki-querier` } >> { resource.service.name = `loki-gateway` && status = error }"},
{"||", "{ resource.service.name = `loki-querier` } || { resource.service.name = `loki-gateway` }"},
{"mixed", `{resource.namespace!="" && resource.service.name="cortex-gateway" && duration>50ms && resource.cluster=~"prod.*"}`},
{"complex", `{resource.cluster=~"prod.*" && resource.namespace = "tempo-prod" && resource.container="query-frontend" && name = "HTTP GET - tempo_api_v2_search_tags" && span.http.status_code = 200 && duration > 1s}`},
{"select", `{resource.cluster=~"prod.*" && resource.namespace = "tempo-prod"} | select(resource.container)`},
}
ctx := context.TODO()
tenantID := "1"
// blockID := uuid.MustParse("06ebd383-8d4e-4289-b0e9-cf2197d611d5")
// blockID := uuid.MustParse("0008e57d-069d-4510-a001-b9433b2da08c")
blockID := uuid.MustParse("030c8c4f-9d47-4916-aadc-26b90b1d2bc4")
r, _, _, err := local.New(&local.Config{
// Path: path.Join("/Users/marty/src/tmp"),
// Path: path.Join("/Users/mapno/workspace/testblock"),
Path: path.Join("/Users/joe/testblock"),
})
require.NoError(b, err)
rr := backend.NewReader(r)
meta, err := rr.BlockMeta(ctx, blockID, tenantID)
require.NoError(b, err)
opts := common.DefaultSearchOptions()
opts.StartPage = 3
opts.TotalPages = 2
block := newBackendBlock(meta, rr)
_, _, err = block.openForSearch(ctx, opts)
require.NoError(b, err)
for _, tc := range testCases {
b.Run(tc.name, func(b *testing.B) {
b.ResetTimer()
bytesRead := 0
for i := 0; i < b.N; i++ {
e := traceql.NewEngine()
resp, err := e.ExecuteSearch(ctx, &tempopb.SearchRequest{Query: tc.query}, traceql.NewSpansetFetcherWrapper(func(ctx context.Context, req traceql.FetchSpansRequest) (traceql.FetchSpansResponse, error) {
return block.Fetch(ctx, req, opts)
}))
require.NoError(b, err)
require.NotNil(b, resp)
// Read first 20 results (if any)
bytesRead += int(resp.Metrics.InspectedBytes)
}
b.SetBytes(int64(bytesRead) / int64(b.N))
b.ReportMetric(float64(bytesRead)/float64(b.N)/1000.0/1000.0, "MB_io/op")
})
}
}
// BenchmarkBackendBlockGetMetrics This doesn't really belong here but I can't think of
// a better place that has access to all of the packages, especially the backend.
func BenchmarkBackendBlockGetMetrics(b *testing.B) {
testCases := []struct {