@@ -210,12 +210,20 @@ async def _(param: str):
210
210
@app .get ("/healthzz" )
211
211
async def _ ():
212
212
return {"message" : "ok" }
213
+
214
+ @app .get ("/error" )
215
+ async def _ ():
216
+ raise UnhandledException ("This is an unhandled exception" )
213
217
214
218
app .mount ("/sub" , app = sub_app )
215
219
216
220
return app
217
221
218
222
223
+ class UnhandledException (Exception ):
224
+ pass
225
+
226
+
219
227
class TestBaseManualFastAPI (TestBaseFastAPI ):
220
228
@classmethod
221
229
def setUpClass (cls ):
@@ -404,6 +412,26 @@ def test_fastapi_excluded_urls(self):
404
412
spans = self .memory_exporter .get_finished_spans ()
405
413
self .assertEqual (len (spans ), 0 )
406
414
415
+ def test_fastapi_unhandled_exception (self ):
416
+ """If the application has an unhandled error the instrumentation should capture that a 500 response is returned."""
417
+ try :
418
+ self ._client .get ("/error" )
419
+ except UnhandledException :
420
+ pass
421
+ else :
422
+ self .fail ("Expected UnhandledException" )
423
+
424
+ spans = self .memory_exporter .get_finished_spans ()
425
+ self .assertEqual (len (spans ), 3 )
426
+ for span in spans :
427
+ self .assertIn ("GET /error" , span .name )
428
+ self .assertEqual (
429
+ span .attributes [SpanAttributes .HTTP_ROUTE ], "/error"
430
+ )
431
+ self .assertEqual (
432
+ span .attributes [SpanAttributes .HTTP_STATUS_CODE ], 500
433
+ )
434
+
407
435
def test_fastapi_excluded_urls_not_env (self ):
408
436
"""Ensure that given fastapi routes are excluded when passed explicitly (not in the environment)"""
409
437
app = self ._create_app_explicit_excluded_urls ()
0 commit comments