@@ -170,7 +170,8 @@ def setUp(self):
170170 self ._instrumentor = otel_fastapi .FastAPIInstrumentor ()
171171 self ._app = self ._create_app ()
172172 self ._app .add_middleware (HTTPSRedirectMiddleware )
173- self ._client = TestClient (self ._app )
173+ self ._client = TestClient (self ._app , base_url = "https://testserver:443" )
174+ self ._client .__enter__ () # run the lifespan, initialize the middleware stack
174175
175176 def tearDown (self ):
176177 super ().tearDown ()
@@ -205,19 +206,11 @@ async def _(param: str):
205206 async def _ ():
206207 return {"message" : "ok" }
207208
208- @app .get ("/error" )
209- async def _ ():
210- raise UnhandledException ("This is an unhandled exception" )
211-
212209 app .mount ("/sub" , app = sub_app )
213210
214211 return app
215212
216213
217- class UnhandledException (Exception ):
218- pass
219-
220-
221214class TestBaseManualFastAPI (TestBaseFastAPI ):
222215 @classmethod
223216 def setUpClass (cls ):
@@ -406,26 +399,6 @@ def test_fastapi_excluded_urls(self):
406399 spans = self .memory_exporter .get_finished_spans ()
407400 self .assertEqual (len (spans ), 0 )
408401
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-
429402 def test_fastapi_excluded_urls_not_env (self ):
430403 """Ensure that given fastapi routes are excluded when passed explicitly (not in the environment)"""
431404 app = self ._create_app_explicit_excluded_urls ()
@@ -1152,9 +1125,13 @@ def test_request(self):
11521125 def test_mulitple_way_instrumentation (self ):
11531126 self ._instrumentor .instrument_app (self ._app )
11541127 count = 0
1155- for middleware in self ._app .user_middleware :
1156- if middleware .cls is OpenTelemetryMiddleware :
1128+ app = self ._app .middleware_stack
1129+ while True :
1130+ if isinstance (app , OpenTelemetryMiddleware ):
11571131 count += 1
1132+ if app is None :
1133+ break
1134+ app = getattr (app , "app" , None )
11581135 self .assertEqual (count , 1 )
11591136
11601137 def test_uninstrument_after_instrument (self ):
0 commit comments