Skip to content

Commit 3118a2a

Browse files
committed
use our own registry
1 parent 82fdefa commit 3118a2a

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

packages/service-library/src/servicelib/fastapi/prometheus_instrumentation.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@
22

33

44
from fastapi import FastAPI
5+
from prometheus_client import CollectorRegistry
56
from prometheus_fastapi_instrumentator import Instrumentator
67

78

89
def setup_prometheus_instrumentation(app: FastAPI) -> Instrumentator:
9-
10+
# NOTE: use that registry to prevent having a global one
11+
registry = CollectorRegistry(auto_describe=True)
1012
instrumentator = Instrumentator(
11-
should_instrument_requests_inprogress=True, inprogress_labels=False
13+
should_instrument_requests_inprogress=True,
14+
inprogress_labels=False,
15+
registry=registry,
1216
).instrument(app)
1317

14-
async def on_startup(app: FastAPI) -> None:
18+
async def _on_startup(app: FastAPI) -> None:
1519
instrumentator.expose(app, include_in_schema=False)
1620

1721
def _unregister() -> None:
@@ -21,6 +25,6 @@ def _unregister() -> None:
2125
):
2226
instrumentator.registry.unregister(collector)
2327

24-
app.add_event_handler("startup", on_startup)
28+
app.add_event_handler("startup", _on_startup)
2529
app.add_event_handler("shutdown", _unregister)
2630
return instrumentator

0 commit comments

Comments
 (0)