Skip to content

Commit

Permalink
Move set_transaction_name out of event processor in fastapi/starlette (
Browse files Browse the repository at this point in the history
  • Loading branch information
sl0thentr0py committed Nov 22, 2022
1 parent bd99d4e commit 19cb5f2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 30 deletions.
25 changes: 13 additions & 12 deletions sentry_sdk/integrations/fastapi.py
Expand Up @@ -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
Expand All @@ -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":
Expand All @@ -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():
Expand All @@ -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()

Expand All @@ -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
Expand Down
38 changes: 20 additions & 18 deletions sentry_sdk/integrations/starlette.py
Expand Up @@ -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
Expand All @@ -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")

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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()

Expand All @@ -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
Expand All @@ -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()

Expand All @@ -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
Expand Down Expand Up @@ -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":
Expand All @@ -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)

0 comments on commit 19cb5f2

Please sign in to comment.