Skip to content

StarLette

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

install

Install logfire using starlette extra:

{{ install_logfire(extras=['starlette']) }}

usage

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

pip install uvicorn

you can use python main.py Run it:

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

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)

Keyword arguments for logfire.instrument_starlette() are passed to the StarletteInstrumentor.instrument_app() method of the OpenTelemetry Starlette Instrumentation package,Read more about here.

Issue

"What about OpenTelemetry ASGI middleware? If you're a more experienced user, you might wonder why we don't use OpenTelemetry ASGI middleware. The reason is that StarletteInstrumentor actually wraps ASGI middleware and adds some additional information related to routing.

Capture request and response headers


本文总阅读量次