Skip to content

Commit

Permalink
completed the external guides docs that include external monitoring, …
Browse files Browse the repository at this point in the history
…custom output and checks and django command
  • Loading branch information
VeldaKiara committed Nov 30, 2023
1 parent bf0353e commit bdc5789
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/src/components/Layout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const navigation = [
{
title: 'Advanced guides',
links: [
{ title: 'Writing sustom checks', href: '/writing-your-own-checks' },
{ title: 'Writing custom checks', href: '/writing-your-own-checks' },
{ title: 'Customizing the output', href: '/customize-output' },
{ title: 'Management Command', href: '/management-command' },
{ title: 'External Monitoring', href: '/monitoring' },
Expand Down
8 changes: 7 additions & 1 deletion docs/src/pages/checks/db.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,11 @@ INSTALLED_APPS = [

4.Run migrations
```shell
$ python manage.py migrate
python manage.py migrate
```

5.Run the health check
```shell
python manage.py health_check

```
46 changes: 46 additions & 0 deletions docs/src/pages/customize-output.md
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'),
]
```
20 changes: 20 additions & 0 deletions docs/src/pages/management-command.md
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.
50 changes: 50 additions & 0 deletions docs/src/pages/monitoring.md
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"
}
```

54 changes: 51 additions & 3 deletions docs/src/pages/writing-your-own-checks.md
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.

0 comments on commit bdc5789

Please sign in to comment.