Repository navigation
Expand file tree
/
Copy pathtest_fastapi.py
More file actions
513 lines (420 loc) · 14 KB
/
Copy pathtest_fastapi.py
File metadata and controls
513 lines (420 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
import json
import logging
import threading
import pytest
from sentry_sdk.integrations.fastapi import FastApiIntegration
from fastapi import FastAPI, Request
from fastapi.testclient import TestClient
from fastapi.middleware.trustedhost import TrustedHostMiddleware
from sentry_sdk import capture_message
from sentry_sdk.integrations.starlette import StarletteIntegration
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
try:
from unittest import mock # python 3.3 and above
except ImportError:
import mock # python < 3.3
def fastapi_app_factory():
app = FastAPI()
@app.get("/error")
async def _error():
capture_message("Hi")
1 / 0
return {"message": "Hi"}
@app.get("/message")
async def _message():
capture_message("Hi")
return {"message": "Hi"}
@app.get("/message/{message_id}")
async def _message_with_id(message_id):
capture_message("Hi")
return {"message": "Hi"}
@app.get("/sync/thread_ids")
def _thread_ids_sync():
return {
"main": str(threading.main_thread().ident),
"active": str(threading.current_thread().ident),
}
@app.get("/async/thread_ids")
async def _thread_ids_async():
return {
"main": str(threading.main_thread().ident),
"active": str(threading.current_thread().ident),
}
return app
@pytest.mark.asyncio
async def test_response(sentry_init, capture_events):
# FastAPI is heavily based on Starlette so we also need
# to enable StarletteIntegration.
# In the future this will be auto enabled.
sentry_init(
integrations=[StarletteIntegration(), FastApiIntegration()],
traces_sample_rate=1.0,
send_default_pii=True,
debug=True,
)
app = fastapi_app_factory()
events = capture_events()
client = TestClient(app)
response = client.get("/message")
assert response.json() == {"message": "Hi"}
assert len(events) == 2
(message_event, transaction_event) = events
assert message_event["message"] == "Hi"
assert transaction_event["transaction"] == "/message"
@pytest.mark.parametrize(
"url,transaction_style,expected_transaction,expected_source",
[
(
"/message",
"url",
"/message",
"route",
),
(
"/message",
"endpoint",
"tests.integrations.fastapi.test_fastapi.fastapi_app_factory.<locals>._message",
"component",
),
(
"/message/123456",
"url",
"/message/{message_id}",
"route",
),
(
"/message/123456",
"endpoint",
"tests.integrations.fastapi.test_fastapi.fastapi_app_factory.<locals>._message_with_id",
"component",
),
],
)
def test_transaction_style(
sentry_init,
capture_events,
url,
transaction_style,
expected_transaction,
expected_source,
):
sentry_init(
integrations=[
StarletteIntegration(transaction_style=transaction_style),
FastApiIntegration(transaction_style=transaction_style),
],
)
app = fastapi_app_factory()
events = capture_events()
client = TestClient(app)
client.get(url)
(event,) = events
assert event["transaction"] == expected_transaction
assert event["transaction_info"] == {"source": expected_source}
# Assert that state is not leaked
events.clear()
capture_message("foo")
(event,) = events
assert "request" not in event
assert "transaction" not in event
def test_legacy_setup(
sentry_init,
capture_events,
):
# Check that behaviour does not change
# if the user just adds the new Integrations
# and forgets to remove SentryAsgiMiddleware
sentry_init()
app = fastapi_app_factory()
asgi_app = SentryAsgiMiddleware(app)
events = capture_events()
client = TestClient(asgi_app)
client.get("/message/123456")
(event,) = events
assert event["transaction"] == "/message/{message_id}"
@pytest.mark.parametrize("endpoint", ["/sync/thread_ids", "/async/thread_ids"])
@mock.patch("sentry_sdk.profiler.PROFILE_MINIMUM_SAMPLES", 0)
def test_active_thread_id(sentry_init, capture_envelopes, teardown_profiling, endpoint):
sentry_init(
traces_sample_rate=1.0,
_experiments={"profiles_sample_rate": 1.0},
)
app = fastapi_app_factory()
asgi_app = SentryAsgiMiddleware(app)
envelopes = capture_envelopes()
client = TestClient(asgi_app)
response = client.get(endpoint)
assert response.status_code == 200
data = json.loads(response.content)
envelopes = [envelope for envelope in envelopes]
assert len(envelopes) == 1
profiles = [item for item in envelopes[0].items if item.type == "profile"]
assert len(profiles) == 1
for profile in profiles:
transactions = profile.payload.json["transactions"]
assert len(transactions) == 1
assert str(data["active"]) == transactions[0]["active_thread_id"]
@pytest.mark.asyncio
async def test_original_request_not_scrubbed(sentry_init, capture_events):
sentry_init(
integrations=[StarletteIntegration(), FastApiIntegration()],
traces_sample_rate=1.0,
debug=True,
)
app = FastAPI()
@app.post("/error")
async def _error(request: Request):
logging.critical("Oh no!")
assert request.headers["Authorization"] == "Bearer ohno"
assert await request.json() == {"password": "secret"}
return {"error": "Oh no!"}
events = capture_events()
client = TestClient(app)
client.post(
"/error", json={"password": "secret"}, headers={"Authorization": "Bearer ohno"}
)
event = events[0]
assert event["request"]["data"] == {"password": "[Filtered]"}
assert event["request"]["headers"]["authorization"] == "[Filtered]"
@pytest.mark.asyncio
def test_response_status_code_ok_in_transaction_context(sentry_init, capture_envelopes):
"""
Tests that the response status code is added to the transaction "response" context.
"""
sentry_init(
integrations=[StarletteIntegration(), FastApiIntegration()],
traces_sample_rate=1.0,
release="demo-release",
)
envelopes = capture_envelopes()
app = fastapi_app_factory()
client = TestClient(app)
client.get("/message")
(_, transaction_envelope) = envelopes
transaction = transaction_envelope.get_transaction_event()
assert transaction["type"] == "transaction"
assert len(transaction["contexts"]) > 0
assert (
"response" in transaction["contexts"].keys()
), "Response context not found in transaction"
assert transaction["contexts"]["response"]["status_code"] == 200
@pytest.mark.asyncio
def test_response_status_code_error_in_transaction_context(
sentry_init,
capture_envelopes,
):
"""
Tests that the response status code is added to the transaction "response" context.
"""
sentry_init(
integrations=[StarletteIntegration(), FastApiIntegration()],
traces_sample_rate=1.0,
release="demo-release",
)
envelopes = capture_envelopes()
app = fastapi_app_factory()
client = TestClient(app)
with pytest.raises(ZeroDivisionError):
client.get("/error")
(
_,
_,
transaction_envelope,
) = envelopes
transaction = transaction_envelope.get_transaction_event()
assert transaction["type"] == "transaction"
assert len(transaction["contexts"]) > 0
assert (
"response" in transaction["contexts"].keys()
), "Response context not found in transaction"
assert transaction["contexts"]["response"]["status_code"] == 500
@pytest.mark.asyncio
def test_response_status_code_not_found_in_transaction_context(
sentry_init,
capture_envelopes,
):
"""
Tests that the response status code is added to the transaction "response" context.
"""
sentry_init(
integrations=[StarletteIntegration(), FastApiIntegration()],
traces_sample_rate=1.0,
release="demo-release",
)
envelopes = capture_envelopes()
app = fastapi_app_factory()
client = TestClient(app)
client.get("/non-existing-route-123")
(transaction_envelope,) = envelopes
transaction = transaction_envelope.get_transaction_event()
assert transaction["type"] == "transaction"
assert len(transaction["contexts"]) > 0
assert (
"response" in transaction["contexts"].keys()
), "Response context not found in transaction"
assert transaction["contexts"]["response"]["status_code"] == 404
@pytest.mark.parametrize(
"request_url,transaction_style,expected_transaction_name,expected_transaction_source",
[
(
"/message/123456",
"endpoint",
"tests.integrations.fastapi.test_fastapi.fastapi_app_factory.<locals>._message_with_id",
"component",
),
(
"/message/123456",
"url",
"/message/{message_id}",
"route",
),
],
)
def test_transaction_name(
sentry_init,
request_url,
transaction_style,
expected_transaction_name,
expected_transaction_source,
capture_envelopes,
):
"""
Tests that the transaction name is something meaningful.
"""
sentry_init(
auto_enabling_integrations=False, # Make sure that httpx integration is not added, because it adds tracing information to the starlette test clients request.
integrations=[
StarletteIntegration(transaction_style=transaction_style),
FastApiIntegration(transaction_style=transaction_style),
],
traces_sample_rate=1.0,
debug=True,
)
envelopes = capture_envelopes()
app = fastapi_app_factory()
client = TestClient(app)
client.get(request_url)
(_, transaction_envelope) = envelopes
transaction_event = transaction_envelope.get_transaction_event()
assert transaction_event["transaction"] == expected_transaction_name
assert (
transaction_event["transaction_info"]["source"] == expected_transaction_source
)
def test_route_endpoint_equal_dependant_call(sentry_init):
"""
Tests that the route endpoint name is equal to the wrapped dependant call name.
"""
sentry_init(
auto_enabling_integrations=False, # Make sure that httpx integration is not added, because it adds tracing information to the starlette test clients request.
integrations=[
StarletteIntegration(),
FastApiIntegration(),
],
traces_sample_rate=1.0,
debug=True,
)
app = fastapi_app_factory()
for route in app.router.routes:
if not hasattr(route, "dependant"):
continue
assert route.endpoint.__qualname__ == route.dependant.call.__qualname__
@pytest.mark.parametrize(
"request_url,transaction_style,expected_transaction_name,expected_transaction_source",
[
(
"/message/123456",
"endpoint",
"http://testserver/message/123456",
"url",
),
(
"/message/123456",
"url",
"http://testserver/message/123456",
"url",
),
],
)
def test_transaction_name_in_traces_sampler(
sentry_init,
request_url,
transaction_style,
expected_transaction_name,
expected_transaction_source,
):
"""
Tests that a custom traces_sampler retrieves a meaningful transaction name.
In this case the URL or endpoint, because we do not have the route yet.
"""
def dummy_traces_sampler(sampling_context):
assert (
sampling_context["transaction_context"]["name"] == expected_transaction_name
)
assert (
sampling_context["transaction_context"]["source"]
== expected_transaction_source
)
sentry_init(
auto_enabling_integrations=False, # Make sure that httpx integration is not added, because it adds tracing information to the starlette test clients request.
integrations=[StarletteIntegration(transaction_style=transaction_style)],
traces_sampler=dummy_traces_sampler,
traces_sample_rate=1.0,
debug=True,
)
app = fastapi_app_factory()
client = TestClient(app)
client.get(request_url)
@pytest.mark.parametrize(
"request_url,transaction_style,expected_transaction_name,expected_transaction_source",
[
(
"/message/123456",
"endpoint",
"starlette.middleware.trustedhost.TrustedHostMiddleware",
"component",
),
(
"/message/123456",
"url",
"http://testserver/message/123456",
"url",
),
],
)
def test_transaction_name_in_middleware(
sentry_init,
request_url,
transaction_style,
expected_transaction_name,
expected_transaction_source,
capture_envelopes,
):
"""
Tests that the transaction name is something meaningful.
"""
sentry_init(
auto_enabling_integrations=False, # Make sure that httpx integration is not added, because it adds tracing information to the starlette test clients request.
integrations=[
StarletteIntegration(transaction_style=transaction_style),
FastApiIntegration(transaction_style=transaction_style),
],
traces_sample_rate=1.0,
debug=True,
)
envelopes = capture_envelopes()
app = fastapi_app_factory()
app.add_middleware(
TrustedHostMiddleware,
allowed_hosts=[
"example.com",
],
)
client = TestClient(app)
client.get(request_url)
(transaction_envelope,) = envelopes
transaction_event = transaction_envelope.get_transaction_event()
assert transaction_event["contexts"]["response"]["status_code"] == 400
assert transaction_event["transaction"] == expected_transaction_name
assert (
transaction_event["transaction_info"]["source"] == expected_transaction_source
)