Skip to main content

Zero-glue FastAPI observability with security presets and runtime controls

Project description

fastapi-observer

CI Sponsor

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:

buymeacoffee.com/FYbPCSu


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 production profile)
  • 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: Forces LOG_LEVEL=DEBUG and disables any OpenTelemetry network overhead to keep local development fast and noisy.
  • OBS_PROFILE=production: Forces LOG_LEVEL=INFO, optimizes internal queues for massive throughput (LOG_QUEUE_MAX_SIZE=20000), and enforces the strict security 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_observer-1.3.2.tar.gz (92.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_observer-1.3.2-py3-none-any.whl (85.8 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_observer-1.3.2.tar.gz.

File metadata

  • Download URL: fastapi_observer-1.3.2.tar.gz
  • Upload date:
  • Size: 92.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fastapi_observer-1.3.2.tar.gz
Algorithm Hash digest
SHA256 f65fc1e175c055ee8824b4442f26db74b9bdfc555ada23b3adee2d375da6ecda
MD5 490b2d0261a630ce1df8772ba2200677
BLAKE2b-256 6c38ececf26088aedf99313e5c3155475a592d0f1492648e4826c9783c9aa879

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_observer-1.3.2.tar.gz:

Publisher: release.yml on Vitaee/FastapiObserver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastapi_observer-1.3.2-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_observer-1.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 29cd153f13abf0b9aa8af09d1c21198efcaee68f1421f3ad0d5cdf35191a7f1f
MD5 c9cff01d6cc22132bd37a6004345c5fb
BLAKE2b-256 1db9479cf0be138874adc5f35b671a160d9f34492b1572baf254e6e3a4dc3307

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_observer-1.3.2-py3-none-any.whl:

Publisher: release.yml on Vitaee/FastapiObserver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page