Skip to main content
Logfire for FastAPI

Trace, monitor and debug your FastAPI app

FastAPI Observability from the team behind Pydantic. One line instruments every route, and each request shows its dependencies, database queries, and Pydantic validation in one connected trace. Query everything in plain SQL. Standard OpenTelemetry, no lock-in.

Install and configure
pip install 'logfire[fastapi]'
logfire auth   # or set LOGFIRE_TOKEN in production
Instrument your app
import logfire
from fastapi import FastAPI

logfire.configure()

app = FastAPI()
logfire.instrument_fastapi(app)

Full details are in the FastAPI setup guide.

What you get

One request, one trace

A Logfire trace of three FastAPI requests: a successful order lookup with its SQLAlchemy queries, outbound HTTP call and Pydantic response validation, then a 404 and a 500 shown in red

Every request becomes a trace, the full timeline of one request, built from nested spans, each a timed unit of work, from the route down to each query, validation, and outbound call, with timing and errors attached.

Try it on your stack

See your own FastAPI trace

Start free with 10 million spans, logs, and metrics each month. No credit card required.

How it works

See the whole request, not just the route

Trace the whole request, not just the route

The route span is the start. Add the libraries the endpoint uses, one line each: logfire.instrument_sqlalchemy(engine=engine), logfire.instrument_asyncpg(), logfire.instrument_httpx(), logfire.instrument_redis(). A request to POST /orders then shows the incoming call, the Pydantic validation, each database query with its timing, and the outbound call to your payments provider. When something is slow, you can see which part.

See your Pydantic validation

FastAPI request bodies are Pydantic models, and instrument_fastapi already captures the validated arguments and validation errors: a malformed request shows the model, the input, and the exact field that failed, turning a vague wave of 422s into a specific, fixable cause. For models you validate outside the endpoint signature, add logfire.instrument_pydantic() to trace those too. Captured arguments can include request data, so note that Logfire scrubs common sensitive fields by default and the scrubbing rules are configurable.

Trace your own endpoint logic

For the business logic inside a handler, open a span where the real work begins with logfire.span("create_order", customer=order.customer_id). It becomes the parent of the auto-instrumented queries and HTTP calls it triggers, with customer_id captured as a queryable attribute.

One query surface for FastAPI telemetry

Query request spans, validation errors, database calls, metrics, and structured logs in one Logfire project. Filter by route or your own attributes, then save the same SQL as a dashboard or alert when the question recurs.

Open standard, no lock-in

Logfire is OpenTelemetry underneath. Your FastAPI instrumentation is portable: send the same data to another OTel backend or self-host, and nothing in your app changes. You instrument once against the open standard and keep your options open.

Query

Query your FastAPI telemetry with SQL

If you can ask it, SQL can find out

  • Query your traces, metrics and logs with real SQL
  • No proprietary query language to learn
  • AI models are great at writing SQL
  • From a one-off investigation to an alert, dashboard, or SLO
select
  attributes->>'http.route' as route,
  count(*) as requests,
  avg(duration) as avg_seconds
from records
where http_response_status_code >= 500
group by route
order by requests desc;
Proof

In production

We are finding and fixing issues in five minutes instead of an hour, which is obviously increasing uptime for our clients.
Zach Silver, Staff Software Engineer, BoostedAI Read the case study
Decision guide

Is Logfire right for you?

Choose Logfire if

  • You want to instrument FastAPI in one line and see every request as a trace
  • You want request validation errors visible in production, down to the field
  • You want database queries and outbound calls timed inside each request
  • You want to query traces, metrics, and logs with PostgreSQL-compatible SQL
  • You want the portability of OpenTelemetry with Python-first ergonomics

Choose a traditional APM if

  • You are already deeply integrated with a specific APM vendor's ecosystem
  • You need a vendor-only integration or compliance capability Logfire does not offer
  • You want one vendor's agent and dashboards, and are comfortable with their pricing model
FAQ

Common questions

How do I add OpenTelemetry to a FastAPI application?

Install Pydantic Logfire, call logfire.configure(), then logfire.instrument_fastapi(app). That one line captures every route, request, response, and exception. Add instrument_sqlalchemy(engine=engine), instrument_httpx(), and similar to trace the database and outbound calls each request makes. Logfire is OpenTelemetry-native, so standard OTel tooling works too.

Can I see FastAPI request validation errors in production?

Yes. FastAPI validates requests with Pydantic, and because Logfire is built by the Pydantic team, calling logfire.instrument_pydantic() surfaces validation as spans. A failed request shows the model, the input, and the exact field that did not match, so a wave of 422s becomes a specific, fixable cause.

How do I exclude health checks from FastAPI tracing?

Set the standard OpenTelemetry variable OTEL_PYTHON_FASTAPI_EXCLUDED_URLS to comma-separated regexes, for example '/health$,/metrics$'.

Does instrumenting FastAPI add much overhead?

Auto-instrumentation is built for production. Telemetry is batched and exported asynchronously, and traces can be sampled to control volume, so the runtime cost is small relative to the visibility gained into slow endpoints and failing calls.

See inside your FastAPI app

Get started with 10 million free spans, logs, and metrics per month. No credit card required.