Skip to content

Commit 898e7ef

Browse files
authored
Return 503 from an unhealthy health check (#370)
1 parent 75b0c87 commit 898e7ef

7 files changed

Lines changed: 10 additions & 10 deletions

File tree

‎docs/introduction/configuration.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ Additional params:
486486
- `health_checks_include_in_schema`
487487
- `health_checks_probes`: named async callables, each returning `bool`, that the route runs on every request
488488

489-
The health check route on Litestar, FastAPI, FastMCP and FastStream runs every probe concurrently. On FastStream the probes run alongside the broker pings. The route returns 200 with `health_status: true` when everything passes. If any probe returns `False` or raises, the route returns 500 with the same JSON body and `health_status: false`. Each failed probe logs one warning with its name; the response body does not say which probe failed.
489+
The health check route on Litestar, FastAPI, FastMCP and FastStream runs every probe concurrently. On FastStream the probes run alongside the broker pings. The route returns 200 with `health_status: true` when everything passes. If any probe returns `False` or raises, the route returns 503 Service Unavailable with the same JSON body and `health_status: false`. Each failed probe logs one warning with its name; the response body does not say which probe failed.
490490

491491
```python
492492
import functools

‎lite_bootstrap/bootstrappers/fastapi_bootstrapper.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def build_fastapi_health_check_router(self) -> "fastapi.APIRouter":
194194
async def health_check_handler(response: fastapi.Response) -> HealthCheckTypedDict:
195195
health_status = await self.define_health_status()
196196
if not health_status:
197-
response.status_code = fastapi.status.HTTP_500_INTERNAL_SERVER_ERROR
197+
response.status_code = fastapi.status.HTTP_503_SERVICE_UNAVAILABLE
198198
return self.render_health_check_data(health_status=health_status)
199199

200200
return fastapi_router

‎lite_bootstrap/bootstrappers/fastmcp_bootstrapper.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ async def health_check_handler(_: "Request") -> "JSONResponse":
229229
health_status = await self.define_health_status()
230230
return JSONResponse(
231231
dict(self.render_health_check_data(health_status=health_status)),
232-
status_code=status.HTTP_200_OK if health_status else status.HTTP_500_INTERNAL_SERVER_ERROR,
232+
status_code=status.HTTP_200_OK if health_status else status.HTTP_503_SERVICE_UNAVAILABLE,
233233
)
234234

235235

‎lite_bootstrap/bootstrappers/faststream_bootstrapper.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ async def check_health(_: object) -> "AsgiResponse":
127127
health_status = await self.define_health_status()
128128
return AsgiResponse(
129129
json.dumps(self.render_health_check_data(health_status=health_status)).encode(),
130-
200 if health_status else 500,
130+
200 if health_status else 503,
131131
headers={"content-type": "application/json"},
132132
)
133133

‎lite_bootstrap/bootstrappers/litestar_bootstrapper.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ async def health_check_handler() -> litestar.Response[HealthCheckTypedDict]:
287287
health_status = await self.define_health_status()
288288
return litestar.Response(
289289
self.render_health_check_data(health_status=health_status),
290-
status_code=status_codes.HTTP_200_OK if health_status else status_codes.HTTP_500_INTERNAL_SERVER_ERROR,
290+
status_code=status_codes.HTTP_200_OK if health_status else status_codes.HTTP_503_SERVICE_UNAVAILABLE,
291291
)
292292

293293
return litestar.Router(

‎tests/conftest.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ async def raising_probe() -> bool:
6565

6666
HEALTH_PROBE_CASES: typing.Final = [
6767
pytest.param({"db": passing_probe, "cache": passing_probe}, 200, True, [], id="all-pass"),
68-
pytest.param({"db": passing_probe, "cache": failing_probe}, 500, False, ["cache"], id="one-returns-false"),
69-
pytest.param({"db": passing_probe, "cache": raising_probe}, 500, False, ["cache"], id="one-raises"),
68+
pytest.param({"db": passing_probe, "cache": failing_probe}, 503, False, ["cache"], id="one-returns-false"),
69+
pytest.param({"db": passing_probe, "cache": raising_probe}, 503, False, ["cache"], id="one-raises"),
7070
]
7171

7272

‎tests/test_faststream_bootstrap.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ async def test_faststream_bootstrap_health_check_wo_broker() -> None:
132132
test_client = TestClient(app=application)
133133

134134
response = test_client.get(bootstrap_config.health_checks_path)
135-
assert response.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR
135+
assert response.status_code == status.HTTP_503_SERVICE_UNAVAILABLE
136136
assert response.json() == {
137137
"health_status": False,
138138
"service_name": "microservice",
@@ -305,7 +305,7 @@ async def test_faststream_health_check_fails_when_second_broker_is_down(broker:
305305
TestClient(app=application) as test_client,
306306
):
307307
response = test_client.get(config.health_checks_path)
308-
assert response.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR
308+
assert response.status_code == status.HTTP_503_SERVICE_UNAVAILABLE
309309
assert response.json()["health_status"] is False
310310
finally:
311311
bootstrapper.teardown()
@@ -573,7 +573,7 @@ async def test_faststream_health_check_fails_on_broker_ping_despite_passing_prob
573573
finally:
574574
bootstrapper.teardown()
575575

576-
assert response.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR
576+
assert response.status_code == status.HTTP_503_SERVICE_UNAVAILABLE
577577
assert response.json()["health_status"] is False
578578

579579

0 commit comments

Comments
 (0)