implement prometheus fast-api-instrumentor for http service metrics (#15657)

This commit is contained in:
daniel-salib 2025-03-28 17:12:02 -07:00 committed by GitHub
parent 26df46ee59
commit f3f8d8fff4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -311,6 +311,7 @@ def mount_metrics(app: FastAPI):
# See https://prometheus.github.io/client_python/multiprocess/
from prometheus_client import (CollectorRegistry, make_asgi_app,
multiprocess)
from prometheus_fastapi_instrumentator import Instrumentator
prometheus_multiproc_dir_path = os.getenv("PROMETHEUS_MULTIPROC_DIR", None)
if prometheus_multiproc_dir_path is not None:
@ -318,6 +319,16 @@ def mount_metrics(app: FastAPI):
prometheus_multiproc_dir_path)
registry = CollectorRegistry()
multiprocess.MultiProcessCollector(registry)
Instrumentator(
excluded_handlers=[
"/metrics",
"/health",
"/load",
"/ping",
"/version",
],
registry=registry,
).add().instrument(app).expose(app)
# Add prometheus asgi middleware to route /metrics requests
metrics_route = Mount("/metrics", make_asgi_app(registry=registry))