Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
adriangb committed Dec 3, 2024
1 parent 5fc3a80 commit 7497d32
Showing 1 changed file with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,20 @@ async def _(param: str):
@app.get("/healthzz")
async def _():
return {"message": "ok"}

@app.get("/error")
async def _():
raise UnhandledException("This is an unhandled exception")

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

return app


class UnhandledException(Exception):
pass


class TestBaseManualFastAPI(TestBaseFastAPI):
@classmethod
def setUpClass(cls):
Expand Down Expand Up @@ -404,6 +412,26 @@ def test_fastapi_excluded_urls(self):
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 0)

def test_fastapi_unhandled_exception(self):
"""If the application has an unhandled error the instrumentation should capture that a 500 response is returned."""
try:
self._client.get("/error")
except UnhandledException:
pass
else:
self.fail("Expected UnhandledException")

spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 3)
for span in spans:
self.assertIn("GET /error", span.name)
self.assertEqual(
span.attributes[SpanAttributes.HTTP_ROUTE], "/error"
)
self.assertEqual(
span.attributes[SpanAttributes.HTTP_STATUS_CODE], 500
)

def test_fastapi_excluded_urls_not_env(self):
"""Ensure that given fastapi routes are excluded when passed explicitly (not in the environment)"""
app = self._create_app_explicit_excluded_urls()
Expand Down

0 comments on commit 7497d32

Please sign in to comment.