Skip to content

Commit

Permalink
use our own registry
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderegg committed Sep 19, 2024
1 parent 3942658 commit 2d9b910
Showing 1 changed file with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

0 comments on commit 2d9b910

Please sign in to comment.