|
14 | 14 |
|
15 | 15 | # pylint: disable=too-many-lines |
16 | 16 |
|
| 17 | +import logging |
17 | 18 | import unittest |
18 | 19 | from contextlib import ExitStack |
19 | 20 | from timeit import default_timer |
20 | 21 | from unittest.mock import Mock, call, patch |
21 | 22 |
|
22 | 23 | import fastapi |
| 24 | +import pytest |
23 | 25 | from fastapi.middleware.httpsredirect import HTTPSRedirectMiddleware |
24 | 26 | from fastapi.responses import JSONResponse, PlainTextResponse |
25 | 27 | from fastapi.testclient import TestClient |
@@ -2014,3 +2016,38 @@ async def _(*_): |
2014 | 2016 | event.attributes.get(EXCEPTION_TYPE), |
2015 | 2017 | f"{__name__}.UnhandledException", |
2016 | 2018 | ) |
| 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