Skip to content

Commit e5216d4

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 e5216d4

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-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: 15 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,24 @@ 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=import-outside-toplevel
78+
from aiohttp.web import Application as WebApplication # pylint: disable=import-outside-toplevel
79+
from aiohttp.web_app import Application as WebAppApplication # pylint: disable=import-outside-toplevel
80+
81+
assert web.Application is _InstrumentedApplication
82+
assert WebApplication is _InstrumentedApplication
83+
assert web_app.Application is _InstrumentedApplication
84+
assert WebAppApplication is _InstrumentedApplication
85+
86+
7387
@pytest_asyncio.fixture(name="server_fixture")
7488
async def fixture_server_fixture(tracer, aiohttp_server, suppress):
7589
_, memory_exporter = tracer
7690

77-
AioHttpServerInstrumentor().instrument()
78-
79-
app = aiohttp.web.Application()
91+
app = _InstrumentedApplication()
8092
app.add_routes([aiohttp.web.get("/test-path", default_handler)])
8193
if suppress:
8294
with suppress_http_instrumentation():
@@ -88,8 +100,6 @@ async def fixture_server_fixture(tracer, aiohttp_server, suppress):
88100

89101
memory_exporter.clear()
90102

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

94104
def test_checking_instrumentor_pkg_installed():
95105
(instrumentor_entrypoint,) = entry_points(

0 commit comments

Comments
 (0)