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.1.tar.gz (91.6 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.1-py3-none-any.whl (85.3 kB view details)

Uploaded Python 3

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

Hashes for fastapi_observer-1.3.1.tar.gz
Algorithm Hash digest
SHA256 607fc1ce1df98920932581b133bebfe25ca9b050c6eac64b11f23191ea81301d
MD5 ef497c9fd7858bb0f14376091d406f44
BLAKE2b-256 11d5894f1d2b77940d2599e152ae09ec8ce81fc8976dbb1f5014511c191080a8

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_observer-1.3.1.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.1-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_observer-1.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6fb5ca893d2ce6fe5bef5f76e69354c5cf2940bb624a3f146a9100470d3adf07
MD5 9fe79cf6234eac63913ff4d73d38510f
BLAKE2b-256 234270acc8ec837d1e77e5f475a987c944164d1cd85850528deef78acc8af572

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_observer-1.3.1-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