Skip to content

Commit

Permalink
Added aiohttp-server instrumentation injection to module web_app be…
Browse files Browse the repository at this point in the history
…cause 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.
  • Loading branch information
nesb1 committed Jan 29, 2025
1 parent 7af1918 commit baa0249
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Added `opentelemetry-instrumentation-aiohttp-server` injection to `aiohttp` module `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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -70,13 +71,20 @@ def fixture_suppress():
return False


@pytest.mark.asyncio
async def test_aiohttp_app_instrumented():
AioHttpServerInstrumentor().instrument()
from aiohttp import web, web_app # pylint: disable=C0415

assert web.Application is _InstrumentedApplication
assert web_app.Application 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():
Expand All @@ -88,8 +96,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(
Expand Down

0 comments on commit baa0249

Please sign in to comment.