Prometheus metrics middleware, circuit breaker listener, and metric helpers for FastAPI — zero project-specific dependencies
Project description
fastapi-telemetry
📢 Hobby Project Notice: This is a research and learning project exploring FastAPI observability and Prometheus metrics best practices. Feel free to use it as a reference, report issues, or suggest improvements! Contributions and feedback are always welcome.
Prometheus metrics middleware, circuit breaker listener, and metric helpers for FastAPI —
zero project-specific dependencies (only Starlette and prometheus-client).
Features
PrometheusMiddleware— HTTP request duration, count, and in-flight metrics out of the box via injectable callbacks (no coupling to a specific metrics facade)CircuitBreakerMetricsListener—pybreakerlistener that exports circuit breaker state, state-changes, and failure counts to Prometheus (optional dependency)get_or_create_counter/get_or_create_gauge/get_or_create_histogram— safe helpers that return an existing metric from the registry instead of raisingValueErroron duplicate registration (useful with--reload)
Installation
# Core (middleware + helpers)
pip install fastapi-telemetry
# With circuit-breaker support
pip install "fastapi-telemetry[circuit-breaker]"
Quick start
from fastapi import FastAPI
from fastapi_telemetry import PrometheusMiddleware, CircuitBreakerMetricsListener
from prometheus_client import make_asgi_app
app = FastAPI()
# Mount Prometheus scrape endpoint
app.mount("/metrics", make_asgi_app())
# Wire up the middleware with your own metric callbacks
app.add_middleware(
PrometheusMiddleware,
request_start_callback=lambda method, path: None, # replace with your gauge.inc()
request_end_callback=lambda m, p, s, d: None, # replace with your histogram
error_callback=lambda error_type, path: None, # replace with your counter.inc()
)
Circuit breaker
from pybreaker import CircuitBreaker
from fastapi_telemetry import CircuitBreakerMetricsListener
cb = CircuitBreaker(
name="redis",
listeners=[CircuitBreakerMetricsListener(service_name="redis")],
)
License
MIT
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_telemetry-0.0.2.tar.gz.
File metadata
- Download URL: fastapi_telemetry-0.0.2.tar.gz
- Upload date:
- Size: 49.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"openSUSE Tumbleweed","version":"20260223","id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dcc2a32c81e1cf853b81f8e4fbca85e64ff6be4138bb0214d83ddbce1e8f628e
|
|
| MD5 |
77e11aa7feaf4538a8e1fb8c32a3f157
|
|
| BLAKE2b-256 |
0a4de39174532ecdd58483c2563c9e369d98707e49e2665723ed402616f4ae93
|
File details
Details for the file fastapi_telemetry-0.0.2-py3-none-any.whl.
File metadata
- Download URL: fastapi_telemetry-0.0.2-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"openSUSE Tumbleweed","version":"20260223","id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c125a676b8bb3fefcbaceb3594bf81c09b061cf9c265c7e577fae4abfde88d15
|
|
| MD5 |
e1e8b12afe00a2f90bc7470fba0e6c2b
|
|
| BLAKE2b-256 |
894bb7ed36080889f106cde8c4b17abeb6ee7482fa29a12fba03fc0b6d91b5a2
|