← All projects
📡
ProductionIO2i Outsourcing Pvt. Ltd. · 2026

Observability Layer

Prometheus metrics, JSON structured logging & request tracing for FastAPI

PrometheusPythonFastAPI MiddlewareDockerJSON LoggingContextVar

4 instruments

Prometheus metrics

20MB × 5 backups

Log rotation

UUID via ContextVar

Request tracing

Docker-native logs

Deployment

A production observability module built for the ODR portal. Provides Prometheus counters and histograms for every HTTP request, JSON-structured rotating log files with request IDs, and an async measurement wrapper for upstream API calls — all with zero external observability platform dependency. Designed for Docker-native log collection and Prometheus scraping.

Initial Thought

Running a multi-tenant legal platform in production with no observability is not viable — you need to know which endpoints are slow, which upstream calls are failing, and be able to trace a specific user's request through the logs when something goes wrong.

Turning Point

A single observability.py module that plugs into FastAPI middleware with four Prometheus metrics, a JSON-structured logger with file rotation, and a ContextVar-based request ID that flows through every log line automatically. The async measure_lwtodr_call() wrapper instruments every call to the upstream Frappe API without changing the call site.

Architecture

The logging_middleware wraps every request: it assigns a UUID request ID via ContextVar, times the request, increments the appropriate Prometheus counters and histograms, logs a structured JSON record on completion, and injects X-Request-ID into the response header. The measure_lwtodr_call() coroutine wrapper times async upstream calls and records them to a separate histogram. A /metrics endpoint exposes the Prometheus scrape format.

Outcome

Full request tracing and latency percentiles in production with no external platform needed. Error rates are alertable via Prometheus rules. Every log line carries a request ID that links it to a specific user request.