|
| 1 | +# Copyright The OpenTelemetry Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import unittest |
| 16 | + |
| 17 | +import fastapi |
| 18 | +from fastapi.testclient import TestClient |
| 19 | + |
| 20 | +import opentelemetry.instrumentation.fastapi as otel_fastapi |
| 21 | +from opentelemetry.test.test_base import TestBase |
| 22 | + |
| 23 | + |
| 24 | +class TestFastAPIManualInstrumentation(TestBase): |
| 25 | + def _create_app(self): |
| 26 | + app = self._create_fastapi_app() |
| 27 | + self._instrumentor.instrument_app(app) |
| 28 | + return app |
| 29 | + |
| 30 | + def setUp(self): |
| 31 | + super().setUp() |
| 32 | + self._instrumentor = otel_fastapi.FastAPIInstrumentor() |
| 33 | + self._app = self._create_app() |
| 34 | + self._client = TestClient(self._app) |
| 35 | + |
| 36 | + def test_basic_fastapi_call(self): |
| 37 | + self._client.get("/foobar") |
| 38 | + spans = self.memory_exporter.get_finished_spans() |
| 39 | + self.assertEqual(len(spans), 3) |
| 40 | + for span in spans: |
| 41 | + self.assertIn("/foobar", span.name) |
| 42 | + |
| 43 | + def test_fastapi_route_attribute_added(self): |
| 44 | + """Ensure that fastapi routes are used as the span name.""" |
| 45 | + self._client.get("/user/123") |
| 46 | + spans = self.memory_exporter.get_finished_spans() |
| 47 | + self.assertEqual(len(spans), 3) |
| 48 | + for span in spans: |
| 49 | + self.assertIn("/user/{username}", span.name) |
| 50 | + self.assertEqual( |
| 51 | + spans[-1].attributes["http.route"], "/user/{username}" |
| 52 | + ) |
| 53 | + # ensure that at least one attribute that is populated by |
| 54 | + # the asgi instrumentation is successfully feeding though. |
| 55 | + self.assertEqual(spans[-1].attributes["http.flavor"], "1.1") |
| 56 | + |
| 57 | + @staticmethod |
| 58 | + def _create_fastapi_app(): |
| 59 | + app = fastapi.FastAPI() |
| 60 | + |
| 61 | + @app.get("/foobar") |
| 62 | + async def _(): |
| 63 | + return {"message": "hello world"} |
| 64 | + |
| 65 | + @app.get("/user/{username}") |
| 66 | + async def _(username: str): |
| 67 | + return {"message": username} |
| 68 | + |
| 69 | + return app |
| 70 | + |
| 71 | + |
| 72 | +class TestAutoInstrumentation(TestFastAPIManualInstrumentation): |
| 73 | + """Test the auto-instrumented variant |
| 74 | +
|
| 75 | + Extending the manual instrumentation as most test cases apply |
| 76 | + to both. |
| 77 | + """ |
| 78 | + |
| 79 | + def _create_app(self): |
| 80 | + # instrumentation is handled by the instrument call |
| 81 | + self._instrumentor.instrument() |
| 82 | + return self._create_fastapi_app() |
| 83 | + |
| 84 | + def tearDown(self): |
| 85 | + self._instrumentor.uninstrument() |
| 86 | + super().tearDown() |
| 87 | + |
| 88 | + |
| 89 | +class TestAutoInstrumentationLogic(unittest.TestCase): |
| 90 | + def test_instrumentation(self): |
| 91 | + """Verify that instrumentation methods are instrumenting and |
| 92 | + removing as expected. |
| 93 | + """ |
| 94 | + instrumentor = otel_fastapi.FastAPIInstrumentor() |
| 95 | + original = fastapi.FastAPI |
| 96 | + instrumentor.instrument() |
| 97 | + try: |
| 98 | + instrumented = fastapi.FastAPI |
| 99 | + self.assertIsNot(original, instrumented) |
| 100 | + finally: |
| 101 | + instrumentor.uninstrument() |
| 102 | + |
| 103 | + should_be_original = fastapi.FastAPI |
| 104 | + self.assertIs(original, should_be_original) |
0 commit comments