@@ -204,12 +204,20 @@ async def _(param: str):
204204 @app .get ("/healthzz" )
205205 async def _ ():
206206 return {"message" : "ok" }
207+
208+ @app .get ("/error" )
209+ async def _ ():
210+ raise UnhandledException ("This is an unhandled exception" )
207211
208212 app .mount ("/sub" , app = sub_app )
209213
210214 return app
211215
212216
217+ class UnhandledException (Exception ):
218+ pass
219+
220+
213221class TestBaseManualFastAPI (TestBaseFastAPI ):
214222 @classmethod
215223 def setUpClass (cls ):
@@ -398,6 +406,26 @@ def test_fastapi_excluded_urls(self):
398406 spans = self .memory_exporter .get_finished_spans ()
399407 self .assertEqual (len (spans ), 0 )
400408
409+ def test_fastapi_unhandled_exception (self ):
410+ """If the application has an unhandled error the instrumentation should capture that a 500 response is returned."""
411+ try :
412+ self ._client .get ("/error" )
413+ except UnhandledException :
414+ pass
415+ else :
416+ self .fail ("Expected UnhandledException" )
417+
418+ spans = self .memory_exporter .get_finished_spans ()
419+ self .assertEqual (len (spans ), 3 )
420+ for span in spans :
421+ self .assertIn ("GET /error" , span .name )
422+ self .assertEqual (
423+ span .attributes [SpanAttributes .HTTP_ROUTE ], "/error"
424+ )
425+ self .assertEqual (
426+ span .attributes [SpanAttributes .HTTP_STATUS_CODE ], 500
427+ )
428+
401429 def test_fastapi_excluded_urls_not_env (self ):
402430 """Ensure that given fastapi routes are excluded when passed explicitly (not in the environment)"""
403431 app = self ._create_app_explicit_excluded_urls ()
0 commit comments