Skip to content

Commit 1c40b4d

Browse files
authored
Merge pull request #46 from modern-python/feature/allow-additional-health-checker-in-faststream
allow additional health checker coroutine for faststream
2 parents 10c611d + 15d9200 commit 1c40b4d

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

docs/introduction/configuration.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,9 @@ Additional params:
126126

127127
- `health_checks_path`
128128
- `health_checks_include_in_schema`
129+
130+
### Health checks FastStream
131+
132+
Additional params:
133+
134+
- `health_checks_additional_checker` - additional coroutine to check service health

lite_bootstrap/bootstrappers/faststream_bootstrapper.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class FastStreamConfig(HealthChecksConfig, LoggingConfig, OpentelemetryConfig, P
5656
broker: typing.Optional["BrokerUsecase[typing.Any, typing.Any]"] = None
5757
opentelemetry_middleware_cls: type[FastStreamTelemetryMiddlewareProtocol] | None = None
5858
prometheus_middleware_cls: type[FastStreamPrometheusMiddlewareProtocol] | None = None
59+
health_checks_additional_checker: typing.Callable[[], typing.Coroutine[bool, typing.Any, typing.Any]] | None = None
5960

6061

6162
@dataclasses.dataclass(kw_only=True, slots=True, frozen=True)
@@ -79,7 +80,12 @@ async def _define_health_status(self) -> bool:
7980
if not self.bootstrap_config.application or not self.bootstrap_config.application.broker:
8081
return False
8182

82-
return await self.bootstrap_config.application.broker.ping(timeout=5)
83+
additional_check = (
84+
await self.bootstrap_config.health_checks_additional_checker()
85+
if self.bootstrap_config.health_checks_additional_checker
86+
else True
87+
)
88+
return additional_check and await self.bootstrap_config.application.broker.ping(timeout=5)
8389

8490

8591
@dataclasses.dataclass(kw_only=True, frozen=True)

tests/test_fastapi_offline_docs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,5 @@ def test_fastapi_offline_docs_root_path() -> None:
4646
def test_fastapi_offline_docs_raises_without_openapi_url() -> None:
4747
app = FastAPI(openapi_url=None)
4848

49-
with pytest.raises(RuntimeError, match="No app.openapi_url specified"):
49+
with pytest.raises(RuntimeError, match=r"No app.openapi_url specified"):
5050
enable_offline_docs(app, static_path="/static")

tests/test_faststream_bootstrap.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ def broker() -> RedisBroker:
2121
return RedisBroker()
2222

2323

24-
def build_faststream_config(broker: BrokerUsecase[typing.Any, typing.Any] | None = None) -> FastStreamConfig:
24+
def build_faststream_config(
25+
broker: BrokerUsecase[typing.Any, typing.Any] | None = None,
26+
health_checks_additional_checker: typing.Callable[[], typing.Coroutine[bool, typing.Any, typing.Any]] | None = None,
27+
) -> FastStreamConfig:
2528
return FastStreamConfig(
2629
service_name="microservice",
2730
service_version="2.0.0",
@@ -37,6 +40,7 @@ def build_faststream_config(broker: BrokerUsecase[typing.Any, typing.Any] | None
3740
health_checks_path="/custom-health/",
3841
logging_buffer_capacity=0,
3942
broker=broker,
43+
health_checks_additional_checker=health_checks_additional_checker,
4044
)
4145

4246

@@ -74,6 +78,20 @@ async def test_faststream_bootstrap_health_check_wo_broker() -> None:
7478
assert response.text == "Service is unhealthy"
7579

7680

81+
async def test_faststream_bootstrap_additional_health_checker(broker: RedisBroker) -> None:
82+
async def custom_checker() -> bool:
83+
return False
84+
85+
bootstrap_config = build_faststream_config(broker=broker, health_checks_additional_checker=custom_checker)
86+
bootstrapper = FastStreamBootstrapper(bootstrap_config=bootstrap_config)
87+
application = bootstrapper.bootstrap()
88+
test_client = TestClient(app=application)
89+
90+
response = test_client.get(bootstrap_config.health_checks_path)
91+
assert response.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR
92+
assert response.text == "Service is unhealthy"
93+
94+
7795
def test_faststream_bootstrapper_not_ready() -> None:
7896
with emulate_package_missing("faststream"), pytest.raises(RuntimeError, match="faststream is not installed"):
7997
FastStreamBootstrapper(bootstrap_config=FastStreamConfig())

0 commit comments

Comments
 (0)