OpenTelemetry Aiohttp Server Instrumentation
This library provides instrumentation for aiohttp servers, enabling automatic tracing of incoming HTTP requests and responses using OpenTelemetry. It's part of the `opentelemetry-python-contrib` project, currently at version `0.62b0`, and generally follows the release cadence of the broader OpenTelemetry Python Contrib repository, with frequent beta updates.
Warnings
- gotcha This instrumentation package is currently in a beta (`b0`) state, indicated by its version `0.62b0`. While generally stable, its API or behavior may undergo breaking changes in minor releases without strict adherence to semantic versioning.
- gotcha The `AiohttpServerInstrumentor` requires explicit instantiation and calling of its `instrument()` method. It is not automatically enabled by `opentelemetry-bootstrap` or similar mechanisms, requiring you to add this code to your application's startup.
- gotcha OpenTelemetry requires a configured `TracerProvider` to be set globally before any instrumentation can effectively create and export spans. Forgetting this setup will result in no traces being generated, even if instrumentation is enabled.
- gotcha In complex `aiohttp` applications, especially those with custom middlewares, the order of middleware registration can affect how traces are generated. If other middlewares modify the request too early, some attributes or context might be missed by the OpenTelemetry instrumentation.
Install
-
pip install opentelemetry-instrumentation-aiohttp-server opentelemetry-sdk aiohttp
Imports
- AiohttpServerInstrumentor
from opentelemetry.instrumentation.aiohttp_server import AiohttpServerInstrumentor
Quickstart
import sys
import asyncio
from opentelemetry import trace
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor
from opentelemetry.instrumentation.aiohttp_server import AiohttpServerInstrumentor
from aiohttp import web
# 1. Configure OpenTelemetry Tracer Provider
resource = Resource.create({"service.name": "aiohttp-server-app"})
provider = TracerProvider(resource=resource)
processor = SimpleSpanProcessor(ConsoleSpanExporter(sys.stdout))
provider.add_span_processor(processor)
trace.set_tracer_provider(provider)
# 2. Instrument the aiohttp server
AiohttpServerInstrumentor().instrument()
# 3. Define a simple aiohttp application
async def hello_handler(request):
tracer = trace.get_tracer(__name__)
with tracer.start_as_current_span("hello-endpoint-logic"):
await asyncio.sleep(0.05) # Simulate some async work
return web.Response(text="Hello, OpenTelemetry aiohttp!")
async def health_check_handler(request):
return web.Response(text="OK")
app = web.Application()
app.router.add_get("/", hello_handler)
app.router.add_get("/health", health_check_handler)
# 4. Run the aiohttp server
if __name__ == '__main__':
print("Server starting on http://localhost:8080")
print("Try: curl http://localhost:8080")
web.run_app(app, host="localhost", port=8080)