Skip to content

ASGI

If the ASGI web framework you're using doesn't have a dedicated integration, you can use the logfire.instrument_asgi() method to instrument it.

Installation

Install logfire with the asgi extra:

pip install 'logfire[asgi]'
uv add 'logfire[asgi]'
rye add logfire -E asgi
poetry add 'logfire[asgi]'

Usage

Below we have a minimal example using Uvicorn. You can run it with python main.py:

main.py
import logfire


logfire.configure()


async def app(scope, receive, send):
    assert scope["type"] == "http"
    await send(
        {
            "type": "http.response.start",
            "status": 200,
            "headers": [(b"content-type", b"text/plain"), (b"content-length", b"13")],
        }
    )
    await send({"type": "http.response.body", "body": b"Hello, world!"})

app = logfire.instrument_asgi(app)

if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app)

The keyword arguments of logfire.instrument_asgi() are passed to the OpenTelemetryMiddleware class of the OpenTelemetry ASGI Instrumentation package.

Excluding URLs from instrumentation

Note

instrument_asgi does accept an excluded_urls parameter, but does not support specifying said URLs via an environment variable, unlike other instrumentations.

Capturing request and response headers