Zero-glue FastAPI observability with security presets and runtime controls
Project description
fastapi-observer
Zero-glue observability for FastAPI.
fastapi-observer gives you structured JSON logs, request correlation, Prometheus metrics, OpenTelemetry tracing, security redaction presets, and runtime controls in one install step and one function call.
Supported Python versions: 3.10 to 3.14
Compatibility Matrix
| Component | Supported / Tested |
|---|---|
| Python | 3.10 to 3.14 (CI matrix) |
| FastAPI | >=0.129.0 |
| Starlette | >=0.52.1 |
| pydantic-settings | >=2.10.1 |
| Prometheus backend | prometheus-client>=0.24.1 (optional extra) |
| OpenTelemetry | opentelemetry-api/sdk/exporter>=1.39.1 (optional extra) |
| Loguru bridge | loguru>=0.7.2 (optional extra) |
Why This Package Exists
Most FastAPI services eventually need the same observability plumbing:
- Structured JSON logging
- Request and trace correlation
- Metrics for dashboards and alerts
- OpenTelemetry setup
- Redaction/sanitization for sensitive data
- Runtime controls for incident response
Teams usually implement this as custom glue code in every service. That costs engineering time and creates drift between services.
fastapi-observer replaces this repeated wiring with a consistent, secure-by-default setup.
Sponsor
If this library saves you engineering time, you can support maintenance here:
What You Get Immediately
After one call to install_observability():
| Capability | Included | Default |
|---|---|---|
| Structured JSON logs | Yes | Enabled |
| Request ID correlation | Yes | Enabled |
| Trace/span IDs in logs | Yes (with OTel) | Off until OTel enabled |
Prometheus /metrics |
Yes | Off until metrics_enabled=True |
| Native FastAPI Lifespan | Yes | Explicit opt-in via observability_lifespan |
| Auto-discovery | Yes | Excluded routes (/docs, etc.) & DB engines |
| Sensitive-data redaction | Yes | Enabled |
Security presets (strict, pci, gdpr) |
Yes | Available |
| Runtime control endpoint | Yes | Off until enabled |
| Plugin hooks for enrichment/hooks | Yes | Available |
Install
# Core (logging + metrics + security)
pip install fastapi-observer
# Prometheus metrics support
pip install "fastapi-observer[prometheus]"
# Loguru coexistence bridge support
pip install "fastapi-observer[loguru]"
# OpenTelemetry tracing/logs support
pip install "fastapi-observer[otel]"
# Everything
pip install "fastapi-observer[all]"
Import path:
import fastapiobserver
5-Minute Quick Start
5-Minute Quick Start ("Zero-Glue")
You can configure the entire library via environment variables simply by calling install_observability(app).
export APP_NAME="orders-api"
export SERVICE_NAME="orders"
export ENVIRONMENT="production"
export METRICS_ENABLED="true"
# Optional: Set a profile ("development" or "production") to auto-configure log levels, queues, and redaction strictness
export OBS_PROFILE="production"
from fastapi import FastAPI
from fastapiobserver import install_observability
app = FastAPI()
# Wires up logging, metrics, OTel, and security automatically from env vars
install_observability(app)
@app.get("/orders/{order_id}")
def get_order(order_id: int) -> dict[str, int]:
return {"order_id": order_id}
@app.get("/hidden", include_in_schema=False)
def hidden_endpoint():
# This endpoint is automatically excluded from
# Prometheus metrics and OTel tracing.
return {"status": "ok"}
Run:
uvicorn main:app --reload
Now you have:
- Structured request logs on every request
- Request ID propagation
- Sanitized event payloads (enforced by the
productionprofile) - Prometheus metrics at
/metrics
Environment Profiles (OBS_PROFILE)
To dramatically reduce boilerplate, you can use the OBS_PROFILE environment variable to automatically set sensible defaults for your environment:
OBS_PROFILE=development: ForcesLOG_LEVEL=DEBUGand disables any OpenTelemetry network overhead to keep local development fast and noisy.OBS_PROFILE=production: ForcesLOG_LEVEL=INFO, optimizes internal queues for massive throughput (LOG_QUEUE_MAX_SIZE=20000), and enforces thestrictsecurity redaction preset.
(Explicit env vars or Python arguments will always override profile defaults).
Documentation Map
For deep-dive documentation, please explore the official documentation site (or read the docs/ folder directly):
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_observer-1.3.1.tar.gz.
File metadata
- Download URL: fastapi_observer-1.3.1.tar.gz
- Upload date:
- Size: 91.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
607fc1ce1df98920932581b133bebfe25ca9b050c6eac64b11f23191ea81301d
|
|
| MD5 |
ef497c9fd7858bb0f14376091d406f44
|
|
| BLAKE2b-256 |
11d5894f1d2b77940d2599e152ae09ec8ce81fc8976dbb1f5014511c191080a8
|
Provenance
The following attestation bundles were made for fastapi_observer-1.3.1.tar.gz:
Publisher:
release.yml on Vitaee/FastapiObserver
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_observer-1.3.1.tar.gz -
Subject digest:
607fc1ce1df98920932581b133bebfe25ca9b050c6eac64b11f23191ea81301d - Sigstore transparency entry: 1003298616
- Sigstore integration time:
-
Permalink:
Vitaee/FastapiObserver@5736061da17b3a4e51cd4b84cc18687373fe7490 -
Branch / Tag:
refs/tags/v1.3.1 - Owner: https://github.com/Vitaee
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@5736061da17b3a4e51cd4b84cc18687373fe7490 -
Trigger Event:
push
-
Statement type:
File details
Details for the file fastapi_observer-1.3.1-py3-none-any.whl.
File metadata
- Download URL: fastapi_observer-1.3.1-py3-none-any.whl
- Upload date:
- Size: 85.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6fb5ca893d2ce6fe5bef5f76e69354c5cf2940bb624a3f146a9100470d3adf07
|
|
| MD5 |
9fe79cf6234eac63913ff4d73d38510f
|
|
| BLAKE2b-256 |
234270acc8ec837d1e77e5f475a987c944164d1cd85850528deef78acc8af572
|
Provenance
The following attestation bundles were made for fastapi_observer-1.3.1-py3-none-any.whl:
Publisher:
release.yml on Vitaee/FastapiObserver
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_observer-1.3.1-py3-none-any.whl -
Subject digest:
6fb5ca893d2ce6fe5bef5f76e69354c5cf2940bb624a3f146a9100470d3adf07 - Sigstore transparency entry: 1003298623
- Sigstore integration time:
-
Permalink:
Vitaee/FastapiObserver@5736061da17b3a4e51cd4b84cc18687373fe7490 -
Branch / Tag:
refs/tags/v1.3.1 - Owner: https://github.com/Vitaee
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@5736061da17b3a4e51cd4b84cc18687373fe7490 -
Trigger Event:
push
-
Statement type: