fastapi-observer
One-call observability for FastAPI. JSON logs with request IDs, Prometheus metrics and OpenTelemetry traces, with sensitive data redacted by default. No vendor backend: send it to whatever you already run.
from fastapi import FastAPI
from fastapiobserver import install_observability
app = FastAPI()
install_observability(app)
Every request now writes one JSON line, with the same trace_id as its trace when tracing is on:
{
"timestamp": "2026-10-31T09:10:20.345831+00:00",
"level": "INFO",
"logger": "fastapiobserver.middleware",
"message": "request.completed",
"app_name": "orders-api",
"service": "orders",
"environment": "production",
"version": "2.3.1",
"request_id": "d496f541-5e5e-4f14-9174-e9bd697faa62",
"trace_id": "11d5b6f088bfdba501927e55463be4cd",
"span_id": "50a12c2d9937eacb",
"event": {
"method": "GET",
"path": "/orders/42",
"status_code": 200,
"duration_ms": 1.314,
"client_ip": "203.0.113.7",
"error_type": "ok"
}
}
Documentation: vitaee.github.io/FastapiObserver
Install
pip install fastapi-observer # JSON logs, request IDs, redaction
pip install "fastapi-observer[prometheus]" # + Prometheus /metrics
pip install "fastapi-observer[otel]" # + OpenTelemetry traces and OTLP logs
pip install "fastapi-observer[all]" # everything
Requires Python 3.11 or newer.
Configure with environment variables
install_observability(app) reads its settings from the environment. These are the ones most apps set:
| Variable | Example | What it does |
|---|---|---|
APP_NAME, SERVICE_NAME, ENVIRONMENT, APP_VERSION |
orders-api, orders, production, 2.3.1 |
Added to every log line |
METRICS_ENABLED |
true |
Serves Prometheus metrics at /metrics (needs [prometheus]) |
OTEL_ENABLED, OTEL_EXPORTER_OTLP_ENDPOINT |
true, http://collector:4317 |
Sends traces over OTLP and adds trace_id to logs (needs [otel]) |
OBS_REDACTION_PRESET |
strict, pci, gdpr |
Chooses which fields and headers are masked |
OBS_PROFILE |
development, production |
Sensible defaults: production sets INFO logs, a larger log queue and the strict preset |
Explicit variables and Python arguments always win over profile defaults. Every setting is listed in the configuration guide.
What you get
- One JSON line per request with method, path, status, duration and client IP, and your service's name, environment and version on every line, including your own
loggingcalls. - Request IDs taken from
X-Request-IDwhen the caller is a trusted proxy, generated otherwise, and returned in the response. - Redaction on by default. Passwords, tokens, API keys,
Authorizationand cookies are masked before anything is written. Presets cover stricter needs (strict,pci,gdpr). - Prometheus metrics labelled by route template (
/orders/{order_id}), so URLs with IDs don't explode cardinality. - OpenTelemetry traces with
trace_idandspan_idin every log line, and optional log export over OTLP. - Non-blocking log writes. Lines are written from a background thread, so a slow stdout never stalls the event loop.
Optional extras: runtime log-level control, a tamper-evident audit log, SQLAlchemy, httpx and requests tracing, Strawberry GraphQL and a Loguru bridge. See the documentation.
The Grafana dashboard from examples/full_stack, a Docker Compose stack you can run locally.
How it compares
| fastapi-observer | structlog or loguru | OpenTelemetry auto-instrumentation | Logfire | |
|---|---|---|---|---|
| JSON log line per request | Built in | You write the middleware | No | A span per request |
| Request ID on every log line | Built in | You wire it up | No | Trace IDs |
| Redaction by default | Yes, with presets | You write a processor | No | Yes |
Prometheus /metrics |
Built in | No | Through an exporter | No |
| Traces | OpenTelemetry | No | Yes | Yes |
| Where data goes | stdout, Prometheus and any OTLP backend | stdout or files | Any OTLP backend | Logfire's platform |
When not to use it:
- You only want logs and need the lowest possible overhead: structlog alone does less work per line.
- You already send everything to Logfire, Datadog or Sentry: their own FastAPI integrations fit better.
- Your app isn't built on FastAPI or Starlette.
What it costs: with default settings, about 43 µs of CPU per request on an Apple M1. See the benchmarks, including a comparison with wiring the same stack by hand.
Works with
- Servers: uvicorn, gunicorn with
UvicornWorker, and Granian. Uvicorn's duplicate access line is dropped automatically. To get the servers' own startup lines as JSON too, see running with gunicorn and Granian. - Backends: Prometheus, Grafana (Loki, Tempo), Jaeger and anything that accepts OTLP. See backends.
- Error tracking: runs alongside Sentry. See the Sentry notes.
Why you can trust it
- Tested in CI on Python 3.11 to 3.15, fully typed, and used in production by its maintainer.
- Releases are published with PyPI Trusted Publishing and signed attestations. CI runs
pip-auditand generates an SBOM. - Security fixes are called out in the changelog. To report a vulnerability, see SECURITY.md.
- Benchmarks are reproducible from
examples/benchmarks.
Compatibility
| Component | Supported |
|---|---|
| Python | >=3.11 (CI: 3.11 to 3.15) |
| FastAPI | >=0.133.0 |
| Starlette | >=1.3.1 |
| Prometheus extra | prometheus-client>=0.24.1 |
| OpenTelemetry extra | opentelemetry-api/sdk/exporter>=1.45.1 |
Documentation
- Configuration and structured logging
- Security and redaction presets
- OpenTelemetry
- Production deployment and Kubernetes
- Benchmarks and performance tuning
- Contributing
Support the project
If this library saves you time, you can buy me a coffee.
Metadata
Release files for fastapi-observer 1.7.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| fastapi_observer-1.7.0.tar.gz | 95.8 kB | With provenance | Details for fastapi_observer-1.7.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| fastapi_observer-1.7.0-py3-none-any.whl | Python 3 | none | any | Details for fastapi_observer-1.7.0-py3-none-any.whl |
Total release size: 177.3 kB
Release files / fastapi_observer-1.7.0.tar.gz
| Download URL | fastapi_observer-1.7.0.tar.gz |
|---|---|
| Size | 95.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fa45d4a97b82e8dc2daca095c3847f84607a9684cf5a4bab0655cd55b3e9db7b
|
|
BLAKE2b-256 checksum How to use checksums |
bfa60dd25c68192deed09ce2e5dfc85d8ca4dcc2813865d3d3e7e7f4de606ed6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 31, 2026.
Transparency logRelease files / fastapi_observer-1.7.0-py3-none-any.whl
| Download URL | fastapi_observer-1.7.0-py3-none-any.whl |
|---|---|
| Size | 81.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d630b81ab805a1ee08cc4cef21e0ef219000854750cd9f6c74fb92dc4beddddf
|
|
BLAKE2b-256 checksum How to use checksums |
0a8a5feac31291c632545e10ff362eaf890f55facfa8793f7fba10dac9d49d08
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 31, 2026.
Transparency logSecurity for fastapi-observer 1.7.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for fastapi_observer-1.7.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 31, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.