From 2e51be9bd7b861e0f821d4337603aa66bfe6edfd Mon Sep 17 00:00:00 2001 From: "anshul.asawa" Date: Fri, 2 Sep 2022 14:21:05 +0530 Subject: [PATCH 1/2] fix check of is_otel_instrumented --- .../src/opentelemetry/instrumentation/fastapi/__init__.py | 1 + .../tests/test_fastapi_instrumentation.py | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/instrumentation/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py b/instrumentation/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py index 1c76c8bb50..4fd4947418 100644 --- a/instrumentation/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py @@ -247,6 +247,7 @@ def __init__(self, *args, **kwargs): client_response_hook=_InstrumentedFastAPI._client_response_hook, tracer_provider=_InstrumentedFastAPI._tracer_provider, ) + self._is_instrumented_by_opentelemetry = True def _get_route_details(scope): diff --git a/instrumentation/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py b/instrumentation/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py index 0d42e7533c..4cd3cbee29 100644 --- a/instrumentation/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py +++ b/instrumentation/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py @@ -274,6 +274,14 @@ def test_request(self): self.assertEqual(span.resource.attributes["key1"], "value1") self.assertEqual(span.resource.attributes["key2"], "value2") + def test_mulitple_way_instrumentation(self): + self._instrumentor.instrument_app(self._app) + count = 0 + for middleware in self._app.user_middleware: + if middleware.cls is OpenTelemetryMiddleware: + count += 1 + self.assertEqual(count, 1) + def tearDown(self): self._instrumentor.uninstrument() super().tearDown() From 408e1b325412f609dc9f3dc29ed120b42cdc4979 Mon Sep 17 00:00:00 2001 From: "anshul.asawa" Date: Fri, 2 Sep 2022 14:29:17 +0530 Subject: [PATCH 2/2] add changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a2b306157..d2e1baa0d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#1208](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1208)) - `opentelemetry-instrumentation-aiohttp-client` Fix producing additional spans with each newly created ClientSession - ([#1246](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1246)) +- Add _is_openetlemetry_instrumented check in _InstrumentedFastAPI class + ([#1313](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1313)) ## [1.12.0-0.33b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.12.0-0.33b0) - 2022-08-08