22import dataclasses
33import typing
44
5+ from litestar .plugins .prometheus import PrometheusConfig , PrometheusController
6+
57from lite_bootstrap .bootstrappers .base import BaseBootstrapper
68from lite_bootstrap .instruments .healthchecks_instrument import HealthChecksInstrument , HealthCheckTypedDict
79from lite_bootstrap .instruments .logging_instrument import LoggingInstrument
810from lite_bootstrap .instruments .opentelemetry_instrument import OpenTelemetryInstrument
11+ from lite_bootstrap .instruments .prometheus_instrument import PrometheusInstrument
912from lite_bootstrap .instruments .sentry_instrument import SentryInstrument
1013from lite_bootstrap .service_config import ServiceConfig
1114
@@ -63,6 +66,26 @@ def bootstrap(self, service_config: ServiceConfig, app_config: AppConfig | None
6366class LitestarSentryInstrument (SentryInstrument ): ...
6467
6568
69+ @dataclasses .dataclass (kw_only = True , frozen = True )
70+ class LitestarPrometheusInstrument (PrometheusInstrument ):
71+ additional_params : dict [str , typing .Any ] = dataclasses .field (default_factory = dict )
72+
73+ def bootstrap (self , service_config : ServiceConfig , app_config : AppConfig | None = None ) -> None :
74+ class LitestarPrometheusController (PrometheusController ):
75+ path = self .metrics_path
76+ include_in_schema = self .metrics_include_in_schema
77+ openmetrics_format = True
78+
79+ litestar_prometheus_config = PrometheusConfig (
80+ app_name = service_config .service_name ,
81+ ** self .additional_params ,
82+ )
83+
84+ if app_config :
85+ app_config .route_handlers .append (LitestarPrometheusController )
86+ app_config .middleware .append (litestar_prometheus_config .middleware )
87+
88+
6689@dataclasses .dataclass (kw_only = True , slots = True , frozen = True )
6790class LitestarBootstrapper (BaseBootstrapper [AppConfig , litestar .Litestar ]):
6891 bootstrap_object : AppConfig
@@ -71,6 +94,7 @@ class LitestarBootstrapper(BaseBootstrapper[AppConfig, litestar.Litestar]):
7194 | LitestarSentryInstrument
7295 | LitestarHealthChecksInstrument
7396 | LitestarLoggingInstrument
97+ | LitestarPrometheusInstrument
7498 ]
7599 service_config : ServiceConfig
76100
0 commit comments