-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
completed the external guides docs that include external monitoring, …
…custom output and checks and django command
- Loading branch information
1 parent
bf0353e
commit bdc5789
Showing
6 changed files
with
175 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
--- | ||
title: Custom django-health-check output | ||
pageTitle: Writing custom django-health-check output | ||
description: How to write your own health checks output. | ||
--- | ||
Customizing your output comes in handy when you need to match the needs your | ||
application or integrate with specific frontend tools and monitoring tools | ||
|
||
You can customize your output (HTML or JSON) rendering by inheriting from MainView in | ||
`health_check.views` and customize the `template_name`,` get`, `render_to_response `and | ||
`render_to_response_json` properties: | ||
|
||
```python | ||
# views.py | ||
from health_check.views import MainView | ||
|
||
class HealthCheckCustomView(MainView): | ||
template_name = 'myapp/health_check_dashboard.html' # customize the used templates | ||
|
||
def get(self, request, *args, **kwargs): | ||
plugins = [] | ||
status = 200 # needs to be filled status you need | ||
# ... | ||
if 'application/json' in request.META.get('HTTP_ACCEPT', ''): | ||
return self.render_to_response_json(plugins, status) | ||
return self.render_to_response(plugins, status) | ||
|
||
def render_to_response(self, plugins, status): # customize HTML output | ||
return HttpResponse('COOL' if status == 200 else 'SWEATY', status=status) | ||
|
||
def render_to_response_json(self, plugins, status): # customize JSON output | ||
return JsonResponse( | ||
{str(p.identifier()): 'COOL' if status == 200 else 'SWEATY' for p in plugins}, | ||
status=status | ||
) | ||
``` | ||
|
||
```python | ||
# urls.py | ||
import views | ||
|
||
urlpatterns = [ | ||
# ... | ||
url(r'^ht/$', views.HealthCheckCustomView.as_view(), name='health_check_custom'), | ||
] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--- | ||
title: Management Command | ||
pageTitle: Management command | ||
description: How to run your health checks | ||
--- | ||
|
||
You can run the Django command `health_check` to perform your health checks via the command line, | ||
or periodically with a cron, as follow: | ||
|
||
```shell | ||
python manage.py health_check | ||
``` | ||
|
||
This should yield the following output: | ||
|
||
```shell | ||
DatabaseHealthCheck ... working | ||
CustomHealthCheck ... unavailable: Something went wrong! | ||
``` | ||
A critical error will cause the command to quit with the exit code 1. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
--- | ||
title: External Monitoring | ||
pageTitle: setting up monitoring | ||
description: How to setup external monitoring | ||
--- | ||
|
||
You can use tools like [Pingdom](https://www.solarwinds.com/pingdom), [StatusCake](https://www.statuscake.com/) | ||
or other uptime robots to monitor service status. | ||
|
||
The `/ht/` endpoint will respond with an `HTTP 200` if all checks passed and with an `HTTP 500` | ||
if any of the tests failed. | ||
|
||
## Getting machine-readable JSON reports | ||
|
||
If you want machine-readable status reports you can request the `/ht/ `endpoint with the Accept | ||
HTTP header set to` application/json` or` pass format=json` as a query parameter. | ||
|
||
The backend will return a JSON response: | ||
|
||
```python | ||
$ curl -v -X GET -H "Accept: application/json" http://www.example.com/ht/ | ||
|
||
> GET /ht/ HTTP/1.1 | ||
> Host: www.example.com | ||
> Accept: application/json | ||
> | ||
< HTTP/1.1 200 OK | ||
< Content-Type: application/json | ||
|
||
{ | ||
"CacheBackend": "working", | ||
"DatabaseBackend": "working", | ||
"S3BotoStorageHealthCheck": "working" | ||
} | ||
|
||
$ curl -v -X GET http://www.example.com/ht/?format=json | ||
|
||
> GET /ht/?format=json HTTP/1.1 | ||
> Host: www.example.com | ||
> | ||
< HTTP/1.1 200 OK | ||
< Content-Type: application/json | ||
|
||
{ | ||
"CacheBackend": "working", | ||
"DatabaseBackend": "working", | ||
"S3BotoStorageHealthCheck": "working" | ||
} | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,53 @@ | ||
--- | ||
title: Writing your own custom django-health-check checks | ||
pageTitle: Writing your own custom django-health-check checks | ||
title: Writing custom health checks | ||
pageTitle: Writing custom health checks | ||
description: How to write your own health checks to specifically tests aspects of your system when there isn't a standard or contrib health check already available. | ||
--- | ||
--- | ||
|
||
Custom Django health checks is necessary for implementing application-specific checks, | ||
monitoring external dependencies, and validating legacy system integrations. | ||
|
||
## Template to write custom health checks | ||
|
||
Start with this code | ||
|
||
```python | ||
|
||
from health_check.backends import BaseHealthCheckBackend | ||
|
||
class MyHealthCheckBackend(BaseHealthCheckBackend): | ||
#: The status endpoints will respond with a 200 status code | ||
#: even if the check errors. | ||
critical_service = False | ||
|
||
def check_status(self): | ||
# The test code goes here. | ||
# You can use `self.add_error` or | ||
# raise a `HealthCheckException`, | ||
# similar to Django's form validation. | ||
pass | ||
|
||
def identifier(self): | ||
return self.__class__.__name__ # Display name on the endpoint. | ||
``` | ||
|
||
After writing a custom checker, register it in your app configuration: | ||
|
||
```python | ||
from django.apps import AppConfig | ||
|
||
from health_check.plugins import plugin_dir | ||
|
||
class MyAppConfig(AppConfig): | ||
name = 'my_app' | ||
|
||
def ready(self): | ||
from .backends import MyHealthCheckBackend | ||
plugin_dir.register(MyHealthCheckBackend) | ||
|
||
``` | ||
|
||
***Note:*** | ||
The application you write the checker into should already be registered in your `INSTALLED_APPS`. | ||
|
||
Remember to adapt the code based on your specific application requirements and the type of health check you want to perform. |