Skip to content

Commit 8bf68c8

Browse files
michael.yakmichaelyaakoby
authored andcommitted
1 parent 0910063 commit 8bf68c8

File tree

6 files changed

+18
-6
lines changed

6 files changed

+18
-6
lines changed

pyctuator/health/health_provider.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from abc import ABC
22
from dataclasses import dataclass
33
from enum import Enum
4+
from http import HTTPStatus
45

56
from typing import Mapping
67

@@ -27,6 +28,11 @@ class HealthSummary:
2728
status: Status
2829
details: Mapping[str, HealthStatus]
2930

31+
def http_status(self) -> int:
32+
if self.status == Status.DOWN:
33+
return HTTPStatus.SERVICE_UNAVAILABLE
34+
return HTTPStatus.OK
35+
3036

3137
class HealthProvider(ABC):
3238
def is_supported(self) -> bool:

pyctuator/impl/aiohttp_pyctuator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ async def get_info(request: web.Request) -> web.Response:
3737
return web.json_response(pyctuator_impl.get_app_info(), dumps=custom_dumps)
3838

3939
async def get_health(request: web.Request) -> web.Response:
40-
return web.json_response(pyctuator_impl.get_health(), dumps=custom_dumps)
40+
health = pyctuator_impl.get_health()
41+
return web.json_response(health, status=health.http_status(), dumps=custom_dumps)
4142

4243
async def get_metric_names(request: web.Request) -> web.Response:
4344
return web.json_response(pyctuator_impl.get_metric_names(), dumps=custom_dumps)

pyctuator/impl/fastapi_pyctuator.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@ def get_info() -> Dict:
7474
return pyctuator_impl.get_app_info()
7575

7676
@router.get("/health", include_in_schema=include_in_openapi_schema, tags=["pyctuator"])
77-
def get_health() -> HealthSummary:
78-
return pyctuator_impl.get_health()
77+
def get_health(response: Response) -> HealthSummary:
78+
health = pyctuator_impl.get_health()
79+
response.status_code = health.http_status()
80+
return health
7981

8082
@router.get("/metrics", include_in_schema=include_in_openapi_schema, tags=["pyctuator"])
8183
def get_metric_names() -> MetricNames:

pyctuator/impl/flask_pyctuator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ def get_info() -> Any:
8585

8686
@flask_blueprint.route("/health")
8787
def get_health() -> Any:
88-
return jsonify(pyctuator_impl.get_health())
88+
health = pyctuator_impl.get_health()
89+
return jsonify(health), health.http_status()
8990

9091
@flask_blueprint.route("/metrics")
9192
def get_metric_names() -> Any:

pyctuator/impl/tornado_pyctuator.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ class HealthHandler(AbstractPyctuatorHandler):
5858
def get(self) -> None:
5959
assert self.pyctuator_router is not None
6060
assert self.dumps is not None
61-
self.write(self.dumps(self.pyctuator_router.pyctuator_impl.get_health()))
61+
health = self.pyctuator_router.pyctuator_impl.get_health()
62+
self.set_status(health.http_status())
63+
self.write(self.dumps(health))
6264

6365

6466
# GET /metrics

tests/test_pyctuator_e2e.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def mock_disk_usage(path: str) -> MockDiskUsage:
132132

133133
monkeypatch.setattr(psutil, "disk_usage", mock_disk_usage)
134134
response = requests.get(endpoints.health, timeout=REQUEST_TIMEOUT)
135-
assert response.status_code == HTTPStatus.OK
135+
assert response.status_code == HTTPStatus.SERVICE_UNAVAILABLE
136136
assert response.json()["status"] == "DOWN"
137137
disk_space_health = response.json()["details"]["diskSpace"]
138138
assert disk_space_health["status"] == "DOWN"

0 commit comments

Comments
 (0)