22
33import pytest
44
5+ import sentry_sdk
6+ from sentry_sdk .traces import StreamedSpan
57from sentry_sdk .tracing import Transaction
68from sentry_sdk .tracing_utils import extract_sentrytrace_data
79
@@ -26,6 +28,32 @@ def test_to_traceparent(sampled):
2628 assert parts [2 ] == "1" if sampled is True else "0" # sampled
2729
2830
31+ @pytest .mark .parametrize ("traces_sample_rate" , [1.0 , 0.0 , None ])
32+ def test_to_traceparent_span_streaming (sentry_init , traces_sample_rate ):
33+ sentry_init (
34+ traces_sample_rate = traces_sample_rate ,
35+ _experiments = {
36+ "trace_lifecycle" : "stream" ,
37+ },
38+ )
39+
40+ with sentry_sdk .traces .start_span (name = "/interactions/other-dogs/new-dog" ) as span :
41+ traceparent = sentry_sdk .get_traceparent ()
42+
43+ parts = traceparent .split ("-" )
44+ if traces_sample_rate is None :
45+ propagation_context = (
46+ sentry_sdk .get_current_scope ().get_active_propagation_context ()
47+ )
48+ assert parts [0 ] == propagation_context .trace_id # trace_id
49+ assert parts [1 ] == propagation_context .span_id # parent_span_id
50+ assert len (parts ) == 2
51+ else :
52+ assert parts [0 ] == span .trace_id # trace_id
53+ assert parts [1 ] == span .span_id # parent_span_id
54+ assert parts [2 ] == "1" if traces_sample_rate == 1.0 else "0" # sampled
55+
56+
2957@pytest .mark .parametrize ("sampling_decision" , [True , False ])
3058def test_sentrytrace_extraction (sampling_decision ):
3159 sentrytrace_header = "12312012123120121231201212312012-0415201309082013-{}" .format (
@@ -75,3 +103,24 @@ def test_iter_headers(monkeypatch):
75103 assert (
76104 headers ["sentry-trace" ] == "12312012123120121231201212312012-0415201309082013-0"
77105 )
106+
107+
108+ def test_iter_headers_span_streaming (sentry_init , monkeypatch ):
109+ sentry_init (
110+ traces_sample_rate = 0.0 ,
111+ _experiments = {
112+ "trace_lifecycle" : "stream" ,
113+ },
114+ )
115+ monkeypatch .setattr (
116+ StreamedSpan ,
117+ "_to_traceparent" ,
118+ mock .Mock (return_value = "12312012123120121231201212312012-0415201309082013-0" ),
119+ )
120+
121+ with sentry_sdk .traces .start_span (name = "/interactions/other-dogs/new-dog" ) as span :
122+ headers = dict (span ._iter_headers ())
123+ assert (
124+ headers ["sentry-trace" ]
125+ == "12312012123120121231201212312012-0415201309082013-0"
126+ )
0 commit comments