Skip to content

Commit baa0249

Browse files
committed
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.
1 parent 7af1918 commit baa0249

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
### Added
1515

16+
- Added `opentelemetry-instrumentation-aiohttp-server` injection to `aiohttp` module `web_app`.
1617
- `opentelemetry-instrumentation-confluent-kafka` Add support for confluent-kafka <=2.7.0
1718
([#3100](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3100))
1819
- Add support to database stability opt-in in `_semconv` utilities and add tests

instrumentation/opentelemetry-instrumentation-aiohttp-server/src/opentelemetry/instrumentation/aiohttp_server/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from timeit import default_timer
1717
from typing import Dict, List, Tuple, Union
1818

19-
from aiohttp import web
19+
from aiohttp import web, web_app
2020
from multidict import CIMultiDictProxy
2121

2222
from opentelemetry import metrics, trace
@@ -257,10 +257,12 @@ class AioHttpServerInstrumentor(BaseInstrumentor):
257257
"""
258258

259259
def _instrument(self, **kwargs):
260-
self._original_app = web.Application
260+
self._original_app = web_app.Application
261+
setattr(web_app, "Application", _InstrumentedApplication)
261262
setattr(web, "Application", _InstrumentedApplication)
262263

263264
def _uninstrument(self, **kwargs):
265+
setattr(web_app, "Application", self._original_app)
264266
setattr(web, "Application", self._original_app)
265267

266268
def instrumentation_dependencies(self):

instrumentation/opentelemetry-instrumentation-aiohttp-server/tests/test_aiohttp_server_integration.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from opentelemetry import trace as trace_api
2323
from opentelemetry.instrumentation.aiohttp_server import (
2424
AioHttpServerInstrumentor,
25+
_InstrumentedApplication,
2526
)
2627
from opentelemetry.instrumentation.utils import suppress_http_instrumentation
2728
from opentelemetry.semconv.trace import SpanAttributes
@@ -70,13 +71,20 @@ def fixture_suppress():
7071
return False
7172

7273

74+
@pytest.mark.asyncio
75+
async def test_aiohttp_app_instrumented():
76+
AioHttpServerInstrumentor().instrument()
77+
from aiohttp import web, web_app # pylint: disable=C0415
78+
79+
assert web.Application is _InstrumentedApplication
80+
assert web_app.Application is _InstrumentedApplication
81+
82+
7383
@pytest_asyncio.fixture(name="server_fixture")
7484
async def fixture_server_fixture(tracer, aiohttp_server, suppress):
7585
_, memory_exporter = tracer
7686

77-
AioHttpServerInstrumentor().instrument()
78-
79-
app = aiohttp.web.Application()
87+
app = _InstrumentedApplication()
8088
app.add_routes([aiohttp.web.get("/test-path", default_handler)])
8189
if suppress:
8290
with suppress_http_instrumentation():
@@ -88,8 +96,6 @@ async def fixture_server_fixture(tracer, aiohttp_server, suppress):
8896

8997
memory_exporter.clear()
9098

91-
AioHttpServerInstrumentor().uninstrument()
92-
9399

94100
def test_checking_instrumentor_pkg_installed():
95101
(instrumentor_entrypoint,) = entry_points(

0 commit comments

Comments
 (0)