Skip to content

Commit 11fe228

Browse files
limit api to traces.py and mark private
1 parent a8adaee commit 11fe228

7 files changed

Lines changed: 30 additions & 35 deletions

File tree

sentry_sdk/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
"get_isolation_scope",
3333
"get_current_scope",
3434
"get_current_span",
35-
"get_current_streamed_span",
3635
"get_traceparent",
3736
"is_initialized",
3837
"isolation_scope",

sentry_sdk/api.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from sentry_sdk._init_implementation import init
77
from sentry_sdk.consts import INSTRUMENTER
88
from sentry_sdk.scope import Scope, _ScopeManager, new_scope, isolation_scope
9-
from sentry_sdk.traces import StreamedSpan
9+
from sentry_sdk.traces import StreamedSpan, _get_current_streamed_span
1010
from sentry_sdk.tracing import NoOpSpan, Transaction, trace
1111
from sentry_sdk.crons import monitor
1212

@@ -67,7 +67,6 @@ def overload(x: "T") -> "T":
6767
"get_isolation_scope",
6868
"get_current_scope",
6969
"get_current_span",
70-
"get_current_streamed_span",
7170
"get_traceparent",
7271
"is_initialized",
7372
"isolation_scope",
@@ -430,15 +429,6 @@ def get_current_span(
430429
return tracing_utils.get_current_span(scope)
431430

432431

433-
def get_current_streamed_span(
434-
scope: "Optional[Scope]" = None,
435-
) -> "Optional[StreamedSpan]":
436-
"""
437-
Returns the currently active streamed span if there is one running, otherwise `None`
438-
"""
439-
return tracing_utils.get_current_streamed_span(scope)
440-
441-
442432
def get_traceparent() -> "Optional[str]":
443433
"""
444434
Returns the traceparent either from the active span or from the scope.
@@ -543,7 +533,7 @@ def update_current_span(
543533
attributes={"user_id": 123, "batch_size": 50}
544534
)
545535
"""
546-
if isinstance(get_current_streamed_span(), StreamedSpan):
536+
if isinstance(_get_current_streamed_span(), StreamedSpan):
547537
warnings.warn(
548538
"The `update_current_span` API isn't available in streaming mode. "
549539
"Retrieve the current span with get_current_span() and use its API "

sentry_sdk/integrations/celery/__init__.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from sentry_sdk.integrations.celery.utils import _now_seconds_since_epoch
1616
from sentry_sdk.integrations.logging import ignore_logger
1717
from sentry_sdk.scope import should_send_default_pii
18-
from sentry_sdk.traces import StreamedSpan
18+
from sentry_sdk.traces import StreamedSpan, _get_current_streamed_span
1919
from sentry_sdk.tracing import BAGGAGE_HEADER_NAME, Span, TransactionSource
2020
from sentry_sdk.tracing_utils import Baggage, has_span_streaming_enabled
2121
from sentry_sdk.utils import (
@@ -292,10 +292,7 @@ def apply_async(*args: "Any", **kwargs: "Any") -> "Any":
292292

293293
span_mgr: "Union[StreamedSpan, Span, NoOpMgr]" = NoOpMgr()
294294
if span_streaming:
295-
if (
296-
not task_started_from_beat
297-
and sentry_sdk.get_current_streamed_span() is not None
298-
):
295+
if not task_started_from_beat and _get_current_streamed_span() is not None:
299296
span_mgr = sentry_sdk.traces.start_span(
300297
name=task_name,
301298
attributes={
@@ -576,7 +573,7 @@ def sentry_publish(self: "Producer", *args: "Any", **kwargs: "Any") -> "Any":
576573

577574
span: "Union[StreamedSpan, Span, None]" = None
578575
if span_streaming:
579-
if sentry_sdk.get_current_streamed_span() is not None:
576+
if _get_current_streamed_span() is not None:
580577
span = sentry_sdk.traces.start_span(
581578
name=task_name,
582579
attributes={

sentry_sdk/integrations/starlette.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
)
2222
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
2323
from sentry_sdk.scope import should_send_default_pii
24-
from sentry_sdk.traces import NoOpStreamedSpan, StreamedSpan
24+
from sentry_sdk.traces import NoOpStreamedSpan, StreamedSpan, _get_current_streamed_span
2525
from sentry_sdk.tracing import (
2626
SOURCE_FOR_STYLE,
2727
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 = sentry_sdk.get_current_streamed_span()
257+
current_span = _get_current_streamed_span()
258258
if (
259259
info
260260
and "data" in info

sentry_sdk/traces.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,3 +755,14 @@ def make_db_query(sql):
755755
return decorator(func)
756756
else:
757757
return decorator
758+
759+
760+
def _get_current_streamed_span(
761+
scope: "Optional[sentry_sdk.Scope]" = None,
762+
) -> "Optional[StreamedSpan]":
763+
"""
764+
Returns the currently active span if there is one running, otherwise `None`
765+
"""
766+
scope = scope or sentry_sdk.get_current_scope()
767+
current_span = scope.streamed_span
768+
return current_span

sentry_sdk/tracing_utils.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
_module_in_list,
3636
)
3737
from sentry_sdk.tracing import Span as LegacySpan
38+
from sentry_sdk.traces import _get_current_streamed_span
3839

3940
from typing import TYPE_CHECKING
4041

@@ -1203,25 +1204,21 @@ def get_current_span(
12031204
return current_span
12041205

12051206

1206-
def get_current_streamed_span(
1207-
scope: "Optional[sentry_sdk.Scope]" = None,
1208-
) -> "Optional[StreamedSpan]":
1209-
"""
1210-
Returns the currently active span if there is one running, otherwise `None`
1211-
"""
1212-
scope = scope or sentry_sdk.get_current_scope()
1213-
current_span = scope.streamed_span
1214-
return current_span
1215-
1216-
12171207
def set_span_errored(span: "Optional[Union[Span, StreamedSpan]]" = None) -> None:
12181208
"""
12191209
Set the status of the current or given span to INTERNAL_ERROR.
12201210
Also sets the status of the transaction (root span) to INTERNAL_ERROR.
12211211
"""
12221212
from sentry_sdk.traces import StreamedSpan, SpanStatus
12231213

1224-
span = span or get_current_span()
1214+
client = sentry_sdk.get_client()
1215+
1216+
if not span:
1217+
span = (
1218+
_get_current_streamed_span()
1219+
if has_span_streaming_enabled(client.options)
1220+
else sentry_sdk.get_current_span()
1221+
)
12251222

12261223
if span is not None:
12271224
if isinstance(span, Span):

tests/integrations/celery/test_celery.py

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

99
import sentry_sdk
10-
from sentry_sdk import start_transaction, get_current_span, get_current_streamed_span
10+
from sentry_sdk import start_transaction, get_current_span
11+
from sentry_sdk.traces import _get_current_streamed_span
1112
from sentry_sdk.integrations.celery import (
1213
CeleryIntegration,
1314
_wrap_task_run,
@@ -664,7 +665,7 @@ def test_sentry_propagate_traces_override(span_streaming, init_celery):
664665
@celery.task(name="dummy_task", bind=True)
665666
def dummy_task(self, message):
666667
trace_id = (
667-
get_current_streamed_span().trace_id
668+
_get_current_streamed_span().trace_id
668669
if span_streaming
669670
else get_current_span().trace_id
670671
)

0 commit comments

Comments
 (0)