Skip to content

Starlette

The logfire.instrument_starlette() method will create a span for every request to your Starlette application.

Installation

Install logfire with the starlette extra:

Terminal
pip install 'logfire[starlette]'

Usage

We have a minimal example below. Please install Uvicorn to run it:

Terminal
pip install uvicorn

You can run it with python main.py:

main.py
from starlette.applications import Starlette
from starlette.requests import Request
from starlette.responses import PlainTextResponse
from starlette.routing import Route

import logfire

logfire.configure()


async def home(request: Request) -> PlainTextResponse:
    return PlainTextResponse('Hello, world!')


app = Starlette(routes=[Route('/', home)])
logfire.instrument_starlette(app)

if __name__ == '__main__':
    import uvicorn

    uvicorn.run(app)

The keyword arguments of logfire.instrument_starlette() are passed to the StarletteInstrumentor.instrument_app() method of the OpenTelemetry Starlette Instrumentation package, read more about it here.

Proxying Browser Telemetry

If your frontend application sends telemetry from the browser, you should never expose your Logfire Write Token in the frontend code.

You can use experimental proxy handler to securely forward OTLP telemetry through your Starlette backend. See the Browser Telemetry Proxying guide for detailed instructions.

Excluding URLs from instrumentation

Capturing request and response headers