opentelemetry-python-contrib icon indicating copy to clipboard operation
opentelemetry-python-contrib copied to clipboard

aiohttp-server instrumentation doesn't work with several types of importing

Open nesb1 opened this issue 9 months ago • 1 comments
trafficstars

Describe your environment

Problem not related to the environment

What happened?

Instrumentation doesn't work with several types of importing. Problem with injecting technique through setattr. Looks like it not the best solution because not works with some imports variants.

Steps to Reproduce

from aiohttp import web
from aiohttp.web import Application
from aiohttp.web_app import Application as ApplicationFromWebApp
from aiohttp import web_app

print(web.Application)
print(Application)
print(web_app.Application)
print(ApplicationFromWebApp)


from opentelemetry.instrumentation.aiohttp_server import AioHttpServerInstrumentor

AioHttpServerInstrumentor().instrument()

print("instrumentation enable")

print(web.Application)
print(Application)
print(web_app.Application)
print(ApplicationFromWebApp)

Expected Result

<class 'aiohttp.web_app.Application'>
<class 'aiohttp.web_app.Application'>
<class 'aiohttp.web_app.Application'>
<class 'aiohttp.web_app.Application'>
instrumentation enable
<class 'opentelemetry.instrumentation.aiohttp_server._InstrumentedApplication'>
<class 'opentelemetry.instrumentation.aiohttp_server._InstrumentedApplication'>
<class 'opentelemetry.instrumentation.aiohttp_server._InstrumentedApplication'>
<class 'opentelemetry.instrumentation.aiohttp_server._InstrumentedApplication'>

Actual Result

<class 'aiohttp.web_app.Application'>
<class 'aiohttp.web_app.Application'>
<class 'aiohttp.web_app.Application'>
<class 'aiohttp.web_app.Application'>
instrumentation enable
<class 'opentelemetry.instrumentation.aiohttp_server._InstrumentedApplication'>
<class 'aiohttp.web_app.Application'>
<class 'aiohttp.web_app.Application'>
<class 'aiohttp.web_app.Application'>

Additional context

No response

Would you like to implement a fix?

yes

nesb1 avatar Jan 30 '25 07:01 nesb1

The import statements above create independent local references to aiohttp at the time of import. As a result, when instrumentation is enabled via monkey-patching (e.g., using setattr), local references such as Application and ApplicationFromWebApp remain bound to the original class and do not properly reflect the instrumentation changes.

Russolves avatar Feb 07 '25 02:02 Russolves