31
31
@pytest .mark .asyncio
32
32
@pytest .mark .forked
33
33
async def test_basic (sentry_init , capture_events , application ):
34
- sentry_init (integrations = [DjangoIntegration ()], send_default_pii = True )
34
+ sentry_init (
35
+ integrations = [DjangoIntegration ()],
36
+ send_default_pii = True ,
37
+ )
35
38
36
39
events = capture_events ()
37
40
38
41
comm = HttpCommunicator (application , "GET" , "/view-exc?test=query" )
39
42
response = await comm .get_response ()
43
+ await comm .wait ()
44
+
40
45
assert response ["status" ] == 500
41
46
42
47
(event ,) = events
@@ -67,12 +72,17 @@ async def test_basic(sentry_init, capture_events, application):
67
72
django .VERSION < (3 , 1 ), reason = "async views have been introduced in Django 3.1"
68
73
)
69
74
async def test_async_views (sentry_init , capture_events , application ):
70
- sentry_init (integrations = [DjangoIntegration ()], send_default_pii = True )
75
+ sentry_init (
76
+ integrations = [DjangoIntegration ()],
77
+ send_default_pii = True ,
78
+ )
71
79
72
80
events = capture_events ()
73
81
74
82
comm = HttpCommunicator (application , "GET" , "/async_message" )
75
83
response = await comm .get_response ()
84
+ await comm .wait ()
85
+
76
86
assert response ["status" ] == 200
77
87
78
88
(event ,) = events
@@ -108,17 +118,16 @@ async def test_active_thread_id(sentry_init, capture_envelopes, endpoint, applic
108
118
109
119
comm = HttpCommunicator (application , "GET" , endpoint )
110
120
response = await comm .get_response ()
111
- assert response ["status" ] == 200 , response ["body" ]
112
-
113
121
await comm .wait ()
114
122
115
- data = json .loads (response ["body" ])
116
- envelopes = [envelope for envelope in envelopes ]
123
+ assert response ["status" ] == 200 , response ["body" ]
117
124
assert len (envelopes ) == 1
118
125
119
126
profiles = [item for item in envelopes [0 ].items if item .type == "profile" ]
120
127
assert len (profiles ) == 1
121
128
129
+ data = json .loads (response ["body" ])
130
+
122
131
for profile in profiles :
123
132
transactions = profile .payload .json ["transactions" ]
124
133
assert len (transactions ) == 1
@@ -137,7 +146,10 @@ async def test_async_views_concurrent_execution(sentry_init, settings):
137
146
settings .MIDDLEWARE = []
138
147
asgi_application .load_middleware (is_async = True )
139
148
140
- sentry_init (integrations = [DjangoIntegration ()], send_default_pii = True )
149
+ sentry_init (
150
+ integrations = [DjangoIntegration ()],
151
+ send_default_pii = True ,
152
+ )
141
153
142
154
comm = HttpCommunicator (
143
155
asgi_application , "GET" , "/my_async_view"
@@ -181,7 +193,10 @@ async def test_async_middleware_that_is_function_concurrent_execution(
181
193
]
182
194
asgi_application .load_middleware (is_async = True )
183
195
184
- sentry_init (integrations = [DjangoIntegration ()], send_default_pii = True )
196
+ sentry_init (
197
+ integrations = [DjangoIntegration ()],
198
+ send_default_pii = True ,
199
+ )
185
200
186
201
comm = HttpCommunicator (
187
202
asgi_application , "GET" , "/my_async_view"
@@ -233,13 +248,13 @@ async def test_async_middleware_spans(
233
248
234
249
events = capture_events ()
235
250
236
- comm = HttpCommunicator (asgi_application , "GET" , "/async_message " )
251
+ comm = HttpCommunicator (asgi_application , "GET" , "/simple_async_view " )
237
252
response = await comm .get_response ()
238
- assert response ["status" ] == 200
239
-
240
253
await comm .wait ()
241
254
242
- message , transaction = events
255
+ assert response ["status" ] == 200
256
+
257
+ (transaction ,) = events
243
258
244
259
assert (
245
260
render_span_tree (transaction )
@@ -252,7 +267,7 @@ async def test_async_middleware_spans(
252
267
- op="middleware.django": description="django.middleware.csrf.CsrfViewMiddleware.__acall__"
253
268
- op="middleware.django": description="tests.integrations.django.myapp.settings.TestMiddleware.__acall__"
254
269
- op="middleware.django": description="django.middleware.csrf.CsrfViewMiddleware.process_view"
255
- - op="view.render": description="async_message "
270
+ - op="view.render": description="simple_async_view "
256
271
- op="event.django": description="django.db.close_old_connections"
257
272
- op="event.django": description="django.core.cache.close_caches"
258
273
- op="event.django": description="django.core.handlers.base.reset_urlconf\" """
@@ -265,27 +280,25 @@ async def test_async_middleware_spans(
265
280
django .VERSION < (3 , 1 ), reason = "async views have been introduced in Django 3.1"
266
281
)
267
282
async def test_has_trace_if_performance_enabled (sentry_init , capture_events ):
268
- sentry_init (integrations = [DjangoIntegration ()], traces_sample_rate = 1.0 )
283
+ sentry_init (
284
+ integrations = [DjangoIntegration ()],
285
+ traces_sample_rate = 1.0 ,
286
+ )
269
287
270
288
events = capture_events ()
271
289
272
290
comm = HttpCommunicator (asgi_application , "GET" , "/view-exc-with-msg" )
273
291
response = await comm .get_response ()
274
- assert response ["status" ] == 500
275
-
276
- # ASGI Django does not create transactions per default,
277
- # so we do not have a transaction_event here.
278
- (msg_event , error_event ) = events
292
+ await comm .wait ()
279
293
280
- assert msg_event ["contexts" ]["trace" ]
281
- assert "trace_id" in msg_event ["contexts" ]["trace" ]
294
+ assert response ["status" ] == 500
282
295
283
- assert error_event ["contexts" ]["trace" ]
284
- assert "trace_id" in error_event ["contexts" ]["trace" ]
296
+ (msg_event , error_event , transaction_event ) = events
285
297
286
298
assert (
287
299
msg_event ["contexts" ]["trace" ]["trace_id" ]
288
300
== error_event ["contexts" ]["trace" ]["trace_id" ]
301
+ == transaction_event ["contexts" ]["trace" ]["trace_id" ]
289
302
)
290
303
291
304
@@ -295,12 +308,16 @@ async def test_has_trace_if_performance_enabled(sentry_init, capture_events):
295
308
django .VERSION < (3 , 1 ), reason = "async views have been introduced in Django 3.1"
296
309
)
297
310
async def test_has_trace_if_performance_disabled (sentry_init , capture_events ):
298
- sentry_init (integrations = [DjangoIntegration ()])
311
+ sentry_init (
312
+ integrations = [DjangoIntegration ()],
313
+ )
299
314
300
315
events = capture_events ()
301
316
302
317
comm = HttpCommunicator (asgi_application , "GET" , "/view-exc-with-msg" )
303
318
response = await comm .get_response ()
319
+ await comm .wait ()
320
+
304
321
assert response ["status" ] == 500
305
322
306
323
(msg_event , error_event ) = events
@@ -322,7 +339,10 @@ async def test_has_trace_if_performance_disabled(sentry_init, capture_events):
322
339
django .VERSION < (3 , 1 ), reason = "async views have been introduced in Django 3.1"
323
340
)
324
341
async def test_trace_from_headers_if_performance_enabled (sentry_init , capture_events ):
325
- sentry_init (integrations = [DjangoIntegration ()], traces_sample_rate = 1.0 )
342
+ sentry_init (
343
+ integrations = [DjangoIntegration ()],
344
+ traces_sample_rate = 1.0 ,
345
+ )
326
346
327
347
events = capture_events ()
328
348
@@ -336,20 +356,15 @@ async def test_trace_from_headers_if_performance_enabled(sentry_init, capture_ev
336
356
headers = [(b"sentry-trace" , sentry_trace_header .encode ())],
337
357
)
338
358
response = await comm .get_response ()
339
- assert response [ "status" ] == 500
359
+ await comm . wait ()
340
360
341
- # ASGI Django does not create transactions per default,
342
- # so we do not have a transaction_event here.
343
- (msg_event , error_event ) = events
361
+ assert response ["status" ] == 500
344
362
345
- assert msg_event ["contexts" ]["trace" ]
346
- assert "trace_id" in msg_event ["contexts" ]["trace" ]
347
-
348
- assert error_event ["contexts" ]["trace" ]
349
- assert "trace_id" in error_event ["contexts" ]["trace" ]
363
+ (msg_event , error_event , transaction_event ) = events
350
364
351
365
assert msg_event ["contexts" ]["trace" ]["trace_id" ] == trace_id
352
366
assert error_event ["contexts" ]["trace" ]["trace_id" ] == trace_id
367
+ assert transaction_event ["contexts" ]["trace" ]["trace_id" ] == trace_id
353
368
354
369
355
370
@pytest .mark .asyncio
@@ -358,7 +373,9 @@ async def test_trace_from_headers_if_performance_enabled(sentry_init, capture_ev
358
373
django .VERSION < (3 , 1 ), reason = "async views have been introduced in Django 3.1"
359
374
)
360
375
async def test_trace_from_headers_if_performance_disabled (sentry_init , capture_events ):
361
- sentry_init (integrations = [DjangoIntegration ()])
376
+ sentry_init (
377
+ integrations = [DjangoIntegration ()],
378
+ )
362
379
363
380
events = capture_events ()
364
381
@@ -372,16 +389,12 @@ async def test_trace_from_headers_if_performance_disabled(sentry_init, capture_e
372
389
headers = [(b"sentry-trace" , sentry_trace_header .encode ())],
373
390
)
374
391
response = await comm .get_response ()
392
+ await comm .wait ()
393
+
375
394
assert response ["status" ] == 500
376
395
377
396
(msg_event , error_event ) = events
378
397
379
- assert msg_event ["contexts" ]["trace" ]
380
- assert "trace_id" in msg_event ["contexts" ]["trace" ]
381
-
382
- assert error_event ["contexts" ]["trace" ]
383
- assert "trace_id" in error_event ["contexts" ]["trace" ]
384
-
385
398
assert msg_event ["contexts" ]["trace" ]["trace_id" ] == trace_id
386
399
assert error_event ["contexts" ]["trace" ]["trace_id" ] == trace_id
387
400
@@ -504,10 +517,8 @@ async def test_asgi_request_body(
504
517
expected_data ,
505
518
):
506
519
sentry_init (
520
+ integrations = [DjangoIntegration ()],
507
521
send_default_pii = send_default_pii ,
508
- integrations = [
509
- DjangoIntegration (),
510
- ],
511
522
)
512
523
513
524
envelopes = capture_envelopes ()
@@ -520,9 +531,9 @@ async def test_asgi_request_body(
520
531
body = body ,
521
532
)
522
533
response = await comm .get_response ()
523
- assert response ["status" ] == 200
524
-
525
534
await comm .wait ()
535
+
536
+ assert response ["status" ] == 200
526
537
assert response ["body" ] == body
527
538
528
539
(envelope ,) = envelopes
@@ -594,3 +605,22 @@ def get_response(): ...
594
605
595
606
instance = sentry_asgi_mixin (get_response )
596
607
assert not inspect .iscoroutinefunction (instance )
608
+
609
+
610
+ @pytest .mark .parametrize ("application" , APPS )
611
+ @pytest .mark .asyncio
612
+ async def test_async_view (sentry_init , capture_events , application ):
613
+ sentry_init (
614
+ integrations = [DjangoIntegration ()],
615
+ traces_sample_rate = 1.0 ,
616
+ )
617
+
618
+ events = capture_events ()
619
+
620
+ comm = HttpCommunicator (application , "GET" , "/simple_async_view" )
621
+ await comm .get_response ()
622
+ await comm .wait ()
623
+
624
+ (event ,) = events
625
+ assert event ["type" ] == "transaction"
626
+ assert event ["transaction" ] == "/simple_async_view"
0 commit comments