From 58a01bcc6a1f1a04922d7b357d2bb89a1e74c3e4 Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Fri, 24 Jul 2026 14:06:54 +0200 Subject: [PATCH] test: Add streaming API tests --- tests/test_api.py | 123 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 121 insertions(+), 2 deletions(-) diff --git a/tests/test_api.py b/tests/test_api.py index 9000945ae6..c25ed3397d 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -21,6 +21,7 @@ start_transaction, ) from sentry_sdk.client import Client, NonRecordingClient +from sentry_sdk.traces import StreamedSpan from sentry_sdk.tracing import Span from tests.conftest import TestTransportWithOptions @@ -34,7 +35,16 @@ def test_get_current_span(): assert get_current_span(fake_scope) is None -def test_get_current_span_default_hub(sentry_init): +def test_get_current_span_span_streaming(): + fake_scope = mock.MagicMock() + fake_scope.streamed_span = mock.MagicMock() + assert sentry_sdk.traces.get_current_span(fake_scope) == fake_scope.streamed_span + + fake_scope.streamed_span = None + assert sentry_sdk.traces.get_current_span(fake_scope) is None + + +def test_get_current_span_current_scope(sentry_init): sentry_init() assert get_current_span() is None @@ -46,7 +56,19 @@ def test_get_current_span_default_hub(sentry_init): assert get_current_span() == fake_span -def test_get_current_span_default_hub_with_transaction(sentry_init): +def test_get_current_span_current_scope_span_streaming(sentry_init): + sentry_init(trace_lifecycle="stream") + + assert sentry_sdk.traces.get_current_span() is None + + scope = get_current_scope() + fake_span = StreamedSpan(name="abc", scope=scope) + + assert scope.streamed_span == fake_span + assert sentry_sdk.traces.get_current_span() == fake_span + + +def test_get_current_span_with_transaction(sentry_init): sentry_init() assert get_current_span() is None @@ -55,6 +77,15 @@ def test_get_current_span_default_hub_with_transaction(sentry_init): assert get_current_span() == new_transaction +def test_get_current_span_with_segment(sentry_init): + sentry_init(trace_lifecycle="stream") + + assert sentry_sdk.traces.get_current_span() is None + + with sentry_sdk.traces.start_span(name="segment") as new_segment: + assert sentry_sdk.traces.get_current_span() == new_segment + + def test_traceparent_with_tracing_enabled(sentry_init): sentry_init(traces_sample_rate=1.0) @@ -66,6 +97,17 @@ def test_traceparent_with_tracing_enabled(sentry_init): assert get_traceparent() == expected_traceparent +def test_traceparent_with_tracing_enabled_span_streaming(sentry_init): + sentry_init(traces_sample_rate=1.0, trace_lifecycle="stream") + + with sentry_sdk.traces.start_span(name="span") as segment: + expected_traceparent = "%s-%s-1" % ( + segment.trace_id, + segment.span_id, + ) + assert get_traceparent() == expected_traceparent + + def test_traceparent_with_tracing_disabled(sentry_init): sentry_init() @@ -77,6 +119,17 @@ def test_traceparent_with_tracing_disabled(sentry_init): assert get_traceparent() == expected_traceparent +def test_traceparent_with_tracing_disabled_span_streaming(sentry_init): + sentry_init(trace_lifecycle="stream") + + propagation_context = get_isolation_scope()._propagation_context + expected_traceparent = "%s-%s" % ( + propagation_context.trace_id, + propagation_context.span_id, + ) + assert get_traceparent() == expected_traceparent + + def test_baggage_with_tracing_disabled(sentry_init): sentry_init(release="1.0.0", environment="dev") propagation_context = get_isolation_scope()._propagation_context @@ -88,6 +141,17 @@ def test_baggage_with_tracing_disabled(sentry_init): assert get_baggage() == expected_baggage +def test_baggage_with_tracing_disabled_span_streaming(sentry_init): + sentry_init(release="1.0.0", environment="dev", trace_lifecycle="stream") + propagation_context = get_isolation_scope()._propagation_context + expected_baggage = ( + "sentry-trace_id={},sentry-environment=dev,sentry-release=1.0.0".format( + propagation_context.trace_id + ) + ) + assert get_baggage() == expected_baggage + + def test_baggage_with_tracing_enabled(sentry_init): sentry_init(traces_sample_rate=1.0, release="1.0.0", environment="dev") with start_transaction() as transaction: @@ -97,6 +161,20 @@ def test_baggage_with_tracing_enabled(sentry_init): assert re.match(expected_baggage_re, get_baggage()) +def test_baggage_with_tracing_enabled_span_streaming(sentry_init): + sentry_init( + traces_sample_rate=1.0, + trace_lifecycle="stream", + release="1.0.0", + environment="dev", + ) + with sentry_sdk.traces.start_span(name="segment") as segment: + expected_baggage_re = r"^sentry-trace_id={},sentry-sample_rand=0\.\d{{6}},sentry-environment=dev,sentry-release=1\.0\.0,sentry-transaction=segment,sentry-sample_rate=1\.0,sentry-sampled={}$".format( + segment.trace_id, "true" if segment.sampled else "false" + ) + assert re.match(expected_baggage_re, get_baggage()) + + def test_baggage_with_dsn(sentry_init): sentry_init( dsn="http://97333d956c9e40989a0139756c121c34@sentry-x.sentry-y.s.c.local/976543210", @@ -112,6 +190,22 @@ def test_baggage_with_dsn(sentry_init): assert re.match(expected_baggage_re, get_baggage()) +def test_baggage_with_dsn_span_streaming(sentry_init): + sentry_init( + dsn="http://97333d956c9e40989a0139756c121c34@sentry-x.sentry-y.s.c.local/976543210", + traces_sample_rate=1.0, + trace_lifecycle="stream", + release="2.0.0", + environment="dev", + transport=TestTransportWithOptions, + ) + with sentry_sdk.traces.start_span(name="segment") as segment: + expected_baggage_re = r"^sentry-trace_id={},sentry-sample_rand=0\.\d{{6}},sentry-environment=dev,sentry-release=2\.0\.0,sentry-public_key=97333d956c9e40989a0139756c121c34,sentry-transaction=segment,sentry-sample_rate=1\.0,sentry-sampled={}$".format( + segment.trace_id, "true" if segment.sampled else "false" + ) + assert re.match(expected_baggage_re, get_baggage()) + + def test_continue_trace(sentry_init): sentry_init() @@ -138,6 +232,31 @@ def test_continue_trace(sentry_init): } +def test_continue_trace_span_streaming(sentry_init): + sentry_init(trace_lifecycle="stream") + + trace_id = "471a43a4192642f0b136d5159a501701" + parent_span_id = "6e8f22c393e68f19" + parent_sampled = 1 + sentry_sdk.traces.continue_trace( + { + "sentry-trace": "{}-{}-{}".format(trace_id, parent_span_id, parent_sampled), + "baggage": "sentry-trace_id=566e3688a61d4bc888951642d6f14a19,sentry-sample_rand=0.123456", + } + ) + with sentry_sdk.traces.start_span(name="some name") as segment: + assert segment.name == "some name" + + propagation_context = get_isolation_scope()._propagation_context + assert propagation_context.trace_id == segment.trace_id == trace_id + assert propagation_context.parent_span_id == parent_span_id + assert propagation_context.parent_sampled == parent_sampled + assert propagation_context.dynamic_sampling_context == { + "trace_id": "566e3688a61d4bc888951642d6f14a19", + "sample_rand": "0.123456", + } + + def test_is_initialized(): assert not is_initialized()