From 2bc4b266fc372f8d7bac14f7d7b5f42bf7a363fb Mon Sep 17 00:00:00 2001 From: Bulygin Evgeny Date: Wed, 29 Jan 2025 15:46:40 +0500 Subject: [PATCH] Added aiohttp-server instrumentation injection to module `web_app` because Application declared in `web_app` module, web only reimports this. If we leave the injection only in module `web_app`, then the tests will fail, we need inject both. --- CHANGELOG.md | 1 + .../aiohttp_server/__init__.py | 6 ++++-- .../tests/test_aiohttp_server_integration.py | 20 ++++++++++++++----- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce7f9acf03..3f3a3c9833 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- Changed `aiohttp-server` instrumentation injecting module from `web` to `web_app` - `opentelemetry-instrumentation-confluent-kafka` Add support for confluent-kafka <=2.7.0 ([#3100](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3100)) - Add support to database stability opt-in in `_semconv` utilities and add tests diff --git a/instrumentation/opentelemetry-instrumentation-aiohttp-server/src/opentelemetry/instrumentation/aiohttp_server/__init__.py b/instrumentation/opentelemetry-instrumentation-aiohttp-server/src/opentelemetry/instrumentation/aiohttp_server/__init__.py index 6db7719c76..5e19387cf8 100644 --- a/instrumentation/opentelemetry-instrumentation-aiohttp-server/src/opentelemetry/instrumentation/aiohttp_server/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-aiohttp-server/src/opentelemetry/instrumentation/aiohttp_server/__init__.py @@ -16,7 +16,7 @@ from timeit import default_timer from typing import Dict, List, Tuple, Union -from aiohttp import web +from aiohttp import web, web_app from multidict import CIMultiDictProxy from opentelemetry import metrics, trace @@ -257,10 +257,12 @@ class AioHttpServerInstrumentor(BaseInstrumentor): """ def _instrument(self, **kwargs): - self._original_app = web.Application + self._original_app = web_app.Application + setattr(web_app, "Application", _InstrumentedApplication) setattr(web, "Application", _InstrumentedApplication) def _uninstrument(self, **kwargs): + setattr(web_app, "Application", self._original_app) setattr(web, "Application", self._original_app) def instrumentation_dependencies(self): diff --git a/instrumentation/opentelemetry-instrumentation-aiohttp-server/tests/test_aiohttp_server_integration.py b/instrumentation/opentelemetry-instrumentation-aiohttp-server/tests/test_aiohttp_server_integration.py index 57eb6234a5..bca0499096 100644 --- a/instrumentation/opentelemetry-instrumentation-aiohttp-server/tests/test_aiohttp_server_integration.py +++ b/instrumentation/opentelemetry-instrumentation-aiohttp-server/tests/test_aiohttp_server_integration.py @@ -22,6 +22,7 @@ from opentelemetry import trace as trace_api from opentelemetry.instrumentation.aiohttp_server import ( AioHttpServerInstrumentor, + _InstrumentedApplication, ) from opentelemetry.instrumentation.utils import suppress_http_instrumentation from opentelemetry.semconv.trace import SpanAttributes @@ -70,13 +71,24 @@ def fixture_suppress(): return False +@pytest.mark.asyncio +async def test_aiohttp_app_instrumented(): + AioHttpServerInstrumentor().instrument() + from aiohttp import web, web_app + from aiohttp.web import Application as WebApplication + from aiohttp.web_app import Application as WebAppApplication + + assert web.Application is _InstrumentedApplication + assert WebApplication is _InstrumentedApplication + assert web_app.Application is _InstrumentedApplication + assert WebAppApplication is _InstrumentedApplication + + @pytest_asyncio.fixture(name="server_fixture") async def fixture_server_fixture(tracer, aiohttp_server, suppress): _, memory_exporter = tracer - AioHttpServerInstrumentor().instrument() - - app = aiohttp.web.Application() + app = _InstrumentedApplication() app.add_routes([aiohttp.web.get("/test-path", default_handler)]) if suppress: with suppress_http_instrumentation(): @@ -88,8 +100,6 @@ async def fixture_server_fixture(tracer, aiohttp_server, suppress): memory_exporter.clear() - AioHttpServerInstrumentor().uninstrument() - def test_checking_instrumentor_pkg_installed(): (instrumentor_entrypoint,) = entry_points(