Skip to content

Commit e695de7

Browse files
authored
Configurable status for failed heartbeat (#98)
* Configurable heartbeat status * Mention setting in docs
1 parent 39ea4e4 commit e695de7

File tree

8 files changed

+34
-5
lines changed

8 files changed

+34
-5
lines changed

docs/django.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,9 @@ spec:
339339
:statuscode 200: no error
340340
:statuscode 500: there was a warning or error
341341

342+
.. note:: Failed status code can be configured with the ``DOCKERFLOW_HEARTBEAT_FAILED_STATUS_CODE``
343+
setting (eg. 503 instead of 500)
344+
342345
.. http:get:: /__lbheartbeat__
343346
344347
The view that simply returns a successful HTTP response so that a load

docs/fastapi.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ spec:
244244
:statuscode 200: no error
245245
:statuscode 500: there was an error
246246

247+
.. note:: Failed status code can be configured with the ``app.state.DOCKERFLOW_HEARTBEAT_FAILED_STATUS_CODE``
248+
attribute (eg. 503 instead of 500)
249+
247250
.. http:get:: /__lbheartbeat__
248251
249252
The view that simply returns a successful HTTP response so that a load

docs/flask.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,9 @@ spec:
392392
:statuscode 200: no error
393393
:statuscode 500: there was a warning or error
394394

395+
.. note:: Failed status code can be configured with the ``DOCKERFLOW_HEARTBEAT_FAILED_STATUS_CODE``
396+
setting (eg. 503 instead of 500)
397+
395398
.. http:get:: /__lbheartbeat__
396399
397400
The view that simply returns a successful HTTP response so that a load

docs/sanic.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,9 @@ spec:
372372
:statuscode 200: no error
373373
:statuscode 500: there was a warning or error
374374

375+
.. note:: Failed status code can be configured with the ``DOCKERFLOW_HEARTBEAT_FAILED_STATUS_CODE``
376+
setting (eg. 503 instead of 500)
377+
375378
.. http:get:: /__lbheartbeat__
376379
377380
The view that simply returns a successful HTTP response so that a load

src/dockerflow/django/views.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212

1313
from .signals import heartbeat_failed, heartbeat_passed
1414

15+
HEARTBEAT_FAILED_STATUS_CODE = int(
16+
getattr(settings, "DOCKERFLOW_HEARTBEAT_FAILED_STATUS_CODE", "500")
17+
)
18+
19+
1520
version_callback = getattr(
1621
settings, "DOCKERFLOW_VERSION_CALLBACK", "dockerflow.version.get_version"
1722
)
@@ -62,7 +67,7 @@ def heartbeat(request):
6267
status_code = 200
6368
heartbeat_passed.send(sender=heartbeat, level=check_results.level)
6469
else:
65-
status_code = 500
70+
status_code = HEARTBEAT_FAILED_STATUS_CODE
6671
heartbeat_failed.send(sender=heartbeat, level=check_results.level)
6772

6873
payload = {"status": checks.level_to_text(check_results.level)}

src/dockerflow/fastapi/views.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ def lbheartbeat():
1111
return {"status": "ok"}
1212

1313

14-
def heartbeat(response: Response):
14+
def heartbeat(request: Request, response: Response):
15+
FAILED_STATUS_CODE = int(
16+
getattr(request.app.state, "DOCKERFLOW_HEARTBEAT_FAILED_STATUS_CODE", "500")
17+
)
18+
1519
check_results = checks.run_checks(
1620
checks.get_checks().items(),
1721
)
@@ -25,7 +29,7 @@ def heartbeat(response: Response):
2529
if check_results.level < checks.ERROR:
2630
response.status_code = 200
2731
else:
28-
response.status_code = 500
32+
response.status_code = FAILED_STATUS_CODE
2933

3034
return payload
3135

src/dockerflow/flask/app.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,11 @@ def _heartbeat_view(self):
307307
Any check that returns a warning or worse (error, critical) will
308308
return a 500 response.
309309
"""
310+
FAILED_STATUS_CODE = int(
311+
flask.current_app.config.get(
312+
"DOCKERFLOW_HEARTBEAT_FAILED_STATUS_CODE", "500"
313+
)
314+
)
310315

311316
check_results = checks.run_checks(
312317
checks.get_checks().items(),
@@ -327,7 +332,7 @@ def render(status_code):
327332
heartbeat_passed.send(self, level=check_results.level)
328333
return render(status_code)
329334
else:
330-
status_code = 500
335+
status_code = FAILED_STATUS_CODE
331336
heartbeat_failed.send(self, level=check_results.level)
332337
raise HeartbeatFailure(response=render(status_code))
333338

src/dockerflow/sanic/app.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ async def _heartbeat_view(self, request):
209209
Any check that returns a warning or worse (error, critical) will
210210
return a 500 response.
211211
"""
212+
FAILED_STATUS_CODE = int(
213+
request.app.config.get("DOCKERFLOW_HEARTBEAT_FAILED_STATUS_CODE", "500")
214+
)
212215

213216
check_results = await checks.run_checks_async(
214217
checks.get_checks().items(),
@@ -224,7 +227,7 @@ async def _heartbeat_view(self, request):
224227
if check_results.level < checks.ERROR:
225228
status_code = 200
226229
else:
227-
status_code = 500
230+
status_code = FAILED_STATUS_CODE
228231

229232
return response.json(payload, status_code)
230233

0 commit comments

Comments
 (0)