Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions sentry_sdk/integrations/fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from sentry_sdk.integrations import DidNotEnable, _check_minimum_version
from sentry_sdk.traces import StreamedSpan, get_current_span
from sentry_sdk.tracing import SOURCE_FOR_STYLE, TransactionSource
from sentry_sdk.tracing_utils import has_span_streaming_enabled
from sentry_sdk.utils import (
has_data_collection_enabled,
parse_version,
Expand Down Expand Up @@ -192,18 +191,11 @@ def _sentry_get_request_handler(*args: "Any", **kwargs: "Any") -> "Any":

@wraps(old_call)
def _sentry_call(*args: "Any", **kwargs: "Any") -> "Any":
current_scope = sentry_sdk.get_current_scope()
current_span = sentry_sdk.traces.get_current_span()

client = sentry_sdk.get_client()
if has_span_streaming_enabled(client.options):
current_span = current_scope.streamed_span

if type(current_span) is StreamedSpan:
segment = current_span._segment
segment._update_active_thread()

elif current_scope.transaction is not None:
current_scope.transaction.update_active_thread()
if type(current_span) is StreamedSpan:
segment = current_span._segment
segment._update_active_thread()

return old_call(*args, **kwargs)

Expand Down
45 changes: 17 additions & 28 deletions sentry_sdk/integrations/starlette.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@
)
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
from sentry_sdk.scope import should_send_default_pii
from sentry_sdk.traces import StreamedSpan, get_current_span
from sentry_sdk.traces import StreamedSpan
from sentry_sdk.tracing import (
SOURCE_FOR_STYLE,
TransactionSource,
)
from sentry_sdk.tracing_utils import has_span_streaming_enabled
from sentry_sdk.utils import (
AnnotatedValue,
capture_internal_exceptions,
Expand Down Expand Up @@ -170,24 +169,18 @@ async def _create_span_call(
return await old_call(app, scope, receive, send, **kwargs)

middleware_name = app.__class__.__name__
is_span_streaming_enabled = has_span_streaming_enabled(client.options)

def _start_middleware_span(op: str, name: str) -> "Any":
if is_span_streaming_enabled:
if sentry_sdk.traces.get_current_span() is None:
return nullcontext()
return sentry_sdk.traces.start_span(
name=name,
attributes={
"sentry.op": op,
"sentry.origin": StarletteIntegration.origin,
"middleware.name": middleware_name,
},
)
return sentry_sdk.start_span(
op=op,
if sentry_sdk.traces.get_current_span() is None:
return nullcontext()

return sentry_sdk.traces.start_span(
name=name,
origin=StarletteIntegration.origin,
attributes={
"sentry.op": op,
"sentry.origin": StarletteIntegration.origin,
"middleware.name": middleware_name,
},
)

with _start_middleware_span(op=OP.MIDDLEWARE_STARLETTE, name=middleware_name):
Expand Down Expand Up @@ -549,7 +542,7 @@ def event_processor(event: "Event", hint: "Dict[str, Any]") -> "Event":
try:
return await handler(*args, **kwargs)
finally:
current_span = get_current_span()
current_span = sentry_sdk.traces.get_current_span()

if type(current_span) is StreamedSpan:
attach_request_data = True
Expand Down Expand Up @@ -594,23 +587,19 @@ def _sentry_sync_func(*args: "Any", **kwargs: "Any") -> "Any":
if integration is None:
return old_func(*args, **kwargs)

current_scope = sentry_sdk.get_current_scope()

span_streaming = has_span_streaming_enabled(client.options)
if span_streaming:
current_span = current_scope.streamed_span
current_span = sentry_sdk.traces.get_current_span()

if type(current_span) is StreamedSpan:
current_span._segment._update_active_thread()
elif current_scope.transaction is not None:
current_scope.transaction.update_active_thread()
if type(current_span) is StreamedSpan:
current_span._segment._update_active_thread()

sentry_scope = sentry_sdk.get_isolation_scope()

request = args[0]

_set_transaction_name_and_source(
current_scope, integration.transaction_style, request
sentry_sdk.get_current_scope(),
integration.transaction_style,
request,
)

extractor = StarletteRequestExtractor(request)
Expand Down
Loading
Loading