diff --git a/sentry_sdk/integrations/fastapi.py b/sentry_sdk/integrations/fastapi.py index 1c21196b76..d38e978fbf 100644 --- a/sentry_sdk/integrations/fastapi.py +++ b/sentry_sdk/integrations/fastapi.py @@ -11,7 +11,7 @@ if MYPY: from typing import Any, Callable, Dict - from sentry_sdk._types import Event + from sentry_sdk.scope import Scope try: import fastapi # type: ignore @@ -31,8 +31,8 @@ def setup_once(): patch_get_request_handler() -def _set_transaction_name_and_source(event, transaction_style, request): - # type: (Event, str, Any) -> None +def _set_transaction_name_and_source(scope, transaction_style, request): + # type: (Scope, str, Any) -> None name = "" if transaction_style == "endpoint": @@ -48,12 +48,12 @@ def _set_transaction_name_and_source(event, transaction_style, request): name = path if not name: - event["transaction"] = _DEFAULT_TRANSACTION_NAME - event["transaction_info"] = {"source": TRANSACTION_SOURCE_ROUTE} - return + name = _DEFAULT_TRANSACTION_NAME + source = TRANSACTION_SOURCE_ROUTE + else: + source = SOURCE_FOR_STYLE[transaction_style] - event["transaction"] = name - event["transaction_info"] = {"source": SOURCE_FOR_STYLE[transaction_style]} + scope.set_transaction_name(name, source=source) def patch_get_request_handler(): @@ -73,6 +73,11 @@ async def _sentry_app(*args, **kwargs): with hub.configure_scope() as sentry_scope: request = args[0] + + _set_transaction_name_and_source( + sentry_scope, integration.transaction_style, request + ) + extractor = StarletteRequestExtractor(request) info = await extractor.extract_request_info() @@ -90,10 +95,6 @@ def event_processor(event, hint): request_info["data"] = info["data"] event["request"] = request_info - _set_transaction_name_and_source( - event, integration.transaction_style, req - ) - return event return event_processor diff --git a/sentry_sdk/integrations/starlette.py b/sentry_sdk/integrations/starlette.py index 109b048bd3..155c840461 100644 --- a/sentry_sdk/integrations/starlette.py +++ b/sentry_sdk/integrations/starlette.py @@ -24,7 +24,7 @@ if MYPY: from typing import Any, Awaitable, Callable, Dict, Optional - from sentry_sdk._types import Event + from sentry_sdk.scope import Scope as SentryScope try: import starlette # type: ignore @@ -36,7 +36,7 @@ ) from starlette.requests import Request # type: ignore from starlette.routing import Match # type: ignore - from starlette.types import ASGIApp, Receive, Scope, Send # type: ignore + from starlette.types import ASGIApp, Receive, Scope as StarletteScope, Send # type: ignore except ImportError: raise DidNotEnable("Starlette is not installed") @@ -312,7 +312,7 @@ def patch_asgi_app(): old_app = Starlette.__call__ async def _sentry_patched_asgi_app(self, scope, receive, send): - # type: (Starlette, Scope, Receive, Send) -> None + # type: (Starlette, StarletteScope, Receive, Send) -> None if Hub.current.get_integration(StarletteIntegration) is None: return await old_app(self, scope, receive, send) @@ -359,6 +359,11 @@ async def _sentry_async_func(*args, **kwargs): with hub.configure_scope() as sentry_scope: request = args[0] + + _set_transaction_name_and_source( + sentry_scope, integration.transaction_style, request + ) + extractor = StarletteRequestExtractor(request) info = await extractor.extract_request_info() @@ -376,10 +381,6 @@ def event_processor(event, hint): request_info["data"] = info["data"] event["request"] = request_info - _set_transaction_name_and_source( - event, integration.transaction_style, req - ) - return event return event_processor @@ -403,6 +404,11 @@ def _sentry_sync_func(*args, **kwargs): with hub.configure_scope() as sentry_scope: request = args[0] + + _set_transaction_name_and_source( + sentry_scope, integration.transaction_style, request + ) + extractor = StarletteRequestExtractor(request) cookies = extractor.extract_cookies_from_request() @@ -418,10 +424,6 @@ def event_processor(event, hint): event["request"] = request_info - _set_transaction_name_and_source( - event, integration.transaction_style, req - ) - return event return event_processor @@ -550,8 +552,8 @@ async def json(self): return await self.request.json() -def _set_transaction_name_and_source(event, transaction_style, request): - # type: (Event, str, Any) -> None +def _set_transaction_name_and_source(scope, transaction_style, request): + # type: (SentryScope, str, Any) -> None name = "" if transaction_style == "endpoint": @@ -573,9 +575,9 @@ def _set_transaction_name_and_source(event, transaction_style, request): break if not name: - event["transaction"] = _DEFAULT_TRANSACTION_NAME - event["transaction_info"] = {"source": TRANSACTION_SOURCE_ROUTE} - return + name = _DEFAULT_TRANSACTION_NAME + source = TRANSACTION_SOURCE_ROUTE + else: + source = SOURCE_FOR_STYLE[transaction_style] - event["transaction"] = name - event["transaction_info"] = {"source": SOURCE_FOR_STYLE[transaction_style]} + scope.set_transaction_name(name, source=source)