Skip to content

Commit 553045b

Browse files
ref(fastapi): Use new scopes API in FastAPI integration (getsentry#2836)
Fixes getsentryGH-2810
1 parent 7a2c153 commit 553045b

1 file changed

Lines changed: 41 additions & 38 deletions

File tree

sentry_sdk/integrations/fastapi.py

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22
from copy import deepcopy
33
from functools import wraps
44

5+
import sentry_sdk
56
from sentry_sdk._types import TYPE_CHECKING
6-
from sentry_sdk.hub import Hub, _should_send_default_pii
77
from sentry_sdk.integrations import DidNotEnable
88
from sentry_sdk.scope import Scope
99
from sentry_sdk.tracing import SOURCE_FOR_STYLE, TRANSACTION_SOURCE_ROUTE
10-
from sentry_sdk.utils import transaction_from_function, logger
10+
from sentry_sdk.utils import (
11+
transaction_from_function,
12+
logger,
13+
ensure_integration_enabled_async,
14+
)
1115

1216
if TYPE_CHECKING:
1317
from typing import Any, Callable, Dict
@@ -84,54 +88,53 @@ def _sentry_get_request_handler(*args, **kwargs):
8488
@wraps(old_call)
8589
def _sentry_call(*args, **kwargs):
8690
# type: (*Any, **Any) -> Any
87-
hub = Hub.current
88-
with hub.configure_scope() as sentry_scope:
89-
if sentry_scope.profile is not None:
90-
sentry_scope.profile.update_active_thread_id()
91-
return old_call(*args, **kwargs)
91+
sentry_scope = Scope.get_isolation_scope()
92+
if sentry_scope.profile is not None:
93+
sentry_scope.profile.update_active_thread_id()
94+
return old_call(*args, **kwargs)
9295

9396
dependant.call = _sentry_call
9497

9598
old_app = old_get_request_handler(*args, **kwargs)
9699

100+
@ensure_integration_enabled_async(FastApiIntegration, old_app)
97101
async def _sentry_app(*args, **kwargs):
98102
# type: (*Any, **Any) -> Any
99-
hub = Hub.current
100-
integration = hub.get_integration(FastApiIntegration)
101-
if integration is None:
102-
return await old_app(*args, **kwargs)
103-
103+
integration = sentry_sdk.get_client().get_integration(FastApiIntegration)
104104
request = args[0]
105105

106106
_set_transaction_name_and_source(
107107
Scope.get_current_scope(), integration.transaction_style, request
108108
)
109-
with hub.configure_scope() as sentry_scope:
110-
extractor = StarletteRequestExtractor(request)
111-
info = await extractor.extract_request_info()
112-
113-
def _make_request_event_processor(req, integration):
114-
# type: (Any, Any) -> Callable[[Event, Dict[str, Any]], Event]
115-
def event_processor(event, hint):
116-
# type: (Event, Dict[str, Any]) -> Event
117-
118-
# Extract information from request
119-
request_info = event.get("request", {})
120-
if info:
121-
if "cookies" in info and _should_send_default_pii():
122-
request_info["cookies"] = info["cookies"]
123-
if "data" in info:
124-
request_info["data"] = info["data"]
125-
event["request"] = deepcopy(request_info)
126-
127-
return event
128-
129-
return event_processor
130-
131-
sentry_scope._name = FastApiIntegration.identifier
132-
sentry_scope.add_event_processor(
133-
_make_request_event_processor(request, integration)
134-
)
109+
sentry_scope = Scope.get_isolation_scope()
110+
extractor = StarletteRequestExtractor(request)
111+
info = await extractor.extract_request_info()
112+
113+
def _make_request_event_processor(req, integration):
114+
# type: (Any, Any) -> Callable[[Event, Dict[str, Any]], Event]
115+
def event_processor(event, hint):
116+
# type: (Event, Dict[str, Any]) -> Event
117+
118+
# Extract information from request
119+
request_info = event.get("request", {})
120+
if info:
121+
if (
122+
"cookies" in info
123+
and sentry_sdk.get_client().should_send_default_pii()
124+
):
125+
request_info["cookies"] = info["cookies"]
126+
if "data" in info:
127+
request_info["data"] = info["data"]
128+
event["request"] = deepcopy(request_info)
129+
130+
return event
131+
132+
return event_processor
133+
134+
sentry_scope._name = FastApiIntegration.identifier
135+
sentry_scope.add_event_processor(
136+
_make_request_event_processor(request, integration)
137+
)
135138

136139
return await old_app(*args, **kwargs)
137140

0 commit comments

Comments
 (0)