Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref(asgi): Use new scopes API #2874

Merged
merged 2 commits into from
Mar 22, 2024
Merged
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
44 changes: 19 additions & 25 deletions sentry_sdk/integrations/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
from copy import deepcopy
from functools import partial

import sentry_sdk
from sentry_sdk._types import TYPE_CHECKING
from sentry_sdk.api import continue_trace
from sentry_sdk.consts import OP
from sentry_sdk.hub import Hub

from sentry_sdk.integrations._asgi_common import (
_get_headers,
_get_request_data,
_get_url,
)
from sentry_sdk.sessions import auto_session_tracking
from sentry_sdk.sessions import auto_session_tracking_scope
from sentry_sdk.tracing import (
SOURCE_FOR_STYLE,
TRANSACTION_SOURCE_ROUTE,
Expand Down Expand Up @@ -54,17 +54,15 @@
TRANSACTION_STYLE_VALUES = ("endpoint", "url")


def _capture_exception(hub, exc, mechanism_type="asgi"):
# type: (Hub, Any, str) -> None
def _capture_exception(exc, mechanism_type="asgi"):
# type: (Any, str) -> None

# Check client here as it might have been unset while streaming response
if hub.client is not None:
event, hint = event_from_exception(
exc,
client_options=hub.client.options,
mechanism={"type": mechanism_type, "handled": False},
)
hub.capture_event(event, hint=hint)
event, hint = event_from_exception(
exc,
client_options=sentry_sdk.get_client().options,
mechanism={"type": mechanism_type, "handled": False},
)
sentry_sdk.capture_event(event, hint=hint)


def _looks_like_asgi3(app):
Expand Down Expand Up @@ -157,19 +155,17 @@
return await self.app(scope, receive, send)

except Exception as exc:
_capture_exception(Hub.current, exc, mechanism_type=self.mechanism_type)
_capture_exception(exc, mechanism_type=self.mechanism_type)

Check warning on line 158 in sentry_sdk/integrations/asgi.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/asgi.py#L158

Added line #L158 was not covered by tests
raise exc from None

_asgi_middleware_applied.set(True)
try:
hub = Hub(Hub.current)
with hub:
with auto_session_tracking(hub, session_mode="request"):
with hub.configure_scope() as sentry_scope:
sentry_scope.clear_breadcrumbs()
sentry_scope._name = "asgi"
processor = partial(self.event_processor, asgi_scope=scope)
sentry_scope.add_event_processor(processor)
with sentry_sdk.isolation_scope() as sentry_scope:
with auto_session_tracking_scope(sentry_scope, session_mode="request"):
sentry_scope.clear_breadcrumbs()
sentry_scope._name = "asgi"
processor = partial(self.event_processor, asgi_scope=scope)
sentry_scope.add_event_processor(processor)

ty = scope["type"]
(
Expand Down Expand Up @@ -208,7 +204,7 @@
transaction.source,
)

with hub.start_transaction(
with sentry_sdk.start_transaction(
transaction, custom_sampling_context={"asgi_scope": scope}
):
logger.debug("[ASGI] Started transaction: %s", transaction)
Expand All @@ -235,9 +231,7 @@
scope, receive, _sentry_wrapped_send
)
except Exception as exc:
_capture_exception(
hub, exc, mechanism_type=self.mechanism_type
)
_capture_exception(exc, mechanism_type=self.mechanism_type)
raise exc from None
finally:
_asgi_middleware_applied.set(False)
Expand Down
Loading