11from __future__ import annotations
2- import contextlib
32import dataclasses
43import json
54import typing
65
6+ from lite_bootstrap import import_checker
77from lite_bootstrap .bootstrappers .base import BaseBootstrapper
88from lite_bootstrap .instruments .healthchecks_instrument import HealthChecksConfig , HealthChecksInstrument
99from lite_bootstrap .instruments .logging_instrument import LoggingConfig , LoggingInstrument
1212from lite_bootstrap .instruments .sentry_instrument import SentryConfig , SentryInstrument
1313
1414
15- with contextlib . suppress ( ImportError ) :
15+ if import_checker . is_faststream_installed :
1616 import faststream
17- import prometheus_client
1817 from faststream .asgi import AsgiFastStream , AsgiResponse
1918 from faststream .asgi import get as handle_get
2019 from faststream .broker .core .usecase import BrokerUsecase
20+
21+ if import_checker .is_prometheus_client_installed :
22+ import prometheus_client
23+
24+ if import_checker .is_opentelemetry_installed :
2125 from opentelemetry .metrics import Meter , MeterProvider
2226 from opentelemetry .trace import TracerProvider , get_tracer_provider
2327
@@ -87,9 +91,10 @@ class FastStreamLoggingInstrument(LoggingInstrument):
8791@dataclasses .dataclass (kw_only = True , frozen = True )
8892class FastStreamOpenTelemetryInstrument (OpenTelemetryInstrument ):
8993 bootstrap_config : FastStreamConfig
94+ not_ready_message = OpenTelemetryInstrument .not_ready_message + " or opentelemetry_middleware_cls is empty"
9095
9196 def is_ready (self ) -> bool :
92- return bool ( self . bootstrap_config . opentelemetry_middleware_cls and super ().is_ready ())
97+ return super ().is_ready () and bool ( self . bootstrap_config . opentelemetry_middleware_cls )
9398
9499 def bootstrap (self ) -> None :
95100 if self .bootstrap_config .opentelemetry_middleware_cls and self .bootstrap_config .application .broker :
@@ -109,9 +114,18 @@ class FastStreamPrometheusInstrument(PrometheusInstrument):
109114 collector_registry : prometheus_client .CollectorRegistry = dataclasses .field (
110115 default_factory = prometheus_client .CollectorRegistry , init = False
111116 )
117+ not_ready_message = (
118+ PrometheusInstrument .not_ready_message
119+ + " or prometheus_middleware_cls is missing or prometheus_client is not installed"
120+ )
112121
113122 def is_ready (self ) -> bool :
114- return bool (self .bootstrap_config .prometheus_middleware_cls and super ().is_ready ())
123+ return (
124+ super ().is_ready ()
125+ and import_checker .is_prometheus_client_installed
126+ and bool (self .bootstrap_config .prometheus_middleware_cls )
127+ and import_checker .is_prometheus_client_installed
128+ )
115129
116130 def bootstrap (self ) -> None :
117131 self .bootstrap_config .application .mount (
@@ -124,6 +138,8 @@ def bootstrap(self) -> None:
124138
125139
126140class FastStreamBootstrapper (BaseBootstrapper [AsgiFastStream ]):
141+ __slots__ = "bootstrap_config" , "instruments"
142+
127143 instruments_types : typing .ClassVar = [
128144 FastStreamOpenTelemetryInstrument ,
129145 FastStreamSentryInstrument ,
@@ -132,7 +148,10 @@ class FastStreamBootstrapper(BaseBootstrapper[AsgiFastStream]):
132148 FastStreamPrometheusInstrument ,
133149 ]
134150 bootstrap_config : FastStreamConfig
135- __slots__ = "bootstrap_config" , "instruments"
151+ not_ready_message = "faststream is not installed"
152+
153+ def is_ready (self ) -> bool :
154+ return import_checker .is_faststream_installed
136155
137156 def __init__ (self , bootstrap_config : FastStreamConfig ) -> None :
138157 super ().__init__ (bootstrap_config )
0 commit comments