Skip to content

Commit 406b01c

Browse files
sentrivanaclaude
andcommitted
ref: Replace _get_current_streamed_span with get_current_span
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 69ce3fb commit 406b01c

6 files changed

Lines changed: 30 additions & 47 deletions

File tree

sentry_sdk/api.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
from sentry_sdk.consts import INSTRUMENTER
99
from sentry_sdk.crons import monitor
1010
from sentry_sdk.scope import Scope, _ScopeManager, isolation_scope, new_scope
11-
from sentry_sdk.traces import StreamedSpan, _get_current_streamed_span
11+
from sentry_sdk.traces import StreamedSpan
12+
from sentry_sdk.traces import get_current_span as _get_current_streamed_span
1213
from sentry_sdk.tracing import NoOpSpan, Transaction, trace
1314

1415
if TYPE_CHECKING:

sentry_sdk/feature_flags.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def add_feature_flag(flag: str, result: bool) -> None:
6565
flags.set(flag, result)
6666

6767
if has_span_streaming_enabled(client.options):
68-
span = sentry_sdk.traces._get_current_streamed_span()
68+
span = sentry_sdk.traces.get_current_span()
6969
if span and isinstance(span, sentry_sdk.traces.StreamedSpan):
7070
span.set_attribute(f"flag.evaluation.{flag}", result)
7171

