@@ -22,12 +22,12 @@ def initialize_prometheus_instrumentation(app: FastAPI) -> None:
2222 app .state .prometheus_instrumentator .instrument (app )
2323
2424
25- async def _startup (app : FastAPI ) -> None :
25+ def _startup (app : FastAPI ) -> None :
2626 assert isinstance (app .state .prometheus_instrumentator , Instrumentator ) # nosec
2727 app .state .prometheus_instrumentator .expose (app , include_in_schema = False )
2828
2929
30- async def _shutdown (app : FastAPI ) -> None :
30+ def _shutdown (app : FastAPI ) -> None :
3131 assert isinstance (app .state .prometheus_registry , CollectorRegistry ) # nosec
3232 registry = app .state .prometheus_registry
3333 for collector in list (registry ._collector_to_names .keys ()): # noqa: SLF001
@@ -42,15 +42,21 @@ def get_prometheus_instrumentator(app: FastAPI) -> Instrumentator:
4242def setup_prometheus_instrumentation (app : FastAPI ) -> Instrumentator :
4343 initialize_prometheus_instrumentation (app )
4444
45- app .add_event_handler ("startup" , _startup )
46- app .add_event_handler ("shutdown" , _shutdown )
45+ async def _on_startup () -> None :
46+ _startup (app )
47+
48+ def _on_shutdown () -> None :
49+ _shutdown (app )
50+
51+ app .add_event_handler ("startup" , _on_startup )
52+ app .add_event_handler ("shutdown" , _on_shutdown )
4753
4854 return get_prometheus_instrumentator (app )
4955
5056
5157async def lifespan_prometheus_instrumentation (app : FastAPI ) -> AsyncIterator [State ]:
5258 # NOTE: requires ``initialize_prometheus_instrumentation`` to be called before the
5359 # lifespan of the applicaiton runs, usually rigth after the ``FastAPI`` instance is created
54- await _startup (app )
60+ _startup (app )
5561 yield {}
56- await _shutdown (app )
62+ _shutdown (app )
0 commit comments