diff --git a/docs/django.rst b/docs/django.rst index 292310d..efc4b04 100644 --- a/docs/django.rst +++ b/docs/django.rst @@ -339,6 +339,9 @@ spec: :statuscode 200: no error :statuscode 500: there was a warning or error + .. note:: Failed status code can be configured with the ``DOCKERFLOW_HEARTBEAT_FAILED_STATUS_CODE`` + setting (eg. 503 instead of 500) + .. http:get:: /__lbheartbeat__ The view that simply returns a successful HTTP response so that a load diff --git a/docs/fastapi.rst b/docs/fastapi.rst index 9f15112..b0740e8 100644 --- a/docs/fastapi.rst +++ b/docs/fastapi.rst @@ -244,6 +244,9 @@ spec: :statuscode 200: no error :statuscode 500: there was an error + .. note:: Failed status code can be configured with the ``app.state.DOCKERFLOW_HEARTBEAT_FAILED_STATUS_CODE`` + attribute (eg. 503 instead of 500) + .. http:get:: /__lbheartbeat__ The view that simply returns a successful HTTP response so that a load diff --git a/docs/flask.rst b/docs/flask.rst index db11ea2..3ba66e8 100644 --- a/docs/flask.rst +++ b/docs/flask.rst @@ -392,6 +392,9 @@ spec: :statuscode 200: no error :statuscode 500: there was a warning or error + .. note:: Failed status code can be configured with the ``DOCKERFLOW_HEARTBEAT_FAILED_STATUS_CODE`` + setting (eg. 503 instead of 500) + .. http:get:: /__lbheartbeat__ The view that simply returns a successful HTTP response so that a load diff --git a/docs/sanic.rst b/docs/sanic.rst index 646bae6..a61af37 100644 --- a/docs/sanic.rst +++ b/docs/sanic.rst @@ -372,6 +372,9 @@ spec: :statuscode 200: no error :statuscode 500: there was a warning or error + .. note:: Failed status code can be configured with the ``DOCKERFLOW_HEARTBEAT_FAILED_STATUS_CODE`` + setting (eg. 503 instead of 500) + .. http:get:: /__lbheartbeat__ The view that simply returns a successful HTTP response so that a load diff --git a/src/dockerflow/django/views.py b/src/dockerflow/django/views.py index cc5d772..0760c68 100644 --- a/src/dockerflow/django/views.py +++ b/src/dockerflow/django/views.py @@ -12,6 +12,11 @@ from .signals import heartbeat_failed, heartbeat_passed +HEARTBEAT_FAILED_STATUS_CODE = int( + getattr(settings, "DOCKERFLOW_HEARTBEAT_FAILED_STATUS_CODE", "500") +) + + version_callback = getattr( settings, "DOCKERFLOW_VERSION_CALLBACK", "dockerflow.version.get_version" ) @@ -62,7 +67,7 @@ def heartbeat(request): status_code = 200 heartbeat_passed.send(sender=heartbeat, level=check_results.level) else: - status_code = 500 + status_code = HEARTBEAT_FAILED_STATUS_CODE heartbeat_failed.send(sender=heartbeat, level=check_results.level) payload = {"status": checks.level_to_text(check_results.level)} diff --git a/src/dockerflow/fastapi/views.py b/src/dockerflow/fastapi/views.py index 8aa1d59..f2dc1cd 100644 --- a/src/dockerflow/fastapi/views.py +++ b/src/dockerflow/fastapi/views.py @@ -11,7 +11,11 @@ def lbheartbeat(): return {"status": "ok"} -def heartbeat(response: Response): +def heartbeat(request: Request, response: Response): + FAILED_STATUS_CODE = int( + getattr(request.app.state, "DOCKERFLOW_HEARTBEAT_FAILED_STATUS_CODE", "500") + ) + check_results = checks.run_checks( checks.get_checks().items(), ) @@ -25,7 +29,7 @@ def heartbeat(response: Response): if check_results.level < checks.ERROR: response.status_code = 200 else: - response.status_code = 500 + response.status_code = FAILED_STATUS_CODE return payload diff --git a/src/dockerflow/flask/app.py b/src/dockerflow/flask/app.py index dd16a83..8ae9c02 100644 --- a/src/dockerflow/flask/app.py +++ b/src/dockerflow/flask/app.py @@ -304,6 +304,11 @@ def _heartbeat_view(self): Any check that returns a warning or worse (error, critical) will return a 500 response. """ + FAILED_STATUS_CODE = int( + flask.current_app.config.get( + "DOCKERFLOW_HEARTBEAT_FAILED_STATUS_CODE", "500" + ) + ) check_results = checks.run_checks( checks.get_checks().items(), @@ -324,7 +329,7 @@ def render(status_code): heartbeat_passed.send(self, level=check_results.level) return render(status_code) else: - status_code = 500 + status_code = FAILED_STATUS_CODE heartbeat_failed.send(self, level=check_results.level) raise HeartbeatFailure(response=render(status_code)) diff --git a/src/dockerflow/sanic/app.py b/src/dockerflow/sanic/app.py index cffc19e..12ab78e 100644 --- a/src/dockerflow/sanic/app.py +++ b/src/dockerflow/sanic/app.py @@ -205,6 +205,9 @@ async def _heartbeat_view(self, request): Any check that returns a warning or worse (error, critical) will return a 500 response. """ + FAILED_STATUS_CODE = int( + request.app.config.get("DOCKERFLOW_HEARTBEAT_FAILED_STATUS_CODE", "500") + ) check_results = await checks.run_checks_async( checks.get_checks().items(), @@ -220,7 +223,7 @@ async def _heartbeat_view(self, request): if check_results.level < checks.ERROR: status_code = 200 else: - status_code = 500 + status_code = FAILED_STATUS_CODE return response.json(payload, status_code)