|
12 | 12 | from opentelemetry.instrumentation.aiohttp_client import ( # pylint:disable=no-name-in-module |
13 | 13 | AioHttpClientInstrumentor, |
14 | 14 | ) |
15 | | -from opentelemetry.instrumentation.aiohttp_server import ( # pylint:disable=no-name-in-module |
16 | | - AioHttpServerInstrumentor, |
| 15 | +from opentelemetry.instrumentation.aiohttp_server import ( |
| 16 | + middleware as aiohttp_server_opentelemetry_middleware, # pylint:disable=no-name-in-module |
17 | 17 | ) |
18 | 18 | from opentelemetry.instrumentation.aiopg import ( # pylint:disable=no-name-in-module |
19 | 19 | AiopgInstrumentor, |
@@ -72,8 +72,24 @@ def setup_tracing( |
72 | 72 |
|
73 | 73 | # Add the span processor to the tracer provider |
74 | 74 | tracer_provider.add_span_processor(BatchSpanProcessor(otlp_exporter)) # type: ignore[attr-defined] # https://github.com/open-telemetry/opentelemetry-python/issues/3713 |
75 | | - # Instrument aiohttp server and client |
76 | | - AioHttpServerInstrumentor().instrument() |
| 75 | + # Instrument aiohttp server |
| 76 | + # Explanation for custom middleware call DK 10/2024: |
| 77 | + # OpenTelemetry Aiohttp autoinstrumentation is meant to be used by only calling `AioHttpServerInstrumentor().instrument()` |
| 78 | + # The call `AioHttpServerInstrumentor().instrument()` monkeypatches the __init__() of aiohttp's web.application() to inject the tracing middleware, in it's `__init__()`. |
| 79 | + # In simcore, we want to switch tracing on or off using the simcore-settings-library. |
| 80 | + # The simcore-settings library in turn depends on the instance of web.application(), i.e. the aiohttp webserver, to exist. So here we face a hen-and-egg problem. |
| 81 | + # At the time when the instrumentation should be configured, the instance of web.application already exists and the overwrite to the __init__() is never called |
| 82 | + # |
| 83 | + # Since the code that is provided (monkeypatched) in the __init__ that the opentelemetry-autoinstrumentation-library provides is only 4 lines, |
| 84 | + # just adding a middleware, we are free to simply execute this "missed call" [since we can't call the monkeypatch'ed __init__()] in this following line: |
| 85 | + app.middlewares.insert(0, aiohttp_server_opentelemetry_middleware) |
| 86 | + # Code of the aiohttp server instrumentation: github.com/open-telemetry/opentelemetry-python-contrib/blob/eccb05c808a7d797ef5b6ecefed3590664426fbf/instrumentation/opentelemetry-instrumentation-aiohttp-server/src/opentelemetry/instrumentation/aiohttp_server/__init__.py#L246 |
| 87 | + # For reference, the above statement was written for: |
| 88 | + # - osparc-simcore 1.77.x |
| 89 | + # - opentelemetry-api==1.27.0 |
| 90 | + # - opentelemetry-instrumentation==0.48b0 |
| 91 | + |
| 92 | + # Instrument aiohttp client |
77 | 93 | AioHttpClientInstrumentor().instrument() |
78 | 94 | if instrument_aiopg: |
79 | 95 | AiopgInstrumentor().instrument() |
|
0 commit comments