Skip to content

Commit 5946f92

Browse files
committed
add test for unhappy instrumentation codepath
1 parent b9a2530 commit 5946f92

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414

1515
# pylint: disable=too-many-lines
1616

17+
import logging
1718
import unittest
1819
from contextlib import ExitStack
1920
from timeit import default_timer
2021
from unittest.mock import Mock, call, patch
2122

2223
import fastapi
24+
import pytest
2325
from fastapi.middleware.httpsredirect import HTTPSRedirectMiddleware
2426
from fastapi.responses import JSONResponse, PlainTextResponse
2527
from fastapi.testclient import TestClient
@@ -2014,3 +2016,38 @@ async def _(*_):
20142016
event.attributes.get(EXCEPTION_TYPE),
20152017
f"{__name__}.UnhandledException",
20162018
)
2019+
2020+
2021+
class TestFastAPIFallback(TestBaseFastAPI):
2022+
@pytest.fixture(autouse=True)
2023+
def inject_fixtures(self, caplog):
2024+
self._caplog = caplog
2025+
2026+
@staticmethod
2027+
def _create_fastapi_app():
2028+
app = TestBaseFastAPI._create_fastapi_app()
2029+
2030+
def build_middleware_stack():
2031+
return app.router
2032+
2033+
app.build_middleware_stack = build_middleware_stack
2034+
return app
2035+
2036+
def setUp(self):
2037+
super().setUp()
2038+
self.client = TestClient(self._app)
2039+
2040+
def test_no_instrumentation(self):
2041+
self.client.get(
2042+
"/foobar",
2043+
)
2044+
2045+
spans = self.memory_exporter.get_finished_spans()
2046+
self.assertEqual(len(spans), 0)
2047+
2048+
errors = [
2049+
record
2050+
for record in self._caplog.get_records("call")
2051+
if record.levelno >= logging.ERROR
2052+
]
2053+
self.assertEqual(len(errors), 1)

0 commit comments

Comments
 (0)