AIOHTTP
See every request your AIOHTTP server handles (the path, how long it took, the response status, and any errors) as a trace (the full journey of one request, made of nested spans, where each span is one unit of work with a name, a start, and a duration) in Logfire.
AIOHTTP is an asynchronous HTTP client and server framework for asyncio. This page covers the server side. For the client side, see AIOHTTP client.
- Each request as a span, with its HTTP status and duration
- The request method and path
- Any errors raised while handling the request
You’ll need a Logfire project. Open Add data in your project (top navigation) and follow the
setup for your language: it signs your machine in with logfire auth (a browser sign-in, no token
to copy) and, for production or other languages, creates a write token (the credential your app
uses to send data). New to Logfire? Start with Getting Started.
Install logfire with the aiohttp-server extra:
pip install 'logfire[aiohttp-server]'
uv add 'logfire[aiohttp-server]'
Add two lines to your app: logfire.configure() to connect to your project, and
logfire.instrument_aiohttp_server() to record every
request.
from aiohttp import web
import logfire
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)
Run it with python server.py.
With the server running, open http://localhost:8080/ or http://localhost:8080/users/123 in your browser.
Then open your project in the Logfire web app and go to the Live view. Within a few seconds you should see a span for the request. Click it to see its duration, the method and path, and the response status.
Not seeing your requests in Logfire? Check these first:
logfire.configure()runs beforelogfire.instrument_aiohttp_server(). Configure the connection first, then instrument.- You call
instrument_aiohttp_server()exactly once, before you start serving. - Your write token is set. In local development, run
logfire projects use <your-project>; in production, set theLOGFIRE_TOKENenvironment variable. See Getting Started. - You actually sent a request. Spans appear only after the server is hit; reload one of the URLs above.
The keyword arguments of
logfire.instrument_aiohttp_server() are passed to the
AioHttpServerInstrumentor().instrument() method of the
OpenTelemetry AIOHTTP server instrumentation package.
- API reference:
logfire.instrument_aiohttp_server() - Underlying OpenTelemetry package: AIOHTTP server instrumentation