-
Notifications
You must be signed in to change notification settings - Fork 608
Expand file tree
/
Copy pathtest_span_streaming.py
More file actions
1472 lines (1115 loc) · 43.1 KB
/
test_span_streaming.py
File metadata and controls
1472 lines (1115 loc) · 43.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
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
import asyncio
import re
import sys
import time
from typing import Any
from unittest import mock
import pytest
import sentry_sdk
from sentry_sdk.profiler.continuous_profiler import get_profiler_id
from sentry_sdk.traces import NoOpStreamedSpan, SpanStatus, StreamedSpan
minimum_python_38 = pytest.mark.skipif(
sys.version_info < (3, 8), reason="Asyncio tests need Python >= 3.8"
)
def envelopes_to_spans(envelopes):
res: "list[dict[str, Any]]" = []
for envelope in envelopes:
for item in envelope.items:
if item.type == "span":
for span_json in item.payload.json["items"]:
span = {
"start_timestamp": span_json["start_timestamp"],
"end_timestamp": span_json.get("end_timestamp"),
"trace_id": span_json["trace_id"],
"span_id": span_json["span_id"],
"name": span_json["name"],
"status": span_json["status"],
"is_segment": span_json["is_segment"],
"parent_span_id": span_json.get("parent_span_id"),
"attributes": {
k: v["value"] for (k, v) in span_json["attributes"].items()
},
}
res.append(span)
return res
def test_start_span(sentry_init, capture_envelopes):
sentry_init(
traces_sample_rate=1.0,
_experiments={"trace_lifecycle": "stream"},
)
events = capture_envelopes()
with sentry_sdk.traces.start_span(name="segment") as segment:
assert segment._is_segment() is True
with sentry_sdk.traces.start_span(name="child") as child:
assert child._is_segment() is False
assert child._segment == segment
sentry_sdk.get_client().flush()
spans = envelopes_to_spans(events)
assert len(spans) == 2
child, segment = spans
assert segment["name"] == "segment"
assert segment["attributes"]["sentry.segment.name"] == "segment"
assert child["name"] == "child"
assert child["attributes"]["sentry.segment.name"] == "segment"
assert segment["is_segment"] is True
assert segment["parent_span_id"] is None
assert child["is_segment"] is False
assert child["parent_span_id"] == segment["span_id"]
assert child["trace_id"] == segment["trace_id"]
assert segment["start_timestamp"] is not None
assert child["start_timestamp"] is not None
assert segment["end_timestamp"] is not None
assert child["end_timestamp"] is not None
assert child["status"] == "ok"
assert segment["status"] == "ok"
def test_start_span_no_context_manager(sentry_init, capture_envelopes):
sentry_init(
traces_sample_rate=1.0,
_experiments={"trace_lifecycle": "stream"},
)
events = capture_envelopes()
segment = sentry_sdk.traces.start_span(name="segment")
child = sentry_sdk.traces.start_span(name="child")
assert child._segment == segment
child.end()
segment.end()
sentry_sdk.get_client().flush()
spans = envelopes_to_spans(events)
assert len(spans) == 2
child, segment = spans
assert segment["name"] == "segment"
assert segment["attributes"]["sentry.segment.name"] == "segment"
assert child["name"] == "child"
assert child["attributes"]["sentry.segment.name"] == "segment"
assert segment["is_segment"] is True
assert child["is_segment"] is False
assert child["parent_span_id"] == segment["span_id"]
assert child["trace_id"] == segment["trace_id"]
assert segment["start_timestamp"] is not None
assert child["start_timestamp"] is not None
assert segment["end_timestamp"] is not None
assert child["end_timestamp"] is not None
assert child["status"] == "ok"
assert segment["status"] == "ok"
def test_span_sampled_when_created(sentry_init, capture_envelopes):
# Test that if a span is created without the context manager, it is sampled
# at start_span() time
def traces_sampler(sampling_context):
assert "delayed_attribute" not in sampling_context["span_context"]["attributes"]
return 1.0
sentry_init(
traces_sampler=traces_sampler,
_experiments={"trace_lifecycle": "stream"},
)
events = capture_envelopes()
segment = sentry_sdk.traces.start_span(name="segment")
segment.set_attribute("delayed_attribute", 12)
segment.end()
sentry_sdk.get_client().flush()
spans = envelopes_to_spans(events)
assert len(spans) == 1
(segment,) = spans
assert segment["name"] == "segment"
assert segment["attributes"]["delayed_attribute"] == 12
def test_start_span_attributes(sentry_init, capture_envelopes):
sentry_init(
traces_sample_rate=1.0,
_experiments={"trace_lifecycle": "stream"},
)
events = capture_envelopes()
with sentry_sdk.traces.start_span(
name="segment", attributes={"my_attribute": "my_value"}
):
...
sentry_sdk.get_client().flush()
spans = envelopes_to_spans(events)
assert len(spans) == 1
(span,) = spans
assert span["name"] == "segment"
assert span["attributes"]["my_attribute"] == "my_value"
def test_start_span_attributes_in_traces_sampler(sentry_init, capture_envelopes):
def traces_sampler(sampling_context):
assert "attributes" in sampling_context["span_context"]
assert "my_attribute" in sampling_context["span_context"]["attributes"]
assert (
sampling_context["span_context"]["attributes"]["my_attribute"] == "my_value"
)
return 1.0
sentry_init(
traces_sampler=traces_sampler,
_experiments={"trace_lifecycle": "stream"},
)
events = capture_envelopes()
with sentry_sdk.traces.start_span(
name="segment", attributes={"my_attribute": "my_value"}
):
...
sentry_sdk.get_client().flush()
spans = envelopes_to_spans(events)
assert len(spans) == 1
(span,) = spans
assert span["name"] == "segment"
assert span["attributes"]["my_attribute"] == "my_value"
def test_sampling_context(sentry_init, capture_envelopes):
received_trace_id = None
def traces_sampler(sampling_context):
nonlocal received_trace_id
assert "trace_id" in sampling_context["span_context"]
received_trace_id = sampling_context["span_context"]["trace_id"]
assert "parent_span_id" in sampling_context["span_context"]
assert sampling_context["span_context"]["parent_span_id"] is None
assert "parent_sampled" in sampling_context["span_context"]
assert sampling_context["span_context"]["parent_sampled"] is None
assert "attributes" in sampling_context["span_context"]
return 1.0
sentry_init(
traces_sampler=traces_sampler,
_experiments={"trace_lifecycle": "stream"},
)
events = capture_envelopes()
with sentry_sdk.traces.start_span(name="span") as span:
trace_id = span._trace_id
assert received_trace_id == trace_id
sentry_sdk.get_client().flush()
spans = envelopes_to_spans(events)
assert len(spans) == 1
def test_custom_sampling_context(sentry_init):
class MyClass: ...
my_class = MyClass()
def traces_sampler(sampling_context):
assert "class" in sampling_context
assert "string" in sampling_context
assert sampling_context["class"] == my_class
assert sampling_context["string"] == "my string"
return 1.0
sentry_init(
traces_sampler=traces_sampler,
_experiments={"trace_lifecycle": "stream"},
)
sentry_sdk.get_current_scope().set_custom_sampling_context(
{
"class": my_class,
"string": "my string",
}
)
with sentry_sdk.traces.start_span(name="span"):
...
def test_custom_sampling_context_update_to_context_value_persists(sentry_init):
def traces_sampler(sampling_context):
if sampling_context["span_context"]["attributes"]["first"] is True:
assert sampling_context["custom_value"] == 1
else:
assert sampling_context["custom_value"] == 2
return 1.0
sentry_init(
traces_sampler=traces_sampler,
_experiments={"trace_lifecycle": "stream"},
)
sentry_sdk.traces.new_trace()
sentry_sdk.get_current_scope().set_custom_sampling_context({"custom_value": 1})
with sentry_sdk.traces.start_span(name="span", attributes={"first": True}):
...
sentry_sdk.traces.new_trace()
sentry_sdk.get_current_scope().set_custom_sampling_context({"custom_value": 2})
with sentry_sdk.traces.start_span(name="span", attributes={"first": False}):
...
def test_span_attributes(sentry_init, capture_envelopes):
sentry_init(
traces_sample_rate=1.0,
_experiments={"trace_lifecycle": "stream"},
)
events = capture_envelopes()
with sentry_sdk.traces.start_span(
name="segment", attributes={"attribute1": "value"}
) as span:
assert span.get_attributes()["attribute1"] == "value"
span.set_attribute("attribute2", 47)
span.remove_attribute("attribute1")
span.set_attributes({"attribute3": 4.5, "attribute4": False})
assert "attribute1" not in span.get_attributes()
attributes = span.get_attributes()
assert attributes["attribute2"] == 47
assert attributes["attribute3"] == 4.5
assert attributes["attribute4"] is False
sentry_sdk.get_client().flush()
spans = envelopes_to_spans(events)
assert len(spans) == 1
(span,) = spans
assert span["name"] == "segment"
assert "attribute1" not in span["attributes"]
assert span["attributes"]["attribute2"] == 47
assert span["attributes"]["attribute3"] == 4.5
assert span["attributes"]["attribute4"] is False
def test_span_attributes_serialize_early(sentry_init, capture_envelopes):
sentry_init(
traces_sample_rate=1.0,
_experiments={"trace_lifecycle": "stream"},
)
events = capture_envelopes()
class Class:
pass
with sentry_sdk.traces.start_span(name="span") as span:
span.set_attributes(
{
# arrays of different types will be serialized
"attribute1": [123, "text"],
# so will custom class instances
"attribute2": Class(),
}
)
attributes = span.get_attributes()
assert isinstance(attributes["attribute1"], str)
assert attributes["attribute1"] == "[123, 'text']"
assert isinstance(attributes["attribute2"], str)
assert "Class" in attributes["attribute2"]
sentry_sdk.get_client().flush()
spans = envelopes_to_spans(events)
assert len(spans) == 1
(span,) = spans
assert span["attributes"]["attribute1"] == "[123, 'text']"
assert "Class" in span["attributes"]["attribute2"]
def test_traces_sampler_drops_span(sentry_init, capture_envelopes):
def traces_sampler(sampling_context):
assert "attributes" in sampling_context["span_context"]
assert "drop" in sampling_context["span_context"]["attributes"]
if sampling_context["span_context"]["attributes"]["drop"] is True:
return 0.0
return 1.0
sentry_init(
traces_sampler=traces_sampler,
_experiments={"trace_lifecycle": "stream"},
)
events = capture_envelopes()
with sentry_sdk.traces.start_span(name="dropped", attributes={"drop": True}):
...
with sentry_sdk.traces.start_span(name="retained", attributes={"drop": False}):
...
sentry_sdk.get_client().flush()
spans = envelopes_to_spans(events)
assert len(spans) == 1
(span,) = spans
assert span["name"] == "retained"
assert span["attributes"]["drop"] is False
def test_traces_sampler_called_once_per_segment(sentry_init):
traces_sampler_called = 0
span_name_in_traces_sampler = None
def traces_sampler(sampling_context):
nonlocal traces_sampler_called, span_name_in_traces_sampler
traces_sampler_called += 1
span_name_in_traces_sampler = sampling_context["span_context"]["name"]
return 1.0
sentry_init(
traces_sampler=traces_sampler,
_experiments={"trace_lifecycle": "stream"},
)
with sentry_sdk.traces.start_span(name="segment") as segment:
with sentry_sdk.traces.start_span(name="child1"):
...
with sentry_sdk.traces.start_span(name="child2"):
with sentry_sdk.traces.start_span(name="child3"):
...
assert traces_sampler_called == 1
assert span_name_in_traces_sampler == segment.name
def test_start_inactive_span(sentry_init, capture_envelopes):
sentry_init(
traces_sample_rate=1.0,
_experiments={"trace_lifecycle": "stream"},
)
events = capture_envelopes()
with sentry_sdk.traces.start_span(name="segment") as segment:
with sentry_sdk.traces.start_span(name="child1", active=False):
with sentry_sdk.traces.start_span(name="child2"):
# Should have segment as parent since child1 is inactive
pass
sentry_sdk.get_client().flush()
spans = envelopes_to_spans(events)
assert len(spans) == 3
child2, child1, segment = spans
assert segment["is_segment"] is True
assert segment["name"] == "segment"
assert segment["attributes"]["sentry.segment.name"] == "segment"
assert child1["is_segment"] is False
assert child1["name"] == "child1"
assert child1["attributes"]["sentry.segment.name"] == "segment"
assert child1["parent_span_id"] == segment["span_id"]
assert child1["trace_id"] == segment["trace_id"]
assert child2["is_segment"] is False
assert child2["name"] == "child2"
assert child2["attributes"]["sentry.segment.name"] == "segment"
assert child2["parent_span_id"] == segment["span_id"]
assert child2["trace_id"] == segment["trace_id"]
def test_start_span_override_parent(sentry_init, capture_envelopes):
sentry_init(
traces_sample_rate=1.0,
_experiments={"trace_lifecycle": "stream"},
)
events = capture_envelopes()
with sentry_sdk.traces.start_span(name="segment") as segment:
with sentry_sdk.traces.start_span(name="child1"):
with sentry_sdk.traces.start_span(name="child2", parent_span=segment):
pass
sentry_sdk.get_client().flush()
spans = envelopes_to_spans(events)
assert len(spans) == 3
child2, child1, segment = spans
assert segment["name"] == "segment"
assert segment["attributes"]["sentry.segment.name"] == "segment"
assert child1["name"] == "child1"
assert child1["attributes"]["sentry.segment.name"] == "segment"
assert child2["name"] == "child2"
assert child2["attributes"]["sentry.segment.name"] == "segment"
assert segment["is_segment"] is True
assert child1["is_segment"] is False
assert child1["parent_span_id"] == segment["span_id"]
assert child1["trace_id"] == segment["trace_id"]
assert child2["is_segment"] is False
assert child2["parent_span_id"] == segment["span_id"]
assert child2["trace_id"] == segment["trace_id"]
def test_sibling_segments(sentry_init, capture_envelopes):
sentry_init(
traces_sample_rate=1.0,
_experiments={"trace_lifecycle": "stream"},
)
events = capture_envelopes()
with sentry_sdk.traces.start_span(name="segment1"):
...
with sentry_sdk.traces.start_span(name="segment2"):
...
sentry_sdk.get_client().flush()
spans = envelopes_to_spans(events)
assert len(spans) == 2
segment1, segment2 = spans
assert segment1["name"] == "segment1"
assert segment1["attributes"]["sentry.segment.name"] == "segment1"
assert segment1["is_segment"] is True
assert segment1["parent_span_id"] is None
assert segment2["name"] == "segment2"
assert segment2["attributes"]["sentry.segment.name"] == "segment2"
assert segment2["is_segment"] is True
assert segment2["parent_span_id"] is None
assert segment1["trace_id"] == segment2["trace_id"]
def test_sibling_segments_new_trace(sentry_init, capture_envelopes):
sentry_init(
traces_sample_rate=1.0,
_experiments={"trace_lifecycle": "stream"},
)
events = capture_envelopes()
with sentry_sdk.traces.start_span(name="segment1"):
...
sentry_sdk.traces.new_trace()
with sentry_sdk.traces.start_span(name="segment2"):
...
sentry_sdk.get_client().flush()
spans = envelopes_to_spans(events)
assert len(spans) == 2
segment1, segment2 = spans
assert segment1["name"] == "segment1"
assert segment1["attributes"]["sentry.segment.name"] == "segment1"
assert segment1["is_segment"] is True
assert segment1["parent_span_id"] is None
assert segment2["name"] == "segment2"
assert segment2["attributes"]["sentry.segment.name"] == "segment2"
assert segment2["is_segment"] is True
assert segment2["parent_span_id"] is None
assert segment1["trace_id"] != segment2["trace_id"]
def test_continue_trace_sampled(sentry_init, capture_envelopes):
sentry_init(
# parent sampling decision takes precedence over traces_sample_rate
traces_sample_rate=0.0,
_experiments={"trace_lifecycle": "stream"},
)
events = capture_envelopes()
trace_id = "0af7651916cd43dd8448eb211c80319c"
parent_span_id = "b7ad6b7169203331"
sample_rand = "0.222222"
sampled = "1"
sentry_sdk.traces.continue_trace(
{
"sentry-trace": f"{trace_id}-{parent_span_id}-{sampled}",
"baggage": f"sentry-trace_id={trace_id},sentry-sample_rate=0.5,sentry-sample_rand={sample_rand}",
}
)
with sentry_sdk.traces.start_span(name="segment") as span:
...
assert span.sampled is True
assert span.trace_id == trace_id
assert span._parent_span_id == parent_span_id
assert span._sample_rand == float(sample_rand)
sentry_sdk.get_client().flush()
spans = envelopes_to_spans(events)
assert len(spans) == 1
(segment,) = spans
assert segment["is_segment"] is True
assert segment["parent_span_id"] == parent_span_id
assert segment["trace_id"] == trace_id
def test_continue_trace_unsampled(sentry_init, capture_envelopes):
sentry_init(
# parent sampling decision takes precedence over traces_sample_rate
traces_sample_rate=1.0,
_experiments={"trace_lifecycle": "stream"},
)
events = capture_envelopes()
trace_id = "0af7651916cd43dd8448eb211c80319c"
parent_span_id = "b7ad6b7169203331"
sample_rand = "0.999999"
sampled = "0"
sentry_sdk.traces.continue_trace(
{
"sentry-trace": f"{trace_id}-{parent_span_id}-{sampled}",
"baggage": f"sentry-trace_id={trace_id},sentry-sample_rate=0.5,sentry-sample_rand={sample_rand}",
}
)
with sentry_sdk.traces.start_span(name="segment") as span:
...
assert span.sampled is False
assert span.name == ""
assert span.trace_id == "00000000000000000000000000000000"
assert span.span_id == "0000000000000000"
sentry_sdk.get_client().flush()
spans = envelopes_to_spans(events)
assert len(spans) == 0
def test_continue_trace_no_sample_rand(sentry_init, capture_envelopes):
sentry_init(
# parent sampling decision takes precedence over traces_sample_rate
traces_sample_rate=0.0,
_experiments={"trace_lifecycle": "stream"},
)
events = capture_envelopes()
trace_id = "0af7651916cd43dd8448eb211c80319c"
parent_span_id = "b7ad6b7169203331"
sampled = "1"
sentry_sdk.traces.continue_trace(
{
"sentry-trace": f"{trace_id}-{parent_span_id}-{sampled}",
"baggage": f"sentry-trace_id={trace_id},sentry-sample_rate=0.5",
}
)
with sentry_sdk.traces.start_span(name="segment") as span:
...
assert span.sampled is True
assert span.trace_id == trace_id
assert span._parent_span_id == parent_span_id
assert isinstance(span._sample_rand, float)
sentry_sdk.get_client().flush()
spans = envelopes_to_spans(events)
assert len(spans) == 1
(segment,) = spans
assert segment["is_segment"] is True
assert segment["parent_span_id"] == parent_span_id
assert segment["trace_id"] == trace_id
def test_outgoing_traceparent_and_baggage(sentry_init, capture_envelopes):
sentry_init(
traces_sample_rate=1.0,
_experiments={"trace_lifecycle": "stream"},
)
sentry_sdk.traces.new_trace()
with sentry_sdk.traces.start_span(name="span") as span:
assert span.sampled is True
trace_id = span.trace_id
span_id = span.span_id
traceparent = sentry_sdk.get_traceparent()
assert traceparent == f"{trace_id}-{span_id}-1"
baggage = sentry_sdk.get_baggage()
baggage_items = dict(tuple(item.split("=")) for item in baggage.split(","))
assert "sentry-trace_id" in baggage_items
assert baggage_items["sentry-trace_id"] == trace_id
assert "sentry-sampled" in baggage_items
assert baggage_items["sentry-sampled"] == "true"
def test_outgoing_traceparent_and_baggage_when_noop_span_is_active(
sentry_init, capture_envelopes
):
sentry_init(
traces_sample_rate=1.0,
_experiments={
"trace_lifecycle": "stream",
"ignore_spans": ["ignored"],
},
)
sentry_sdk.traces.new_trace()
propagation_context = (
sentry_sdk.get_current_scope().get_active_propagation_context()
)
propagation_trace_id = propagation_context.trace_id
propagation_span_id = propagation_context.span_id
with sentry_sdk.traces.start_span(name="ignored") as span:
assert span.sampled is False
noop_trace_id = span.trace_id
noop_span_id = span.span_id
traceparent = sentry_sdk.get_traceparent()
assert traceparent != f"{noop_trace_id}-{noop_span_id}"
assert traceparent == f"{propagation_trace_id}-{propagation_span_id}"
baggage = sentry_sdk.get_baggage()
baggage_items = dict(tuple(item.split("=")) for item in baggage.split(","))
assert "sentry-trace_id" in baggage_items
assert baggage_items["sentry-trace_id"] != noop_trace_id
assert baggage_items["sentry-trace_id"] == propagation_trace_id
def test_trace_decorator(sentry_init, capture_envelopes):
sentry_init(
traces_sample_rate=1.0,
_experiments={"trace_lifecycle": "stream"},
)
events = capture_envelopes()
@sentry_sdk.traces.trace
def traced_function(): ...
traced_function()
sentry_sdk.get_client().flush()
spans = envelopes_to_spans(events)
assert len(spans) == 1
(span,) = spans
assert (
span["name"]
== "test_span_streaming.test_trace_decorator.<locals>.traced_function"
)
assert span["status"] == "ok"
def test_trace_decorator_arguments(sentry_init, capture_envelopes):
sentry_init(
traces_sample_rate=1.0,
_experiments={"trace_lifecycle": "stream"},
)
events = capture_envelopes()
@sentry_sdk.traces.trace(name="traced", attributes={"traced.attribute": 123})
def traced_function(): ...
traced_function()
sentry_sdk.get_client().flush()
spans = envelopes_to_spans(events)
assert len(spans) == 1
(span,) = spans
assert span["name"] == "traced"
assert span["attributes"]["traced.attribute"] == 123
assert span["status"] == "ok"
def test_trace_decorator_inactive(sentry_init, capture_envelopes):
sentry_init(
traces_sample_rate=1.0,
_experiments={"trace_lifecycle": "stream"},
)
events = capture_envelopes()
@sentry_sdk.traces.trace(name="outer", active=False)
def traced_function():
with sentry_sdk.traces.start_span(name="inner"):
...
traced_function()
sentry_sdk.get_client().flush()
spans = envelopes_to_spans(events)
assert len(spans) == 2
(span1, span2) = spans
assert span1["name"] == "inner"
assert span1["parent_span_id"] != span2["span_id"]
assert span2["name"] == "outer"
@minimum_python_38
def test_trace_decorator_async(sentry_init, capture_envelopes):
sentry_init(
traces_sample_rate=1.0,
_experiments={"trace_lifecycle": "stream"},
)
events = capture_envelopes()
@sentry_sdk.traces.trace
async def traced_function(): ...
asyncio.run(traced_function())
sentry_sdk.get_client().flush()
spans = envelopes_to_spans(events)
assert len(spans) == 1
(span,) = spans
assert (
span["name"]
== "test_span_streaming.test_trace_decorator_async.<locals>.traced_function"
)
assert span["status"] == "ok"
@minimum_python_38
def test_trace_decorator_async_arguments(sentry_init, capture_envelopes):
sentry_init(
traces_sample_rate=1.0,
_experiments={"trace_lifecycle": "stream"},
)
events = capture_envelopes()
@sentry_sdk.traces.trace(name="traced", attributes={"traced.attribute": 123})
async def traced_function(): ...
asyncio.run(traced_function())
sentry_sdk.get_client().flush()
spans = envelopes_to_spans(events)
assert len(spans) == 1
(span,) = spans
assert span["name"] == "traced"
assert span["attributes"]["traced.attribute"] == 123
assert span["status"] == "ok"
@minimum_python_38
def test_trace_decorator_async_inactive(sentry_init, capture_envelopes):
sentry_init(
traces_sample_rate=1.0,
_experiments={"trace_lifecycle": "stream"},
)
events = capture_envelopes()
@sentry_sdk.traces.trace(name="outer", active=False)
async def traced_function():
with sentry_sdk.traces.start_span(name="inner"):
...
asyncio.run(traced_function())
sentry_sdk.get_client().flush()
spans = envelopes_to_spans(events)
assert len(spans) == 2
(span1, span2) = spans
assert span1["name"] == "inner"
assert span1["parent_span_id"] != span2["span_id"]
assert span2["name"] == "outer"
def test_set_span_status(sentry_init, capture_envelopes):
sentry_init(
traces_sample_rate=1.0,
_experiments={"trace_lifecycle": "stream"},
)
events = capture_envelopes()
with sentry_sdk.traces.start_span(name="span") as span:
span.status = SpanStatus.ERROR
with sentry_sdk.traces.start_span(name="span") as span:
span.status = "error"
sentry_sdk.get_client().flush()
spans = envelopes_to_spans(events)
assert len(spans) == 2
(span1, span2) = spans
assert span1["status"] == "error"
assert span2["status"] == "error"
def test_set_span_status_on_error(sentry_init, capture_envelopes):
sentry_init(
traces_sample_rate=1.0,
_experiments={"trace_lifecycle": "stream"},
)
events = capture_envelopes()
with pytest.raises(ValueError):
with sentry_sdk.traces.start_span(name="span") as span:
raise ValueError("oh no!")
sentry_sdk.get_client().flush()
spans = envelopes_to_spans(events)
assert len(spans) == 1
(span,) = spans
assert span["status"] == "error"
def test_set_span_status_on_ignored_span(sentry_init, capture_envelopes):
sentry_init(
traces_sample_rate=1.0,
_experiments={"trace_lifecycle": "stream", "ignore_spans": ["ignored"]},
)
events = capture_envelopes()
with sentry_sdk.traces.start_span(name="ignored") as span:
span.status = "error"
sentry_sdk.get_client().flush()
spans = envelopes_to_spans(events)
assert len(spans) == 0
@pytest.mark.parametrize(
("ignore_spans", "name", "attributes", "ignored"),
[
# no regexes
([], "/health", {}, False),
([{}], "/health", {}, False),
(["/health"], "/health", {}, True),
(["/health"], "/health", {"custom": "custom"}, True),
([{"name": "/health"}], "/health", {}, True),
([{"name": "/health"}], "/health", {"custom": "custom"}, True),
([{"attributes": {"custom": "custom"}}], "/health", {"custom": "custom"}, True),
([{"attributes": {"custom": "custom"}}], "/health", {}, False),
(
[{"name": "/nothealth", "attributes": {"custom": "custom"}}],
"/health",
{"custom": "custom"},
False,
),
(
[{"name": "/health", "attributes": {"custom": "notcustom"}}],
"/health",
{"custom": "custom"},
False,
),
(
[{"name": "/health", "attributes": {"custom": "custom"}}],
"/health",
{"custom": "custom"},
True,
),
# test cases with regexes
([re.compile("/hea.*")], "/health", {}, True),
([re.compile("/hea.*")], "/health", {"custom": "custom"}, True),
([{"name": re.compile("/hea.*")}], "/health", {}, True),
([{"name": re.compile("/hea.*")}], "/health", {"custom": "custom"}, True),
(
[{"attributes": {"custom": re.compile("c.*")}}],
"/health",