Skip to content

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.

What you’ll capture

  • Each request as a span, with its HTTP status and duration
  • The request method and path
  • Any errors raised while handling the request

Before you start

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.

Installation

Install logfire with the aiohttp-server extra:

Terminal
pip install 'logfire[aiohttp-server]'

Usage

Add two lines to your app: logfire.configure() to connect to your project, and logfire.instrument_aiohttp_server() to record every request.

server.py
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.

Verify it worked

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.

Troubleshooting

Not seeing your requests in Logfire? Check these first:

  • logfire.configure() runs before logfire.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 the LOGFIRE_TOKEN environment variable. See Getting Started.
  • You actually sent a request. Spans appear only after the server is hit; reload one of the URLs above.

Advanced

Passing options to the OpenTelemetry instrumentor

The keyword arguments of logfire.instrument_aiohttp_server() are passed to the AioHttpServerInstrumentor().instrument() method of the OpenTelemetry AIOHTTP server instrumentation package.

Reference

Excluding URLs from instrumentation

Capturing request and response headers