1+ from dataclasses import dataclass , field
2+ from typing import cast
3+
14from fastapi import FastAPI
5+ from prometheus_client import CollectorRegistry , Counter
26from servicelib .fastapi .prometheus_instrumentation import (
37 setup_prometheus_instrumentation ,
48)
9+ from servicelib .instrumentation import MetricsBase , get_metrics_namespace
510
11+ from ._meta import APP_NAME
12+ from .core .errors import ConfigurationError
613from .core .settings import get_application_settings
714
15+ MONITOR_SERVICE_STARTED_LABELS : list [str ] = [
16+ "service_key" ,
17+ "service_tag" ,
18+ "simcore_user_agent" ,
19+ ]
20+
21+ MONITOR_SERVICE_STOPPED_LABELS : list [str ] = [
22+ "service_key" ,
23+ "service_tag" ,
24+ "result" ,
25+ "simcore_user_agent" ,
26+ ]
27+
28+
29+ @dataclass (slots = True , kw_only = True )
30+ class DirectorV0Instrumentation (MetricsBase ):
31+ registry : CollectorRegistry
32+
33+ services_started : Counter = field (init = False )
34+ services_stopped : Counter = field (init = False )
35+
36+ def __post_init__ (self ) -> None :
37+ self .services_started = Counter (
38+ name = "services_started_total" ,
39+ documentation = "Counts the services started" ,
40+ labelnames = MONITOR_SERVICE_STARTED_LABELS ,
41+ namespace = get_metrics_namespace (APP_NAME ),
42+ subsystem = self .subsystem ,
43+ registry = self .registry ,
44+ )
45+
46+ self .services_stopped = Counter (
47+ name = "services_stopped_total" ,
48+ documentation = "Counts the services stopped" ,
49+ labelnames = MONITOR_SERVICE_STOPPED_LABELS ,
50+ namespace = get_metrics_namespace (APP_NAME ),
51+ subsystem = self .subsystem ,
52+ registry = self .registry ,
53+ )
54+
855
956def setup (app : FastAPI ) -> None :
1057 app_settings = get_application_settings (app )
@@ -15,15 +62,10 @@ def setup(app: FastAPI) -> None:
1562 instrumentator = setup_prometheus_instrumentation (app )
1663
1764 async def on_startup () -> None :
18- # metrics_subsystem = (
19- # "dynamic" if app_settings.AUTOSCALING_NODES_MONITORING else "computational"
20- # )
21- # app.state.instrumentation = (
22- # AutoscalingInstrumentation( # pylint: disable=unexpected-keyword-arg
23- # registry=instrumentator.registry, subsystem=metrics_subsystem
24- # )
25- # )
26- ...
65+ metrics_subsystem = ""
66+ app .state .instrumentation = DirectorV0Instrumentation (
67+ registry = instrumentator .registry , subsystem = metrics_subsystem
68+ )
2769
2870 async def on_shutdown () -> None :
2971 ...
@@ -32,12 +74,12 @@ async def on_shutdown() -> None:
3274 app .add_event_handler ("shutdown" , on_shutdown )
3375
3476
35- # def get_instrumentation(app: FastAPI) -> AutoscalingInstrumentation :
36- # if not app.state.instrumentation:
37- # raise ConfigurationError(
38- # msg="Instrumentation not setup. Please check the configuration."
39- # )
40- # return cast(AutoscalingInstrumentation , app.state.instrumentation)
77+ def get_instrumentation (app : FastAPI ) -> DirectorV0Instrumentation :
78+ if not app .state .instrumentation :
79+ raise ConfigurationError (
80+ msg = "Instrumentation not setup. Please check the configuration."
81+ )
82+ return cast (DirectorV0Instrumentation , app .state .instrumentation )
4183
4284
4385def has_instrumentation (app : FastAPI ) -> bool :
0 commit comments