Skip to content

Commit f825c76

Browse files
committed
add test
1 parent ec545b7 commit f825c76

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
@@ -210,12 +210,20 @@ async def _(param: str):
210210
@app.get("/healthzz")
211211
async def _():
212212
return {"message": "ok"}
213+
214+
@app.get("/error")
215+
async def _():
216+
raise UnhandledException("This is an unhandled exception")
213217

214218
app.mount("/sub", app=sub_app)
215219

216220
return app
217221

218222

223+
class UnhandledException(Exception):
224+
pass
225+
226+
219227
class TestBaseManualFastAPI(TestBaseFastAPI):
220228
@classmethod
221229
def setUpClass(cls):
@@ -404,6 +412,26 @@ def test_fastapi_excluded_urls(self):
404412
spans = self.memory_exporter.get_finished_spans()
405413
self.assertEqual(len(spans), 0)
406414

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+
407435
def test_fastapi_excluded_urls_not_env(self):
408436
"""Ensure that given fastapi routes are excluded when passed explicitly (not in the environment)"""
409437
app = self._create_app_explicit_excluded_urls()

0 commit comments

Comments
 (0)