AIOHTTP Server¶
AIOHTTP is an asynchronous HTTP client/server framework for asyncio and Python.
The logfire.instrument_aiohttp_server()
method will create a span for every request made to your AIOHTTP server.
For AIOHTTP client instrumentation, see here.
Installation¶
Install logfire
with the aiohttp-server
extra:
pip install 'logfire[aiohttp-server]'
uv add 'logfire[aiohttp-server]'
poetry add 'logfire[aiohttp-server]'
Usage¶
Here's a minimal server example:
server.py
import logfire
from aiohttp import web
logfire.configure()
logfire.instrument_aiohttp_server()
async def hello(request):
return web.Response(text="Hello, World!")
async def user_handler(request):
user_id = request.match_info['user_id']
return web.json_response({"user_id": user_id, "message": "User profile"})
app = web.Application()
app.router.add_get('/', hello)
app.router.add_get('/users/{user_id}', user_handler)
if __name__ == "__main__":
web.run_app(app, host='localhost', port=8080)
You can run this server with python server.py
and then make requests to http://localhost:8080/
or http://localhost:8080/users/123
to see the spans created for each request.
The keyword arguments of logfire.instrument_aiohttp_server()
are passed to the AioHttpServerInstrumentor().instrument()
method of the OpenTelemetry AIOHTTP server instrumentation package.