|
41 | 41 | import uvloop |
42 | 42 | from aiohttp import web |
43 | 43 | from aiohttp.typedefs import Handler, Middleware |
| 44 | +from opentelemetry.instrumentation.aiohttp_server import ( |
| 45 | + middleware as otel_server_middleware, |
| 46 | +) |
44 | 47 | from setproctitle import setproctitle |
45 | 48 | from zmq.auth.certs import load_certificate |
46 | 49 |
|
@@ -835,8 +838,6 @@ async def service_discovery_ctx(root_ctx: RootContext) -> AsyncIterator[None]: |
835 | 838 | service_instance_name=meta.display_name, |
836 | 839 | ) |
837 | 840 | BraceStyleAdapter.apply_otel(otel_spec) |
838 | | - instrument_aiohttp_server() |
839 | | - instrument_aiohttp_client() |
840 | 841 | try: |
841 | 842 | yield |
842 | 843 | finally: |
@@ -1787,6 +1788,15 @@ async def webapp_ctx(root_app: web.Application) -> AsyncGenerator[None]: |
1787 | 1788 | jwt_config = root_ctx.config_provider.config.jwt.to_jwt_config() |
1788 | 1789 | root_ctx.jwt_validator = JWTValidator(jwt_config) |
1789 | 1790 |
|
| 1791 | + # TODO: Remove manual middleware injection once the manager startup is |
| 1792 | + # decoupled from the aiohttp Application lifecycle. Currently root_app is |
| 1793 | + # instantiated before OTel config is available, so instrument_aiohttp_server() |
| 1794 | + # (which patches the class via setattr) cannot take effect automatically. |
| 1795 | + if root_ctx.config_provider.config.otel.enabled: |
| 1796 | + instrument_aiohttp_server() |
| 1797 | + instrument_aiohttp_client() |
| 1798 | + root_app.middlewares.insert(0, otel_server_middleware) |
| 1799 | + |
1790 | 1800 | # Plugin webapps should be loaded before runner.setup() because root_app is frozen upon on_startup event. |
1791 | 1801 | await manager_init_stack.enter_async_context(webapp_plugin_ctx(root_app)) |
1792 | 1802 | await manager_init_stack.enter_async_context(webapp_ctx(root_app)) |
|
0 commit comments