Skip to main content
Logfire for FastAPI

FastAPI observability with OpenTelemetry

OpenTelemetry-native observability for FastAPI, from the team that makes Pydantic. One line instruments every route, and each request shows its dependencies, database queries, and Pydantic validation in one connected trace. Free for 10 million spans a month.

Start free As Markdown
Getting started

One line to instrument FastAPI

pip install 'logfire[fastapi]'
import logfire
from fastapi import FastAPI

logfire.configure()

app = FastAPI()
logfire.instrument_fastapi(app)

That single call captures every request: the route, method, status code, timing, the validated arguments, and any exception. Because Logfire wraps OpenTelemetry's ASGI instrumentation, it understands the async request lifecycle correctly.

What you get

One request, one trace

A Logfire trace of an HTTP request, broken into nested timed spans including its database queries, filterable with SQL

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.

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(), 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.

Traces, metrics, and logs in one place

Logfire is not just tracing. Send metrics and structured logs alongside your traces, build dashboards, and set alerts, all in the same OpenTelemetry-native project, and query all of it with the same SQL.

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 telemetry with SQL

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;

Your traces, metrics, and logs are queryable with real SQL, no proprietary query language to learn. Any question you can express becomes a dashboard, an alert, or a one-off investigation.

Comparison

How Logfire compares for FastAPI

Approaches to FastAPI observability compared
Approach Strengths Trade-offs
Datadog / New Relic Deep, mature APM Proprietary, pricing climbs at scale
Sentry Strong error tracking Lighter on tracing and performance detail
Prometheus + Grafana Great metrics You assemble tracing and logs separately
Raw OpenTelemetry + DIY backend Portable and open You run the backend yourself
Pydantic Logfire One-line FastAPI setup, Pydantic-aware, SQL, hosted Python and OTel-first, so a weaker fit for non-Python stacks; hosted by default, and younger than the incumbents
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 yet offer
  • You want a single proprietary agent and are comfortable with per-host pricing
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(), 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 a comma-separated list, for example health,metrics. Those routes are then skipped, keeping your traces focused on real traffic.

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 per month. No credit card required.