diff --git a/packages/service-library/src/servicelib/fastapi/prometheus_instrumentation.py b/packages/service-library/src/servicelib/fastapi/prometheus_instrumentation.py index 2593ef0a7902..c4c6af97bdac 100644 --- a/packages/service-library/src/servicelib/fastapi/prometheus_instrumentation.py +++ b/packages/service-library/src/servicelib/fastapi/prometheus_instrumentation.py @@ -2,16 +2,20 @@ from fastapi import FastAPI +from prometheus_client import CollectorRegistry from prometheus_fastapi_instrumentator import Instrumentator def setup_prometheus_instrumentation(app: FastAPI) -> Instrumentator: - + # NOTE: use that registry to prevent having a global one + registry = CollectorRegistry(auto_describe=True) instrumentator = Instrumentator( - should_instrument_requests_inprogress=True, inprogress_labels=False + should_instrument_requests_inprogress=True, + inprogress_labels=False, + registry=registry, ).instrument(app) - async def on_startup(app: FastAPI) -> None: + async def _on_startup(app: FastAPI) -> None: instrumentator.expose(app, include_in_schema=False) def _unregister() -> None: @@ -21,6 +25,6 @@ def _unregister() -> None: ): instrumentator.registry.unregister(collector) - app.add_event_handler("startup", on_startup) + app.add_event_handler("startup", _on_startup) app.add_event_handler("shutdown", _unregister) return instrumentator