|
2 | 2 | from copy import deepcopy |
3 | 3 | from functools import wraps |
4 | 4 |
|
| 5 | +import sentry_sdk |
5 | 6 | from sentry_sdk._types import TYPE_CHECKING |
6 | | -from sentry_sdk.hub import Hub, _should_send_default_pii |
7 | 7 | from sentry_sdk.integrations import DidNotEnable |
8 | 8 | from sentry_sdk.scope import Scope |
9 | 9 | 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 | +) |
11 | 15 |
|
12 | 16 | if TYPE_CHECKING: |
13 | 17 | from typing import Any, Callable, Dict |
@@ -84,54 +88,53 @@ def _sentry_get_request_handler(*args, **kwargs): |
84 | 88 | @wraps(old_call) |
85 | 89 | def _sentry_call(*args, **kwargs): |
86 | 90 | # 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) |
92 | 95 |
|
93 | 96 | dependant.call = _sentry_call |
94 | 97 |
|
95 | 98 | old_app = old_get_request_handler(*args, **kwargs) |
96 | 99 |
|
| 100 | + @ensure_integration_enabled_async(FastApiIntegration, old_app) |
97 | 101 | async def _sentry_app(*args, **kwargs): |
98 | 102 | # 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) |
104 | 104 | request = args[0] |
105 | 105 |
|
106 | 106 | _set_transaction_name_and_source( |
107 | 107 | Scope.get_current_scope(), integration.transaction_style, request |
108 | 108 | ) |
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 | + ) |
135 | 138 |
|
136 | 139 | return await old_app(*args, **kwargs) |
137 | 140 |
|
|
0 commit comments