-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtest_headers_precedence.py
More file actions
793 lines (686 loc) · 36.7 KB
/
test_headers_precedence.py
File metadata and controls
793 lines (686 loc) · 36.7 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
import pytest
from utils.docker_fixtures.spec.tracecontext import get_tracecontext
from utils import scenarios, features
from utils.docker_fixtures import TestAgentAPI
from .conftest import APMLibrary
parametrize = pytest.mark.parametrize
def enable_tracecontext() -> pytest.MarkDecorator:
env = {
"DD_TRACE_PROPAGATION_STYLE_EXTRACT": "tracecontext",
"DD_TRACE_PROPAGATION_STYLE_INJECT": "tracecontext",
}
return parametrize("library_env", [env])
def enable_datadog() -> pytest.MarkDecorator:
env = {
"DD_TRACE_PROPAGATION_STYLE_EXTRACT": "Datadog",
"DD_TRACE_PROPAGATION_STYLE_INJECT": "Datadog",
}
return parametrize("library_env", [env])
def enable_datadog_tracecontext() -> pytest.MarkDecorator:
env = {
"DD_TRACE_PROPAGATION_STYLE_EXTRACT": "Datadog,tracecontext",
"DD_TRACE_PROPAGATION_STYLE_INJECT": "Datadog,tracecontext",
}
return parametrize("library_env", [env])
def enable_tracecontext_datadog() -> pytest.MarkDecorator:
env = {
"DD_TRACE_PROPAGATION_STYLE_EXTRACT": "tracecontext,Datadog",
"DD_TRACE_PROPAGATION_STYLE_INJECT": "tracecontext,Datadog",
}
return parametrize("library_env", [env])
def enable_datadog_b3multi_tracecontext_extract_first_false() -> pytest.MarkDecorator:
env1 = {
"DD_TRACE_PROPAGATION_STYLE_EXTRACT": "Datadog,b3multi,tracecontext",
"DD_TRACE_PROPAGATION_STYLE_INJECT": "Datadog,b3multi,tracecontext",
}
env2 = {
"DD_TRACE_PROPAGATION_STYLE_EXTRACT": "Datadog,b3multi,tracecontext",
"DD_TRACE_PROPAGATION_STYLE_INJECT": "Datadog,b3multi,tracecontext",
"DD_TRACE_PROPAGATION_EXTRACT_FIRST": "false",
}
return parametrize("library_env", [env1, env2])
def enable_datadog_b3multi_tracecontext_extract_first_true() -> pytest.MarkDecorator:
env = {
"DD_TRACE_PROPAGATION_STYLE_EXTRACT": "Datadog,b3multi,tracecontext",
"DD_TRACE_PROPAGATION_STYLE_INJECT": "Datadog,b3multi,tracecontext",
"DD_TRACE_PROPAGATION_EXTRACT_FIRST": "true",
}
return parametrize("library_env", [env])
def enable_tracecontext_datadog_b3multi_extract_first_false() -> pytest.MarkDecorator:
env1 = {
"DD_TRACE_PROPAGATION_STYLE_EXTRACT": "tracecontext,Datadog,b3multi",
"DD_TRACE_PROPAGATION_STYLE_INJECT": "tracecontext,Datadog,b3multi",
}
env2 = {
"DD_TRACE_PROPAGATION_STYLE_EXTRACT": "tracecontext,Datadog,b3multi",
"DD_TRACE_PROPAGATION_STYLE_INJECT": "tracecontext,Datadog,b3multi",
"DD_TRACE_PROPAGATION_EXTRACT_FIRST": "false",
}
return parametrize("library_env", [env1, env2])
def enable_tracecontext_datadog_b3multi_extract_first_true() -> pytest.MarkDecorator:
env = {
"DD_TRACE_PROPAGATION_STYLE_EXTRACT": "tracecontext,Datadog,b3multi",
"DD_TRACE_PROPAGATION_STYLE_INJECT": "tracecontext,Datadog,b3multi",
"DD_TRACE_PROPAGATION_EXTRACT_FIRST": "true",
}
return parametrize("library_env", [env])
@scenarios.parametric
@features.datadog_headers_propagation
class Test_Headers_Precedence:
def test_headers_precedence_propagationstyle_legacy(
self, test_agent: TestAgentAPI, test_library: APMLibrary
) -> None:
self.test_headers_precedence_propagationstyle_datadog(test_agent, test_library)
@enable_datadog()
def test_headers_precedence_propagationstyle_datadog(self, test_library: APMLibrary) -> None:
with test_library:
# 1) No headers
headers1 = test_library.dd_make_child_span_and_get_headers([])
# 2) Only tracecontext headers
headers2 = test_library.dd_make_child_span_and_get_headers(
[("traceparent", "00-12345678901234567890123456789012-1234567890123456-01")]
)
# 3) Only tracecontext headers, includes existing tracestate
headers3 = test_library.dd_make_child_span_and_get_headers(
[("traceparent", "00-12345678901234567890123456789012-1234567890123456-01"), ("tracestate", "foo=1")],
)
# 4) Both tracecontext and Datadog headers
headers4 = test_library.dd_make_child_span_and_get_headers(
[
("traceparent", "00-12345678901234567890123456789012-1234567890123456-01"),
("tracestate", "foo=1"),
("x-datadog-trace-id", "123456789"),
("x-datadog-parent-id", "987654321"),
("x-datadog-sampling-priority", "-2"),
],
)
# 5) Only Datadog headers
headers5 = test_library.dd_make_child_span_and_get_headers(
[
("x-datadog-trace-id", "123456789"),
("x-datadog-parent-id", "987654321"),
("x-datadog-sampling-priority", "-2"),
],
)
# 6) Invalid tracecontext, valid Datadog headers
headers6 = test_library.dd_make_child_span_and_get_headers(
[
("traceparent", "00-12345678901234567890123456789012-0000000000000000-01"),
("tracestate", "foo=1"),
("x-datadog-trace-id", "123456789"),
("x-datadog-parent-id", "987654321"),
("x-datadog-sampling-priority", "-2"),
],
)
# 1) No headers
# Result: new Datadog span context
assert "x-datadog-trace-id" in headers1
assert "x-datadog-parent-id" in headers1
assert "x-datadog-sampling-priority" in headers1
# traceparent not injected
assert "traceparent" not in headers1
assert "tracestate" not in headers1
# 2) Only tracecontext headers
# Result: new Datadog span context
assert "x-datadog-trace-id" in headers2
assert "x-datadog-parent-id" in headers2
assert "x-datadog-sampling-priority" in headers2
# traceparent not injected
assert "traceparent" not in headers2
assert "tracestate" not in headers2
# 3) Only tracecontext headers, includes existing tracestate
# Result: new Datadog span context
assert "x-datadog-trace-id" in headers3
assert "x-datadog-parent-id" in headers3
assert "x-datadog-sampling-priority" in headers3
# traceparent not injected
assert "traceparent" not in headers3
assert "tracestate" not in headers3
# 4) Both tracecontext and Datadog headers
# Result: use existing Datadog span context
assert headers4["x-datadog-trace-id"] == "123456789"
assert headers4["x-datadog-parent-id"] != "987654321"
assert headers4["x-datadog-sampling-priority"] == "-2"
# traceparent not injected
assert "traceparent" not in headers4
assert "tracestate" not in headers4
# 5) Only Datadog headers
# Result: use existing Datadog span context
assert headers5["x-datadog-trace-id"] == "123456789"
assert headers5["x-datadog-parent-id"] != "987654321"
assert headers5["x-datadog-sampling-priority"] == "-2"
# traceparent not injected
assert "traceparent" not in headers5
assert "tracestate" not in headers5
# 6) Invalid tracecontext, valid Datadog headers
# Result: use existing Datadog span context
assert headers6["x-datadog-trace-id"] == "123456789"
assert headers6["x-datadog-parent-id"] != "987654321"
assert headers6["x-datadog-sampling-priority"] == "-2"
# traceparent not injected
assert "traceparent" not in headers6
assert "tracestate" not in headers6
def test_headers_precedence_propagationstyle_default_tracecontext_datadog(
self, test_agent: TestAgentAPI, test_library: APMLibrary
) -> None:
self.test_headers_precedence_propagationstyle_tracecontext_datadog(test_agent, test_library)
@enable_tracecontext_datadog()
def test_headers_precedence_propagationstyle_tracecontext_datadog(self, test_library: APMLibrary) -> None:
with test_library:
# 1) No headers
headers1 = test_library.dd_make_child_span_and_get_headers([])
# 2) Only tracecontext headers
headers2 = test_library.dd_make_child_span_and_get_headers(
[("traceparent", "00-12345678901234567890123456789012-1234567890123456-01")]
)
# 3) Only tracecontext headers, includes existing tracestate
headers3 = test_library.dd_make_child_span_and_get_headers(
[("traceparent", "00-12345678901234567890123456789012-1234567890123456-01"), ("tracestate", "foo=1")]
)
# 4) Both tracecontext and Datadog headers
headers4 = test_library.dd_make_child_span_and_get_headers(
[
("traceparent", "00-12345678901234567890123456789012-1234567890123456-01"),
("tracestate", "foo=1"),
("x-datadog-trace-id", "123456789"),
("x-datadog-parent-id", "987654321"),
("x-datadog-sampling-priority", "-2"),
],
)
# 5) Only Datadog headers
headers5 = test_library.dd_make_child_span_and_get_headers(
[
("x-datadog-trace-id", "123456789"),
("x-datadog-parent-id", "987654321"),
("x-datadog-sampling-priority", "-2"),
],
)
# 6) Invalid tracecontext, valid Datadog headers
headers6 = test_library.dd_make_child_span_and_get_headers(
[
("traceparent", "00-12345678901234567890123456789012-0000000000000000-01"),
("tracestate", "foo=1"),
("x-datadog-trace-id", "123456789"),
("x-datadog-parent-id", "987654321"),
("x-datadog-sampling-priority", "-2"),
],
)
# 1) No headers
# Result: new Datadog span context
assert "x-datadog-trace-id" in headers1
assert "x-datadog-parent-id" in headers1
assert "x-datadog-sampling-priority" in headers1
# traceparent also injected, assert that they are equal to Datadog values
traceparent1, tracestate1 = get_tracecontext(headers1)
tracestate_1_arr = str(tracestate1).split(",")
assert "traceparent" in headers1
assert int(traceparent1.trace_id[-16:], base=16) == int(headers1["x-datadog-trace-id"])
assert int(traceparent1.parent_id, base=16) == int(headers1["x-datadog-parent-id"])
assert "tracestate" in headers1
assert len(tracestate_1_arr) == 1
assert tracestate_1_arr[0].startswith("dd=")
# 2) Only tracecontext headers
# Result: traceparent used
traceparent2, tracestate2 = get_tracecontext(headers2)
tracestate_2_arr = str(tracestate2).split(",")
assert "traceparent" in headers2
assert traceparent2.trace_id == "12345678901234567890123456789012"
assert traceparent2.parent_id != "1234567890123456"
assert "tracestate" in headers2
assert len(tracestate_2_arr) == 1
assert tracestate_2_arr[0].startswith("dd=")
# Datadog also injected, assert that they are equal to traceparent values
assert int(headers2["x-datadog-trace-id"]) == int(traceparent2.trace_id[16:], base=16)
assert int(headers2["x-datadog-parent-id"]) == int(traceparent2.parent_id, base=16)
assert "x-datadog-sampling-priority" in headers2
# 3) Only tracecontext headers, includes existing tracestate
# Result: traceparent used
traceparent3, tracestate3 = get_tracecontext(headers3)
tracestate_3_arr = str(tracestate3).split(",")
assert "traceparent" in headers3
assert traceparent3.trace_id == "12345678901234567890123456789012"
assert traceparent3.parent_id != "1234567890123456"
assert "tracestate" in headers3
assert len(tracestate_3_arr) == 2
assert tracestate_3_arr[0].startswith("dd=")
assert tracestate_3_arr[1] == "foo=1"
# Datadog also injected, assert that they are equal to traceparent values
assert int(headers3["x-datadog-trace-id"]) == int(traceparent3.trace_id[16:], base=16)
assert int(headers3["x-datadog-parent-id"]) == int(traceparent3.parent_id, base=16)
assert "x-datadog-sampling-priority" in headers3
# 4) Both tracecontext and Datadog headers
# Result: traceparent used
traceparent4, tracestate4 = get_tracecontext(headers4)
tracestate_4_arr = str(tracestate4).split(",")
assert "traceparent" in headers4
assert traceparent4.trace_id == "12345678901234567890123456789012"
assert traceparent4.parent_id != "1234567890123456"
assert "tracestate" in headers4
assert len(tracestate_4_arr) == 2
assert tracestate_4_arr[0].startswith("dd=")
assert tracestate_4_arr[1] == "foo=1"
# Datadog also injected, assert that they are equal to traceparent values
assert int(headers4["x-datadog-trace-id"]) == int(traceparent4.trace_id[16:], base=16)
assert int(headers4["x-datadog-parent-id"]) == int(traceparent4.parent_id, base=16)
assert headers4["x-datadog-sampling-priority"] != -2
# 5) Only Datadog headers
# Result: Datadog used
assert headers5["x-datadog-trace-id"] == "123456789"
assert headers5["x-datadog-parent-id"] != "987654321"
assert headers5["x-datadog-sampling-priority"] == "-2"
# traceparent also injected, assert that they are equal to Datadog values
traceparent5, tracestate5 = get_tracecontext(headers5)
tracestate_5_arr = str(tracestate5).split(",")
assert "traceparent" in headers5
assert int(traceparent5.trace_id, base=16) == int(headers5["x-datadog-trace-id"])
assert int(traceparent5.parent_id, base=16) == int(headers5["x-datadog-parent-id"])
assert "tracestate" in headers5
assert len(tracestate_5_arr) == 1
assert tracestate_5_arr[0].startswith("dd=")
# 6) Invalid tracecontext, valid Datadog headers
# Result: Datadog used
assert headers6["x-datadog-trace-id"] == "123456789"
assert headers6["x-datadog-parent-id"] != "987654321"
assert headers6["x-datadog-sampling-priority"] == "-2"
# traceparent also injected, assert that they are equal to Datadog values
traceparent6, tracestate6 = get_tracecontext(headers6)
tracestate_6_arr = str(tracestate6).split(",")
assert "traceparent" in headers6
assert int(traceparent6.trace_id, base=16) == int(headers6["x-datadog-trace-id"])
assert int(traceparent6.parent_id, base=16) == int(headers6["x-datadog-parent-id"])
assert "tracestate" in headers6
assert len(tracestate_6_arr) == 1
assert tracestate_6_arr[0].startswith("dd=")
@enable_tracecontext()
def test_headers_precedence_propagationstyle_tracecontext(self, test_library: APMLibrary) -> None:
with test_library:
# 1) No headers
headers1 = test_library.dd_make_child_span_and_get_headers([])
# 2) Only tracecontext headers
headers2 = test_library.dd_make_child_span_and_get_headers(
[("traceparent", "00-12345678901234567890123456789012-1234567890123456-01")]
)
# 3) Only tracecontext headers, includes existing tracestate
headers3 = test_library.dd_make_child_span_and_get_headers(
[("traceparent", "00-12345678901234567890123456789012-1234567890123456-01"), ("tracestate", "foo=1")]
)
# 4) Both tracecontext and Datadog headers
headers4 = test_library.dd_make_child_span_and_get_headers(
[
("traceparent", "00-12345678901234567890123456789012-1234567890123456-01"),
("tracestate", "foo=1"),
("x-datadog-trace-id", "123456789"),
("x-datadog-parent-id", "987654321"),
("x-datadog-sampling-priority", "-2"),
],
)
# 5) Only Datadog headers
headers5 = test_library.dd_make_child_span_and_get_headers(
[
("x-datadog-trace-id", "123456789"),
("x-datadog-parent-id", "987654321"),
("x-datadog-sampling-priority", "-2"),
],
)
# 6) Invalid tracecontext, valid Datadog headers
headers6 = test_library.dd_make_child_span_and_get_headers(
[
("traceparent", "00-12345678901234567890123456789012-0000000000000000-01"),
("tracestate", "foo=1"),
("x-datadog-trace-id", "123456789"),
("x-datadog-parent-id", "987654321"),
("x-datadog-sampling-priority", "-2"),
],
)
# 1) No headers
# Result: new Datadog span context, tracestate updated with `dd` key
tracestate_1_arr = headers1["tracestate"].split(",")
assert "traceparent" in headers1
assert "tracestate" in headers1
assert len(tracestate_1_arr) == 1
assert tracestate_1_arr[0].startswith("dd=")
assert "x-datadog-trace-id" not in headers1
assert "x-datadog-parent-id" not in headers1
assert "x-datadog-sampling-priority" not in headers1
# 2) Only tracecontext headers
# Result: traceparent used, tracestate updated with `dd` key
traceparent2, tracestate2 = get_tracecontext(headers2)
tracestate_2_arr = str(tracestate2).split(",")
assert "traceparent" in headers2
assert traceparent2.trace_id == "12345678901234567890123456789012"
assert traceparent2.parent_id != "1234567890123456"
assert "tracestate" in headers2
assert len(tracestate_2_arr)
assert tracestate_2_arr[0].startswith("dd=")
assert "x-datadog-trace-id" not in headers2
assert "x-datadog-parent-id" not in headers2
assert "x-datadog-sampling-priority" not in headers2
# 3) Only tracecontext headers, includes existing tracestate
# Result: traceparent used, tracestate updated with `dd` key
traceparent3, tracestate3 = get_tracecontext(headers3)
tracestate_3_arr = str(tracestate3).split(",")
assert "traceparent" in headers3
assert traceparent3.trace_id == "12345678901234567890123456789012"
assert traceparent3.parent_id != "1234567890123456"
assert "tracestate" in headers3
assert len(tracestate_3_arr) == 2
assert tracestate_3_arr[0].startswith("dd=")
assert tracestate_3_arr[1] == "foo=1"
assert "x-datadog-trace-id" not in headers3
assert "x-datadog-parent-id" not in headers3
assert "x-datadog-sampling-priority" not in headers3
# 4) Both tracecontext and Datadog headers
# Result: traceparent used, tracestate updated with `dd` key
traceparent4, tracestate4 = get_tracecontext(headers4)
tracestate_4_arr = str(tracestate4).split(",")
assert "traceparent" in headers4
assert traceparent4.trace_id == "12345678901234567890123456789012"
assert traceparent4.parent_id != "1234567890123456"
assert "tracestate" in headers4
assert len(tracestate_4_arr) == 2
assert tracestate_4_arr[0].startswith("dd=")
assert tracestate_4_arr[1] == "foo=1"
assert "x-datadog-trace-id" not in headers4
assert "x-datadog-parent-id" not in headers4
assert "x-datadog-sampling-priority" not in headers4
# 5) Only Datadog headers
# Result: new Datadog span context, tracestate updated with `dd` key
tracestate_5_arr = headers5["tracestate"].split(",")
assert "traceparent" in headers5
assert "tracestate" in headers5
assert len(tracestate_5_arr) == 1
assert tracestate_5_arr[0].startswith("dd=")
assert "x-datadog-trace-id" not in headers5
assert "x-datadog-parent-id" not in headers5
assert "x-datadog-sampling-priority" not in headers5
# 6) Invalid tracecontext, valid Datadog headers
# Result: new Datadog span context, tracestate updated with `dd` key
tracestate_6_arr = headers6["tracestate"].split(",")
assert "traceparent" in headers6
assert "tracestate" in headers6
assert len(tracestate_6_arr) == 1
assert tracestate_6_arr[0].startswith("dd=")
assert "x-datadog-trace-id" not in headers6
assert "x-datadog-parent-id" not in headers6
assert "x-datadog-sampling-priority" not in headers6
def test_headers_precedence_propagationstyle_default_datadog_tracecontext(self, test_library: APMLibrary) -> None:
self.test_headers_precedence_propagationstyle_datadog_tracecontext(test_library)
@enable_datadog_tracecontext()
def test_headers_precedence_propagationstyle_datadog_tracecontext(self, test_library: APMLibrary) -> None:
with test_library:
# 1) No headers
headers1 = test_library.dd_make_child_span_and_get_headers([])
# 2) Only tracecontext headers
headers2 = test_library.dd_make_child_span_and_get_headers(
[("traceparent", "00-12345678901234567890123456789012-1234567890123456-01")]
)
# 3) Only tracecontext headers, includes existing tracestate
headers3 = test_library.dd_make_child_span_and_get_headers(
[("traceparent", "00-12345678901234567890123456789012-1234567890123456-01"), ("tracestate", "foo=1")]
)
# 4) Both tracecontext and Datadog headers
headers4 = test_library.dd_make_child_span_and_get_headers(
[
("traceparent", "00-12345678901234567890123456789012-1234567890123456-01"),
("tracestate", "foo=1"),
("x-datadog-trace-id", "123456789"),
("x-datadog-parent-id", "987654321"),
("x-datadog-sampling-priority", "-2"),
],
)
# 5) Only Datadog headers
headers5 = test_library.dd_make_child_span_and_get_headers(
[
("x-datadog-trace-id", "123456789"),
("x-datadog-parent-id", "987654321"),
("x-datadog-sampling-priority", "-2"),
],
)
# 6) Invalid tracecontext, valid Datadog headers
headers6 = test_library.dd_make_child_span_and_get_headers(
[
("traceparent", "00-12345678901234567890123456789012-0000000000000000-01"),
("tracestate", "foo=1"),
("x-datadog-trace-id", "123456789"),
("x-datadog-parent-id", "987654321"),
("x-datadog-sampling-priority", "-2"),
],
)
# 1) No headers
# Result: new Datadog span context
assert "x-datadog-trace-id" in headers1
assert "x-datadog-parent-id" in headers1
assert "x-datadog-sampling-priority" in headers1
# traceparent also injected, assert that they are equal to Datadog values
traceparent1, tracestate1 = get_tracecontext(headers1)
tracestate_1_arr = str(tracestate1).split(",")
assert "traceparent" in headers1
assert int(traceparent1.trace_id[-16:], base=16) == int(headers1["x-datadog-trace-id"])
assert int(traceparent1.parent_id, base=16) == int(headers1["x-datadog-parent-id"])
assert "tracestate" in headers1
assert len(tracestate_1_arr) == 1
assert tracestate_1_arr[0].startswith("dd=")
# 2) Only tracecontext headers
# Result: traceparent used
traceparent2, tracestate2 = get_tracecontext(headers2)
tracestate_2_arr = str(tracestate2).split(",")
assert "traceparent" in headers2
assert traceparent2.trace_id == "12345678901234567890123456789012"
assert traceparent2.parent_id != "1234567890123456"
assert "tracestate" in headers2
assert len(tracestate_2_arr) == 1
assert tracestate_2_arr[0].startswith("dd=")
# Datadog also injected, assert that they are equal to traceparent values
assert int(headers2["x-datadog-trace-id"]) == int(traceparent2.trace_id[16:], base=16)
assert int(headers2["x-datadog-parent-id"]) == int(traceparent2.parent_id, base=16)
assert "x-datadog-sampling-priority" in headers2
# 3) Only tracecontext headers, includes existing tracestate
# Result: traceparent used
traceparent3, tracestate3 = get_tracecontext(headers3)
tracestate_3_arr = str(tracestate3).split(",")
assert "traceparent" in headers3
assert traceparent3.trace_id == "12345678901234567890123456789012"
assert traceparent3.parent_id != "1234567890123456"
assert "tracestate" in headers3
assert len(tracestate_3_arr) == 2
assert tracestate_3_arr[0].startswith("dd=")
assert tracestate_3_arr[1] == "foo=1"
# Datadog also injected, assert that they are equal to traceparent values
assert int(headers3["x-datadog-trace-id"]) == int(traceparent3.trace_id[16:], base=16)
assert int(headers3["x-datadog-parent-id"]) == int(traceparent3.parent_id, base=16)
assert "x-datadog-sampling-priority" in headers3
# 4) Both tracecontext and Datadog headers
# Result: Datadog used
assert headers4["x-datadog-trace-id"] == "123456789"
assert headers4["x-datadog-parent-id"] != "987654321"
assert headers4["x-datadog-sampling-priority"] == "-2"
# traceparent also injected, assert that they are equal to Datadog values
traceparent4, tracestate4 = get_tracecontext(headers4)
tracestate_4_arr = str(tracestate4).split(",")
assert "traceparent" in headers4
assert int(traceparent4.trace_id, base=16) == int(headers4["x-datadog-trace-id"])
assert int(traceparent4.parent_id, base=16) == int(headers4["x-datadog-parent-id"])
assert "tracestate" in headers4
assert len(tracestate_4_arr) == 1
assert tracestate_4_arr[0].startswith("dd=")
# 5) Only Datadog headers
# Result: Datadog used
assert headers5["x-datadog-trace-id"] == "123456789"
assert headers5["x-datadog-parent-id"] != "987654321"
assert headers5["x-datadog-sampling-priority"] == "-2"
# traceparent also injected, assert that they are equal to Datadog values
traceparent5, tracestate5 = get_tracecontext(headers5)
tracestate_5_arr = str(tracestate5).split(",")
assert "traceparent" in headers5
assert int(traceparent5.trace_id, base=16) == int(headers5["x-datadog-trace-id"])
assert int(traceparent5.parent_id, base=16) == int(headers5["x-datadog-parent-id"])
assert "tracestate" in headers5
assert len(tracestate_5_arr) == 1
assert tracestate_5_arr[0].startswith("dd=")
# 6) Invalid tracecontext, valid Datadog headers
# Result: Datadog used
assert headers6["x-datadog-trace-id"] == "123456789"
assert headers6["x-datadog-parent-id"] != "987654321"
assert headers6["x-datadog-sampling-priority"] == "-2"
# traceparent also injected, assert that they are equal to Datadog values
traceparent6, tracestate6 = get_tracecontext(headers6)
tracestate_6_arr = str(tracestate6).split(",")
assert "traceparent" in headers6
assert int(traceparent6.trace_id, base=16) == int(headers6["x-datadog-trace-id"])
assert int(traceparent6.parent_id, base=16) == int(headers6["x-datadog-parent-id"])
assert "tracestate" in headers6
assert len(tracestate_6_arr) == 1
assert tracestate_6_arr[0].startswith("dd=")
@enable_datadog_b3multi_tracecontext_extract_first_false()
def test_headers_precedence_propagationstyle_tracecontext_last_extract_first_false_correctly_propagates_tracestate(
self, test_agent: TestAgentAPI, test_library: APMLibrary
) -> None:
self._test_headers_precedence_propagationstyle_includes_tracecontext_correctly_propagates_tracestate(
test_agent, test_library, prefer_tracecontext=False, extract_first=False
)
@enable_datadog_b3multi_tracecontext_extract_first_true()
def test_headers_precedence_propagationstyle_tracecontext_last_extract_first_true_correctly_propagates_tracestate(
self, test_agent: TestAgentAPI, test_library: APMLibrary
) -> None:
self._test_headers_precedence_propagationstyle_includes_tracecontext_correctly_propagates_tracestate(
test_agent, test_library, prefer_tracecontext=False, extract_first=True
)
@enable_tracecontext_datadog_b3multi_extract_first_false()
def test_headers_precedence_propagationstyle_tracecontext_first_extract_first_false_correctly_propagates_tracestate(
self, test_agent: TestAgentAPI, test_library: APMLibrary
) -> None:
self._test_headers_precedence_propagationstyle_includes_tracecontext_correctly_propagates_tracestate(
test_agent, test_library, prefer_tracecontext=True, extract_first=False
)
@enable_tracecontext_datadog_b3multi_extract_first_true()
def test_headers_precedence_propagationstyle_tracecontext_first_extract_first_true_correctly_propagates_tracestate(
self, test_agent: TestAgentAPI, test_library: APMLibrary
) -> None:
self._test_headers_precedence_propagationstyle_includes_tracecontext_correctly_propagates_tracestate(
test_agent, test_library, prefer_tracecontext=True, extract_first=True
)
def _test_headers_precedence_propagationstyle_includes_tracecontext_correctly_propagates_tracestate(
self, test_agent: TestAgentAPI, test_library: APMLibrary, *, prefer_tracecontext: bool, extract_first: bool
) -> None:
"""This test asserts that ALL the propagators are executed in the specified
order, and the the first propagator to extract a valid trace context determines
the trace-id, parent-id, and supplemental information such as
x-datadog-sampling-priority, x-datadog-tags, tracestate, etc.
However, one exception is this: If the tracecontext propagator is configured,
even if it is not the first propagator to extract the trace context, the
tracestate will be saved in the local trace context if the traceparent
trace-id matches the extracted the trace-id.
"""
with test_library:
# 1) Datadog and tracecontext headers, trace-id and span-id match, tracestate is present
# Note: This is expected to be the most frequent case
headers1 = test_library.dd_make_child_span_and_get_headers(
[
("traceparent", "00-11111111111111110000000000000001-000000003ade68b1-01"),
("tracestate", "dd=s:2;t.tid:1111111111111111,foo=1"),
("x-datadog-trace-id", "1"),
("x-datadog-parent-id", "987654321"),
("x-datadog-sampling-priority", "2"),
("x-datadog-tags", "_dd.p.tid=1111111111111111"),
],
)
# 2) Scenario 1 but the x-datadog-* headers don't match the tracestate string
# Note: This is an exceptional case that should not happen, but we should be consistent
headers2 = test_library.dd_make_child_span_and_get_headers(
[
("traceparent", "00-11111111111111110000000000000002-000000003ade68b1-01"),
("tracestate", "dd=s:1;t.tid:1111111111111111,foo=1"),
("x-datadog-trace-id", "2"),
("x-datadog-parent-id", "987654321"),
("x-datadog-sampling-priority", "2"),
("x-datadog-tags", "_dd.p.tid=1111111111111111"),
],
)
# 3) Scenario 1 but there is no dd tracestate list-member
# Note: This is an exceptional case that should not happen, but we should be consistent
headers3 = test_library.dd_make_child_span_and_get_headers(
[
("traceparent", "00-11111111111111110000000000000003-000000003ade68b1-01"),
("tracestate", "foo=1"),
("x-datadog-trace-id", "3"),
("x-datadog-parent-id", "987654321"),
("x-datadog-sampling-priority", "2"),
("x-datadog-tags", "_dd.p.tid=1111111111111111"),
],
)
# 4) Datadog and tracecontext headers, trace-id is the same but span-id is different, tracestate is present
# Note: This happens when a W3C Proxy / Cloud Provider continues the W3C trace
headers4 = test_library.dd_make_child_span_and_get_headers(
[
("traceparent", "00-11111111111111110000000000000004-000000003ade68b1-01"),
("tracestate", "dd=s:2;t.tid:1111111111111111,foo=1"),
("x-datadog-trace-id", "4"),
("x-datadog-parent-id", "3540"), # 3539 == 0xdd4
("x-datadog-sampling-priority", "2"),
("x-datadog-tags", "_dd.p.tid=1111111111111111"),
],
)
# 5) Datadog and tracecontext headers, trace-id is different, tracestate is present
# Note: This happens when a W3C Proxy / Cloud Provider starts a new W3C trace,
# which would happen if the incoming request only had x-datadog-* headers
headers5 = test_library.dd_make_child_span_and_get_headers(
[
("traceparent", "00-11111111111111110000000000000005-000000003ade68b1-01"),
("tracestate", "foo=1"),
("x-datadog-trace-id", "3541"), # 3538 == 0xdd5
("x-datadog-parent-id", "987654321"),
("x-datadog-sampling-priority", "2"),
("x-datadog-tags", "_dd.p.tid=1111111111111111"),
],
)
test_agent.wait_for_num_traces(num=5)
# 1) Datadog and tracecontext headers, trace-id and span-id match, tracestate is present
# Note: This is expected to be the most frequent case
traceparent1, tracestate1 = get_tracecontext(headers1)
assert traceparent1.trace_id == "11111111111111110000000000000001"
if extract_first and not prefer_tracecontext:
assert "foo" not in tracestate1
else:
assert tracestate1["foo"] == "1"
# 2) Scenario 1 but the x-datadog-tags mismatch somehow
# Note: This is an exceptional case that should not happen, but we should be consistent
traceparent2, tracestate2 = get_tracecontext(headers2)
assert traceparent2.trace_id == "11111111111111110000000000000002"
if extract_first and not prefer_tracecontext:
assert "foo" not in tracestate2
else:
assert tracestate2["foo"] == "1"
if prefer_tracecontext:
assert "s:2" not in tracestate2["dd"]
else:
assert "s:2" in tracestate2["dd"]
# 3) Scenario 1 but there is no dd tracestate list-member
# Note: This is an exceptional case that should not happen, but we should be consistent
traceparent3, tracestate3 = get_tracecontext(headers3)
assert traceparent3.trace_id == "11111111111111110000000000000003"
if extract_first and not prefer_tracecontext:
assert "foo" not in tracestate3
else:
assert tracestate3["foo"] == "1"
if prefer_tracecontext:
assert "s:2" not in tracestate3["dd"]
else:
assert "s:2" in tracestate3["dd"]
# 4) Datadog and tracecontext headers, trace-id is the same but span-id is different, tracestate is present
# Note: This happens when a W3C Proxy / Cloud Provider continues the W3C trace
traceparent4, tracestate4 = get_tracecontext(headers4)
assert traceparent4.trace_id == "11111111111111110000000000000004"
if extract_first and not prefer_tracecontext:
assert "foo" not in tracestate4
else:
assert tracestate4["foo"] == "1"
# 5) Datadog and tracecontext headers, trace-id is different, tracestate is present
# Note: This happens when a W3C Proxy / Cloud Provider starts a new W3C trace,
# which would happen if the incoming request only had x-datadog-* headers
traceparent5, tracestate5 = get_tracecontext(headers5)
if prefer_tracecontext:
assert traceparent5.trace_id == "11111111111111110000000000000005"
assert tracestate5["foo"] == "1"
else:
assert traceparent5.trace_id == "11111111111111110000000000000dd5"
assert "foo" not in tracestate5