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
149 changes: 54 additions & 95 deletions sentry_sdk/integrations/litestar.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from sentry_sdk.integrations.logging import ignore_logger_for_events
from sentry_sdk.scope import should_send_default_pii
from sentry_sdk.tracing import SOURCE_FOR_STYLE, TransactionSource
from sentry_sdk.tracing_utils import has_span_streaming_enabled
from sentry_sdk.utils import (
ensure_integration_enabled,
event_from_exception,
Expand Down Expand Up @@ -169,100 +168,60 @@ async def _create_span_call(
return await old_call(self, scope, receive, send)

middleware_name = self.__class__.__name__
if has_span_streaming_enabled(client.options):
if sentry_sdk.traces.get_current_span() is None:
return await old_call(self, scope, receive, send)
with sentry_sdk.traces.start_span(
name=middleware_name,
attributes={
"sentry.op": OP.MIDDLEWARE_LITESTAR,
"sentry.origin": LitestarIntegration.origin,
},
) as middleware_span:
middleware_span.set_attribute(SPANDATA.MIDDLEWARE_NAME, middleware_name)

# Creating spans for the "receive" callback
async def _sentry_receive(
*args: "Any", **kwargs: "Any"
) -> "Union[HTTPReceiveMessage, WebSocketReceiveMessage]":
if client.get_integration(LitestarIntegration) is None:
return await receive(*args, **kwargs)
if sentry_sdk.traces.get_current_span() is None:
return await receive(*args, **kwargs)
with sentry_sdk.traces.start_span(
name=getattr(receive, "__qualname__", str(receive)),
attributes={
"sentry.op": OP.MIDDLEWARE_LITESTAR_RECEIVE,
"sentry.origin": LitestarIntegration.origin,
},
) as span:
span.set_attribute(SPANDATA.MIDDLEWARE_NAME, middleware_name)
return await receive(*args, **kwargs)

receive_name = getattr(receive, "__name__", str(receive))
receive_patched = receive_name == "_sentry_receive"
new_receive = _sentry_receive if not receive_patched else receive

# Creating spans for the "send" callback
async def _sentry_send(message: "Message") -> None:
if client.get_integration(LitestarIntegration) is None:
return await send(message)
if sentry_sdk.traces.get_current_span() is None:
return await send(message)
with sentry_sdk.traces.start_span(
name=getattr(send, "__qualname__", str(send)),
attributes={
"sentry.op": OP.MIDDLEWARE_LITESTAR_SEND,
"sentry.origin": LitestarIntegration.origin,
},
) as span:
span.set_attribute(SPANDATA.MIDDLEWARE_NAME, middleware_name)
return await send(message)

send_name = getattr(send, "__name__", str(send))
send_patched = send_name == "_sentry_send"
new_send = _sentry_send if not send_patched else send

return await old_call(self, scope, new_receive, new_send)
else:
with sentry_sdk.start_span(
op=OP.MIDDLEWARE_LITESTAR,
name=middleware_name,
origin=LitestarIntegration.origin,
) as middleware_span:
# Creating spans for the "receive" callback
async def _sentry_receive(
*args: "Any", **kwargs: "Any"
) -> "Union[HTTPReceiveMessage, WebSocketReceiveMessage]":
if client.get_integration(LitestarIntegration) is None:
return await receive(*args, **kwargs)
with sentry_sdk.start_span(
op=OP.MIDDLEWARE_LITESTAR_RECEIVE,
name=getattr(receive, "__qualname__", str(receive)),
origin=LitestarIntegration.origin,
):
return await receive(*args, **kwargs)

receive_name = getattr(receive, "__name__", str(receive))
receive_patched = receive_name == "_sentry_receive"
new_receive = _sentry_receive if not receive_patched else receive

# Creating spans for the "send" callback
async def _sentry_send(message: "Message") -> None:
if client.get_integration(LitestarIntegration) is None:
return await send(message)
with sentry_sdk.start_span(
op=OP.MIDDLEWARE_LITESTAR_SEND,
name=getattr(send, "__qualname__", str(send)),
origin=LitestarIntegration.origin,
):
return await send(message)

send_name = getattr(send, "__name__", str(send))
send_patched = send_name == "_sentry_send"
new_send = _sentry_send if not send_patched else send

return await old_call(self, scope, new_receive, new_send)
if sentry_sdk.traces.get_current_span() is None:
return await old_call(self, scope, receive, send)
with sentry_sdk.traces.start_span(
name=middleware_name,
attributes={
"sentry.op": OP.MIDDLEWARE_LITESTAR,
"sentry.origin": LitestarIntegration.origin,
},
) as middleware_span:
middleware_span.set_attribute(SPANDATA.MIDDLEWARE_NAME, middleware_name)

# Creating spans for the "receive" callback
async def _sentry_receive(
*args: "Any", **kwargs: "Any"
) -> "Union[HTTPReceiveMessage, WebSocketReceiveMessage]":
if client.get_integration(LitestarIntegration) is None:
return await receive(*args, **kwargs)
if sentry_sdk.traces.get_current_span() is None:
return await receive(*args, **kwargs)
with sentry_sdk.traces.start_span(
name=getattr(receive, "__qualname__", str(receive)),
attributes={
"sentry.op": OP.MIDDLEWARE_LITESTAR_RECEIVE,
"sentry.origin": LitestarIntegration.origin,
},
) as span:
span.set_attribute(SPANDATA.MIDDLEWARE_NAME, middleware_name)
return await receive(*args, **kwargs)

receive_name = getattr(receive, "__name__", str(receive))
receive_patched = receive_name == "_sentry_receive"
new_receive = _sentry_receive if not receive_patched else receive

# Creating spans for the "send" callback
async def _sentry_send(message: "Message") -> None:
if client.get_integration(LitestarIntegration) is None:
return await send(message)
if sentry_sdk.traces.get_current_span() is None:
return await send(message)
with sentry_sdk.traces.start_span(
name=getattr(send, "__qualname__", str(send)),
attributes={
"sentry.op": OP.MIDDLEWARE_LITESTAR_SEND,
"sentry.origin": LitestarIntegration.origin,
},
) as span:
span.set_attribute(SPANDATA.MIDDLEWARE_NAME, middleware_name)
return await send(message)

send_name = getattr(send, "__name__", str(send))
send_patched = send_name == "_sentry_send"
new_send = _sentry_send if not send_patched else send

return await old_call(self, scope, new_receive, new_send)

not_yet_patched = old_call.__name__ not in ["_create_span_call"]

Expand Down
Loading
Loading