|
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, |
17 | | - middleware, |
| 15 | +from opentelemetry.instrumentation.aiohttp_server import ( |
| 16 | + middleware as aiohttp_server_opentelemetry_middleware, # pylint:disable=no-name-in-module |
18 | 17 | ) |
19 | 18 | from opentelemetry.instrumentation.aiopg import ( # pylint:disable=no-name-in-module |
20 | 19 | AiopgInstrumentor, |
@@ -74,17 +73,16 @@ def setup_tracing( |
74 | 73 | # Add the span processor to the tracer provider |
75 | 74 | tracer_provider.add_span_processor(BatchSpanProcessor(otlp_exporter)) # type: ignore[attr-defined] # https://github.com/open-telemetry/opentelemetry-python/issues/3713 |
76 | 75 | # Instrument aiohttp server |
77 | | - AioHttpServerInstrumentor().instrument() |
78 | | - # Explanation for extra call DK 10/2024: |
| 76 | + # Explanation for custom middleware call DK 10/2024: |
79 | 77 | # OpenTelemetry Aiohttp autoinstrumentation is meant to be used by only calling `AioHttpServerInstrumentor().instrument()` |
80 | | - # But, the call `AioHttpServerInstrumentor().instrument()` monkeypatches the __init__() of aiohttp's web.application() to in |
81 | | - # the init inject the tracing middleware. |
82 | | - # In simcore, we want to switch tracing on or off and thus depend on the simcore-settings library when we call `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. |
83 | 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 |
84 | 82 | # |
85 | 83 | # Since the code that is provided (monkeypatched) in the __init__ that the opentelemetry-autoinstrumentation-library provides is only 4 lines, |
86 | | - # literally 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: |
87 | | - app.middlewares.append(middleware) |
| 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) |
88 | 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 |
89 | 87 | # For reference, the above statement was written for: |
90 | 88 | # - osparc-simcore 1.77.x |
|
0 commit comments