Skip to content

Commit 19cb5f2

Browse files
authored
Move set_transaction_name out of event processor in fastapi/starlette (#1751)
1 parent bd99d4e commit 19cb5f2

File tree

2 files changed

+33
-30
lines changed

2 files changed

+33
-30
lines changed

sentry_sdk/integrations/fastapi.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
if MYPY:
1212
from typing import Any, Callable, Dict
1313

14-
from sentry_sdk._types import Event
14+
from sentry_sdk.scope import Scope
1515

1616
try:
1717
import fastapi # type: ignore
@@ -31,8 +31,8 @@ def setup_once():
3131
patch_get_request_handler()
3232

3333

34-
def _set_transaction_name_and_source(event, transaction_style, request):
35-
# type: (Event, str, Any) -> None
34+
def _set_transaction_name_and_source(scope, transaction_style, request):
35+
# type: (Scope, str, Any) -> None
3636
name = ""
3737

3838
if transaction_style == "endpoint":
@@ -48,12 +48,12 @@ def _set_transaction_name_and_source(event, transaction_style, request):
4848
name = path
4949

5050
if not name:
51-
event["transaction"] = _DEFAULT_TRANSACTION_NAME
52-
event["transaction_info"] = {"source": TRANSACTION_SOURCE_ROUTE}
53-
return
51+
name = _DEFAULT_TRANSACTION_NAME
52+
source = TRANSACTION_SOURCE_ROUTE
53+
else:
54+
source = SOURCE_FOR_STYLE[transaction_style]
5455

55-
event["transaction"] = name
56-
event["transaction_info"] = {"source": SOURCE_FOR_STYLE[transaction_style]}
56+
scope.set_transaction_name(name, source=source)
5757

5858

5959
def patch_get_request_handler():
@@ -73,6 +73,11 @@ async def _sentry_app(*args, **kwargs):
7373

7474
with hub.configure_scope() as sentry_scope:
7575
request = args[0]
76+
77+
_set_transaction_name_and_source(
78+
sentry_scope, integration.transaction_style, request
79+
)
80+
7681
extractor = StarletteRequestExtractor(request)
7782
info = await extractor.extract_request_info()
7883

@@ -90,10 +95,6 @@ def event_processor(event, hint):
9095
request_info["data"] = info["data"]
9196
event["request"] = request_info
9297

93-
_set_transaction_name_and_source(
94-
event, integration.transaction_style, req
95-
)
96-
9798
return event
9899

99100
return event_processor

sentry_sdk/integrations/starlette.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
if MYPY:
2525
from typing import Any, Awaitable, Callable, Dict, Optional
2626

27-
from sentry_sdk._types import Event
27+
from sentry_sdk.scope import Scope as SentryScope
2828

2929
try:
3030
import starlette # type: ignore
@@ -36,7 +36,7 @@
3636
)
3737
from starlette.requests import Request # type: ignore
3838
from starlette.routing import Match # type: ignore
39-
from starlette.types import ASGIApp, Receive, Scope, Send # type: ignore
39+
from starlette.types import ASGIApp, Receive, Scope as StarletteScope, Send # type: ignore
4040
except ImportError:
4141
raise DidNotEnable("Starlette is not installed")
4242

@@ -312,7 +312,7 @@ def patch_asgi_app():
312312
old_app = Starlette.__call__
313313

314314
async def _sentry_patched_asgi_app(self, scope, receive, send):
315-
# type: (Starlette, Scope, Receive, Send) -> None
315+
# type: (Starlette, StarletteScope, Receive, Send) -> None
316316
if Hub.current.get_integration(StarletteIntegration) is None:
317317
return await old_app(self, scope, receive, send)
318318

@@ -359,6 +359,11 @@ async def _sentry_async_func(*args, **kwargs):
359359

360360
with hub.configure_scope() as sentry_scope:
361361
request = args[0]
362+
363+
_set_transaction_name_and_source(
364+
sentry_scope, integration.transaction_style, request
365+
)
366+
362367
extractor = StarletteRequestExtractor(request)
363368
info = await extractor.extract_request_info()
364369

@@ -376,10 +381,6 @@ def event_processor(event, hint):
376381
request_info["data"] = info["data"]
377382
event["request"] = request_info
378383

379-
_set_transaction_name_and_source(
380-
event, integration.transaction_style, req
381-
)
382-
383384
return event
384385

385386
return event_processor
@@ -403,6 +404,11 @@ def _sentry_sync_func(*args, **kwargs):
403404

404405
with hub.configure_scope() as sentry_scope:
405406
request = args[0]
407+
408+
_set_transaction_name_and_source(
409+
sentry_scope, integration.transaction_style, request
410+
)
411+
406412
extractor = StarletteRequestExtractor(request)
407413
cookies = extractor.extract_cookies_from_request()
408414

@@ -418,10 +424,6 @@ def event_processor(event, hint):
418424

419425
event["request"] = request_info
420426

421-
_set_transaction_name_and_source(
422-
event, integration.transaction_style, req
423-
)
424-
425427
return event
426428

427429
return event_processor
@@ -550,8 +552,8 @@ async def json(self):
550552
return await self.request.json()
551553

552554

553-
def _set_transaction_name_and_source(event, transaction_style, request):
554-
# type: (Event, str, Any) -> None
555+
def _set_transaction_name_and_source(scope, transaction_style, request):
556+
# type: (SentryScope, str, Any) -> None
555557
name = ""
556558

557559
if transaction_style == "endpoint":
@@ -573,9 +575,9 @@ def _set_transaction_name_and_source(event, transaction_style, request):
573575
break
574576

575577
if not name:
576-
event["transaction"] = _DEFAULT_TRANSACTION_NAME
577-
event["transaction_info"] = {"source": TRANSACTION_SOURCE_ROUTE}
578-
return
578+
name = _DEFAULT_TRANSACTION_NAME
579+
source = TRANSACTION_SOURCE_ROUTE
580+
else:
581+
source = SOURCE_FOR_STYLE[transaction_style]
579582

580-
event["transaction"] = name
581-
event["transaction_info"] = {"source": SOURCE_FOR_STYLE[transaction_style]}
583+
scope.set_transaction_name(name, source=source)

0 commit comments

Comments
 (0)