@@ -234,6 +234,25 @@ def setUpClass(cls):
234234
235235 super (TestBaseManualFastAPI , cls ).setUpClass ()
236236
237+ def test_fastapi_unhandled_exception (self ):
238+ """If the application has an unhandled error the instrumentation should capture that a 500 response is returned."""
239+ try :
240+ resp = self ._client .get ("/error" )
241+ assert resp .status_code == 500 , resp .content # pragma: no cover, for debugging this test if an exception is _not_ raised
242+ except UnhandledException :
243+ pass
244+ else :
245+ self .fail ("Expected UnhandledException" )
246+
247+ spans = self .memory_exporter .get_finished_spans ()
248+ self .assertEqual (len (spans ), 3 )
249+ span = spans [0 ]
250+ assert span .name == "GET /error http send"
251+ assert span .attributes [SpanAttributes .HTTP_STATUS_CODE ] == 500
252+ span = spans [2 ]
253+ assert span .name == "GET /error"
254+ assert span .attributes [SpanAttributes .HTTP_TARGET ] == "/error"
255+
237256 def test_sub_app_fastapi_call (self ):
238257 """
239258 This test is to ensure that a span in case of a sub app targeted contains the correct server url
@@ -412,26 +431,6 @@ def test_fastapi_excluded_urls(self):
412431 spans = self .memory_exporter .get_finished_spans ()
413432 self .assertEqual (len (spans ), 0 )
414433
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-
435434 def test_fastapi_excluded_urls_not_env (self ):
436435 """Ensure that given fastapi routes are excluded when passed explicitly (not in the environment)"""
437436 app = self ._create_app_explicit_excluded_urls ()
@@ -1010,6 +1009,10 @@ async def _(param: str):
10101009 async def _ ():
10111010 return {"message" : "ok" }
10121011
1012+ @app .get ("/error" )
1013+ async def _ ():
1014+ raise UnhandledException ("This is an unhandled exception" )
1015+
10131016 app .mount ("/sub" , app = sub_app )
10141017
10151018 return app
0 commit comments