Skip to content

Commit 0647245

Browse files
committed
add test
1 parent da4db42 commit 0647245

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

instrumentation/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
213221
class 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

Comments
 (0)