sentry_sdk/integrations/celery/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from sentry_sdk.integrations.celery.utils import _now_seconds_since_epoch
1717
from sentry_sdk.integrations.logging import ignore_logger
1818
from sentry_sdk.scope import Scope, should_send_default_pii
19-
from sentry_sdk.traces import StreamedSpan, _get_current_streamed_span
19+
from sentry_sdk.traces import StreamedSpan, get_current_span
2020
from sentry_sdk.tracing import BAGGAGE_HEADER_NAME, Span, TransactionSource
2121
from sentry_sdk.tracing_utils import Baggage, has_span_streaming_enabled
2222
from sentry_sdk.utils import (
@@ -286,7 +286,7 @@ def apply_async(*args: "Any", **kwargs: "Any") -> "Any":
286286

287287
span_mgr: "Union[StreamedSpan, Span, NoOpMgr]" = NoOpMgr()
288288
if span_streaming:
289-
if not task_started_from_beat and _get_current_streamed_span() is not None:
289+
if not task_started_from_beat and get_current_span() is not None:
290290
span_mgr = sentry_sdk.traces.start_span(
291291
name=task_name,
292292
attributes={
@@ -567,7 +567,7 @@ def sentry_publish(self: "Producer", *args: "Any", **kwargs: "Any") -> "Any":
567567

568568
span: "Union[StreamedSpan, Span, None]" = None
569569
if span_streaming:
570-
if _get_current_streamed_span() is not None:
570+
if get_current_span() is not None:
571571
span = sentry_sdk.traces.start_span(
572572
name=task_name,
573573
attributes={

sentry_sdk/integrations/starlette.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
)
2323
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
2424
from sentry_sdk.scope import should_send_default_pii
25-
from sentry_sdk.traces import StreamedSpan, _get_current_streamed_span
25+
from sentry_sdk.traces import StreamedSpan, get_current_span
2626
from sentry_sdk.tracing import (
2727
SOURCE_FOR_STYLE,
2828
TransactionSource,
@@ -254,7 +254,7 @@ def _default(value: "Any") -> "Any":
254254
def _set_request_body_data_on_streaming_segment(
255255
info: "Optional[Dict[str, Any]]",
256256
) -> None:
257-
current_span = _get_current_streamed_span()
257+
current_span = get_current_span()
258258
if info and "data" in info and type(current_span) is StreamedSpan:
259259
with capture_internal_exceptions():
260260
current_span._segment.set_attribute(

tests/integrations/celery/test_celery.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
from celery.bin import worker
99

1010
import sentry_sdk
11+
import sentry_sdk.traces
1112
from sentry_sdk import get_current_span, start_transaction
1213
from sentry_sdk.integrations.celery import (
1314
CeleryIntegration,
1415
_wrap_task_run,
1516
)
1617
from sentry_sdk.integrations.celery.beat import _get_headers
17-
from sentry_sdk.traces import _get_current_streamed_span
1818
from sentry_sdk.utils import SENSITIVE_DATA_SUBSTITUTE
1919
from tests.conftest import ApproxDict
2020

@@ -667,7 +667,7 @@ def test_sentry_propagate_traces_override(span_streaming, init_celery):
667667
@celery.task(name="dummy_task", bind=True)
668668
def dummy_task(self, message):
669669
trace_id = (
670-
_get_current_streamed_span().trace_id
670+
sentry_sdk.traces.get_current_span().trace_id
671671
if span_streaming
672672
else get_current_span().trace_id
673673
)

tests/integrations/rust_tracing/test_rust_tracing.py

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,13 @@ def test_on_new_span_on_close(
8787
with sentry_sdk.traces.start_span(name="custom parent"):
8888
rust_tracing.new_span(RustTracingLevel.Info, 3)
8989

90-
sentry_first_rust_span = sentry_sdk.traces._get_current_streamed_span()
90+
sentry_first_rust_span = sentry_sdk.traces.get_current_span()
9191
rust_first_rust_span = rust_tracing.spans[3]
9292

9393
assert sentry_first_rust_span == rust_first_rust_span
9494

9595
rust_tracing.close_span(3)
96-
assert (
97-
sentry_sdk.traces._get_current_streamed_span() != sentry_first_rust_span
98-
)
96+
assert sentry_sdk.traces.get_current_span() != sentry_first_rust_span
9997

10098
sentry_sdk.flush()
10199
spans = [item.payload for item in items if item.type == "span"]
@@ -173,32 +171,30 @@ def test_nested_on_new_span_on_close(
173171
if span_streaming:
174172
items = capture_items("event", "transaction", "span")
175173
with sentry_sdk.traces.start_span(name="custom parent"):
176-
original_sentry_span = sentry_sdk.traces._get_current_streamed_span()
174+
original_sentry_span = sentry_sdk.traces.get_current_span()
177175

178176
rust_tracing.new_span(RustTracingLevel.Info, 3, index_arg=10)
179-
sentry_first_rust_span = sentry_sdk.traces._get_current_streamed_span()
177+
sentry_first_rust_span = sentry_sdk.traces.get_current_span()
180178
rust_first_rust_span = rust_tracing.spans[3]
181179

182180
# Use a different `index_arg` value for the inner span to help
183181
# distinguish the two at the end of the test
184182
rust_tracing.new_span(RustTracingLevel.Info, 5, index_arg=9)
185-
sentry_second_rust_span = sentry_sdk.traces._get_current_streamed_span()
183+
sentry_second_rust_span = sentry_sdk.traces.get_current_span()
186184
rust_second_rust_span = rust_tracing.spans[5]
187185

188186
assert rust_second_rust_span == sentry_second_rust_span
189187

190188
rust_tracing.close_span(5)
191189

192190
# Ensure the current sentry span was moved back to the parent
193-
sentry_span_after_close = sentry_sdk.traces._get_current_streamed_span()
191+
sentry_span_after_close = sentry_sdk.traces.get_current_span()
194192
assert sentry_span_after_close == sentry_first_rust_span
195193
assert sentry_span_after_close == rust_first_rust_span
196194

197195
rust_tracing.close_span(3)
198196

199-
assert (
200-
sentry_sdk.traces._get_current_streamed_span() == original_sentry_span
201-
)
197+
assert sentry_sdk.traces.get_current_span() == original_sentry_span
202198

203199
sentry_sdk.flush()
204200
spans = [item.payload for item in items if item.type == "span"]
@@ -312,10 +308,10 @@ def test_on_new_span_without_transaction(sentry_init, span_streaming):
312308
)
313309

314310
if span_streaming:
315-
assert sentry_sdk.traces._get_current_streamed_span() is None
311+
assert sentry_sdk.traces.get_current_span() is None
316312

317313
rust_tracing.new_span(RustTracingLevel.Info, 3)
318-
current_span = sentry_sdk.traces._get_current_streamed_span()
314+
current_span = sentry_sdk.traces.get_current_span()
319315
assert current_span is not None
320316
assert current_span._segment is current_span
321317
else:
@@ -584,24 +580,22 @@ def span_filter(metadata: Dict[str, object]) -> bool:
584580
if span_streaming:
585581
items = capture_items("event", "transaction", "span")
586582
with sentry_sdk.traces.start_span(name="custom parent"):
587-
original_sentry_span = sentry_sdk.traces._get_current_streamed_span()
583+
original_sentry_span = sentry_sdk.traces.get_current_span()
588584

589585
# Span is not ignored
590586
rust_tracing.new_span(RustTracingLevel.Info, 3, index_arg=10)
591-
info_span = sentry_sdk.traces._get_current_streamed_span()
587+
info_span = sentry_sdk.traces.get_current_span()
592588

593589
# Span is ignored, current span should remain the same
594590
rust_tracing.new_span(RustTracingLevel.Trace, 5, index_arg=9)
595-
assert sentry_sdk.traces._get_current_streamed_span() == info_span
591+
assert sentry_sdk.traces.get_current_span() == info_span
596592

597593
# Closing the filtered span should leave the current span alone
598594
rust_tracing.close_span(5)
599-
assert sentry_sdk.traces._get_current_streamed_span() == info_span
595+
assert sentry_sdk.traces.get_current_span() == info_span
600596

601597
rust_tracing.close_span(3)
602-
assert (
603-
sentry_sdk.traces._get_current_streamed_span() == original_sentry_span
604-
)
598+
assert sentry_sdk.traces.get_current_span() == original_sentry_span
605599

606600
sentry_sdk.flush()
607601
spans = [item.payload for item in items if item.type == "span"]
@@ -652,16 +646,12 @@ def test_record(sentry_init, span_streaming):
652646
with sentry_sdk.traces.start_span(name="custom parent"):
653647
rust_tracing.new_span(RustTracingLevel.Info, 3)
654648

655-
span_before_record = (
656-
sentry_sdk.traces._get_current_streamed_span()._to_json()
657-
)
649+
span_before_record = sentry_sdk.traces.get_current_span()._to_json()
658650
assert span_before_record["attributes"]["version"] == "None"
659651

660652
rust_tracing.record(3)
661653

662-
span_after_record = (
663-
sentry_sdk.traces._get_current_streamed_span()._to_json()
664-
)
654+
span_after_record = sentry_sdk.traces.get_current_span()._to_json()
665655
assert span_after_record["attributes"]["version"] == "memoized"
666656
else:
667657
with start_transaction():
@@ -699,18 +689,14 @@ def span_filter(metadata: Dict[str, object]) -> bool:
699689
with sentry_sdk.traces.start_span(name="custom parent"):
700690
rust_tracing.new_span(RustTracingLevel.Info, 3)
701691

702-
span_before_record = (
703-
sentry_sdk.traces._get_current_streamed_span()._to_json()
704-
)
692+
span_before_record = sentry_sdk.traces.get_current_span()._to_json()
705693
assert span_before_record["attributes"]["version"] == "None"
706694

707695
rust_tracing.new_span(RustTracingLevel.Trace, 5)
708696
rust_tracing.record(5)
709697

710698
# `on_record()` should not do anything to the current Sentry span if the associated Rust span was ignored
711-
span_after_record = (
712-
sentry_sdk.traces._get_current_streamed_span()._to_json()
713-
)
699+
span_after_record = sentry_sdk.traces.get_current_span()._to_json()
714700
assert span_after_record["attributes"]["version"] == "None"
715701
else:
716702
with start_transaction():
@@ -764,19 +750,15 @@ def test_include_tracing_fields(
764750
with sentry_sdk.traces.start_span(name="custom parent"):
765751
rust_tracing.new_span(RustTracingLevel.Info, 3)
766752

767-
span_before_record = (
768-
sentry_sdk.traces._get_current_streamed_span()._to_json()
769-
)
753+
span_before_record = sentry_sdk.traces.get_current_span()._to_json()
770754
if tracing_fields_expected:
771755
assert span_before_record["attributes"]["version"] == "None"
772756
else:
773757
assert span_before_record["attributes"]["version"] == "[Filtered]"
774758

775759
rust_tracing.record(3)
776760

777-
span_after_record = (
778-
sentry_sdk.traces._get_current_streamed_span()._to_json()
779-
)
761+
span_after_record = sentry_sdk.traces.get_current_span()._to_json()
780762

781763
if tracing_fields_expected:
782764
assert span_after_record["attributes"] == {

0 commit comments

Comments
 (0)