1
1
import sys
2
2
import weakref
3
3
4
+ import sentry_sdk
4
5
from sentry_sdk .api import continue_trace
5
6
from sentry_sdk .consts import OP , SPANDATA
6
- from sentry_sdk .hub import Hub
7
7
from sentry_sdk .integrations import Integration , DidNotEnable
8
8
from sentry_sdk .integrations .logging import ignore_logger
9
9
from sentry_sdk .scope import Scope
10
- from sentry_sdk .sessions import auto_session_tracking
10
+ from sentry_sdk .sessions import auto_session_tracking_scope
11
11
from sentry_sdk .integrations ._wsgi_common import (
12
12
_filter_headers ,
13
13
request_body_within_bounds ,
20
20
from sentry_sdk .tracing_utils import should_propagate_trace
21
21
from sentry_sdk .utils import (
22
22
capture_internal_exceptions ,
23
+ ensure_integration_enabled ,
24
+ ensure_integration_enabled_async ,
23
25
event_from_exception ,
24
26
logger ,
25
27
parse_url ,
@@ -96,21 +98,18 @@ def setup_once():
96
98
97
99
old_handle = Application ._handle
98
100
101
+ @ensure_integration_enabled_async (AioHttpIntegration , old_handle )
99
102
async def sentry_app_handle (self , request , * args , ** kwargs ):
100
103
# type: (Any, Request, *Any, **Any) -> Any
101
- hub = Hub .current
102
- if hub .get_integration (AioHttpIntegration ) is None :
103
- return await old_handle (self , request , * args , ** kwargs )
104
-
105
104
weak_request = weakref .ref (request )
106
105
107
- with Hub ( hub ) as hub :
108
- with auto_session_tracking ( hub , session_mode = "request" ):
106
+ with sentry_sdk . isolation_scope ( ) as scope :
107
+ with auto_session_tracking_scope ( scope , session_mode = "request" ):
109
108
# Scope data will not leak between requests because aiohttp
110
109
# create a task to wrap each request.
111
- with hub . configure_scope () as scope :
112
- scope .clear_breadcrumbs ()
113
- scope .add_event_processor (_make_request_processor (weak_request ))
110
+ scope . generate_propagation_context ()
111
+ scope .clear_breadcrumbs ()
112
+ scope .add_event_processor (_make_request_processor (weak_request ))
114
113
115
114
headers = dict (request .headers )
116
115
transaction = continue_trace (
@@ -121,7 +120,7 @@ async def sentry_app_handle(self, request, *args, **kwargs):
121
120
name = "generic AIOHTTP request" ,
122
121
source = TRANSACTION_SOURCE_ROUTE ,
123
122
)
124
- with hub .start_transaction (
123
+ with sentry_sdk .start_transaction (
125
124
transaction ,
126
125
custom_sampling_context = {"aiohttp_request" : request },
127
126
):
@@ -136,7 +135,7 @@ async def sentry_app_handle(self, request, *args, **kwargs):
136
135
except Exception :
137
136
# This will probably map to a 500 but seems like we
138
137
# have no way to tell. Do not set span status.
139
- reraise (* _capture_exception (hub ))
138
+ reraise (* _capture_exception ())
140
139
141
140
transaction .set_http_status (response .status )
142
141
return response
@@ -149,8 +148,7 @@ async def sentry_urldispatcher_resolve(self, request):
149
148
# type: (UrlDispatcher, Request) -> UrlMappingMatchInfo
150
149
rv = await old_urldispatcher_resolve (self , request )
151
150
152
- hub = Hub .current
153
- integration = hub .get_integration (AioHttpIntegration )
151
+ integration = sentry_sdk .get_client ().get_integration (AioHttpIntegration )
154
152
155
153
name = None
156
154
@@ -176,12 +174,9 @@ async def sentry_urldispatcher_resolve(self, request):
176
174
177
175
old_client_session_init = ClientSession .__init__
178
176
177
+ @ensure_integration_enabled (AioHttpIntegration , old_client_session_init )
179
178
def init (* args , ** kwargs ):
180
179
# type: (Any, Any) -> None
181
- hub = Hub .current
182
- if hub .get_integration (AioHttpIntegration ) is None :
183
- return old_client_session_init (* args , ** kwargs )
184
-
185
180
client_trace_configs = list (kwargs .get ("trace_configs" ) or ())
186
181
trace_config = create_trace_config ()
187
182
client_trace_configs .append (trace_config )
@@ -194,10 +189,11 @@ def init(*args, **kwargs):
194
189
195
190
def create_trace_config ():
196
191
# type: () -> TraceConfig
192
+
197
193
async def on_request_start (session , trace_config_ctx , params ):
198
194
# type: (ClientSession, SimpleNamespace, TraceRequestStartParams) -> None
199
- hub = Hub . current
200
- if hub .get_integration (AioHttpIntegration ) is None :
195
+ client = sentry_sdk . get_client ()
196
+ if client .get_integration (AioHttpIntegration ) is None :
201
197
return
202
198
203
199
method = params .method .upper ()
@@ -206,7 +202,7 @@ async def on_request_start(session, trace_config_ctx, params):
206
202
with capture_internal_exceptions ():
207
203
parsed_url = parse_url (str (params .url ), sanitize = False )
208
204
209
- span = hub .start_span (
205
+ span = sentry_sdk .start_span (
210
206
op = OP .HTTP_CLIENT ,
211
207
description = "%s %s"
212
208
% (method , parsed_url .url if parsed_url else SENSITIVE_DATA_SUBSTITUTE ),
@@ -217,8 +213,10 @@ async def on_request_start(session, trace_config_ctx, params):
217
213
span .set_data (SPANDATA .HTTP_QUERY , parsed_url .query )
218
214
span .set_data (SPANDATA .HTTP_FRAGMENT , parsed_url .fragment )
219
215
220
- if should_propagate_trace (hub , str (params .url )):
221
- for key , value in hub .iter_trace_propagation_headers (span ):
216
+ if should_propagate_trace (client , str (params .url )):
217
+ for key , value in Scope .get_current_scope ().iter_trace_propagation_headers (
218
+ span = span
219
+ ):
222
220
logger .debug (
223
221
"[Tracing] Adding `{key}` header {value} to outgoing request to {url}." .format (
224
222
key = key , value = value , url = params .url
@@ -275,42 +273,40 @@ def aiohttp_processor(
275
273
request_info ["query_string" ] = request .query_string
276
274
request_info ["method" ] = request .method
277
275
request_info ["env" ] = {"REMOTE_ADDR" : request .remote }
278
-
279
- hub = Hub .current
280
276
request_info ["headers" ] = _filter_headers (dict (request .headers ))
281
277
282
278
# Just attach raw data here if it is within bounds, if available.
283
279
# Unfortunately there's no way to get structured data from aiohttp
284
280
# without awaiting on some coroutine.
285
- request_info ["data" ] = get_aiohttp_request_data (hub , request )
281
+ request_info ["data" ] = get_aiohttp_request_data (request )
286
282
287
283
return event
288
284
289
285
return aiohttp_processor
290
286
291
287
292
- def _capture_exception (hub ):
293
- # type: (Hub ) -> ExcInfo
288
+ def _capture_exception ():
289
+ # type: () -> ExcInfo
294
290
exc_info = sys .exc_info ()
295
291
event , hint = event_from_exception (
296
292
exc_info ,
297
- client_options = hub . client .options , # type: ignore
293
+ client_options = sentry_sdk . get_client () .options ,
298
294
mechanism = {"type" : "aiohttp" , "handled" : False },
299
295
)
300
- hub .capture_event (event , hint = hint )
296
+ sentry_sdk .capture_event (event , hint = hint )
301
297
return exc_info
302
298
303
299
304
300
BODY_NOT_READ_MESSAGE = "[Can't show request body due to implementation details.]"
305
301
306
302
307
- def get_aiohttp_request_data (hub , request ):
308
- # type: (Hub, Request) -> Union[Optional[str], AnnotatedValue]
303
+ def get_aiohttp_request_data (request ):
304
+ # type: (Request) -> Union[Optional[str], AnnotatedValue]
309
305
bytes_body = request ._read_bytes
310
306
311
307
if bytes_body is not None :
312
308
# we have body to show
313
- if not request_body_within_bounds (hub . client , len (bytes_body )):
309
+ if not request_body_within_bounds (sentry_sdk . get_client () , len (bytes_body )):
314
310
return AnnotatedValue .removed_because_over_size_limit ()
315
311
316
312
encoding = request .charset or "utf-8"
0 commit comments