Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable status for failed heartbeat #98

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/django.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions docs/fastapi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions docs/flask.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions docs/sanic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/dockerflow/django/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)}
Expand Down
8 changes: 6 additions & 2 deletions src/dockerflow/fastapi/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)
Expand All @@ -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

Expand Down
7 changes: 6 additions & 1 deletion src/dockerflow/flask/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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))

Expand Down
5 changes: 4 additions & 1 deletion src/dockerflow/sanic/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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)

Expand Down
Loading