Skip to main content

version ci coverage health license

This project checks the health for a number of backends and sees if they are able to connect and do a simple action.

The following health check backends are bundled into this project:

  • Cache

  • Database

  • Storage

  • AWS S3 storage

  • Celery task queue

Writing your own custom health checks is also very quick and easy.

We also like contributions, so don’t be afraid to make a pull request.

Installation

First install the django-health-check package:

pip install django-health-check

Add the health checker to an URL you want to use:

urlpatterns = [
    # ...
    url(r'^ht/', include('health_check.urls')),
]

Add the health_check applications to your INSTALLED_APPS:

INSTALLED_APPS = [
    # ...
    'health_check',                             # required
    'health_check.db',                          # stock Django health checkers
    'health_check.cache',
    'health_check.storage',
    'health_check.contrib.celery',              # requires celery
    'health_check.contrib.s3boto_storage',      # requires boto and S3BotoStorage backend
]

If using the DB check, run migrations:

django-admin migrate

Setting up monitoring

You can use tools like Pingdom or other uptime robots to monitor service status. The /ht/ endpoint will respond a HTTP 200 if all checks passed and a HTTP 500 if any of the tests failed.

$ curl -v -X GET -H http://www.example.com/ht/

> GET /ht/ HTTP/1.1
> Host: www.example.com
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: text/html; charset=utf-8

<!-- This is an excerpt -->
<div class="container">
    <h1>System status</h1>
    <table>
        <tr>
            <td class="status_1"></td>
            <td>CacheBackend</td>
            <td>working</td>
        </tr>
        <tr>
            <td class="status_1"></td>
            <td>DatabaseBackend</td>
            <td>working</td>
        </tr>
        <tr>
            <td class="status_1"></td>
            <td>S3BotoStorageHealthCheck</td>
            <td>working</td>
        </tr>
    </table>
</div>

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.

The backend will return a JSON response:

$ 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"
}

Writing a custom health check

Writing a health check is quick and easy:

from health_check.backends import BaseHealthCheckBackend

class MyHealthCheckBackend(BaseHealthCheckBackend):
    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:

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)

Make sure the application you write the checker into is registered in your INSTALLED_APPS.

Customizing output

You can customize HTML or JSON rendering by inheriting from MainView in health_check.views and customizing the template_name, get, render_to_response and render_to_response_json properties:

# 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 = []
        # ...
        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
        )

# urls.py
import views

urlpatterns = [
    # ...
    url(r'^ht/$', views.HealthCheckCustomView.as_view(), name='health_check_custom'),
]

Other resources

  • django-watchman is a package that does some of the same things in a slightly different way.

  • See this weblog about configuring Django and health checking with AWS Elastic Load Balancer.

Metadata

Release files for django-health-check 3.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-health-check 3.3.0
File Size Uploaded
django-health-check-3.3.0.tar.gz 18.2 kB Details

Release files / django-health-check-3.3.0.tar.gz

Download URL django-health-check-3.3.0.tar.gz
Size 18.2 kB
Tags Source
SHA-256 checksum
How to use checksums
da3b7ffdd64b69ffd96b582c32ca7eb16274ceb99d3f5de9862c78d45914975e
BLAKE2b-256 checksum
How to use checksums
8b363dbf2626523c274929da3077a81ce38111aada26c3bb2ba9465d1ed951ac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

4.8.0

2 release files

4.7.2

2 release files

4.7.1

2 release files

4.7.0

2 release files

4.6.1

2 release files

4.6.0

2 release files

4.5.1

2 release files

4.5.0

2 release files

4.4.4

2 release files

4.4.3

2 release files

4.4.2

2 release files

4.4.1

2 release files

4.4.0

2 release files

4.3.1

2 release files

4.3.0

2 release files

4.2.2

2 release files

4.2.1

2 release files

4.2.0

2 release files

4.1.2

2 release files

4.1.1

2 release files

4.1.0

2 release files

4.0.6

2 release files

4.0.5

2 release files

4.0.4

2 release files

4.0.3

2 release files

4.0.2

2 release files

4.0.1

2 release files

4.0.0

2 release files

3.24.0

2 release files

3.21.0

2 release files

3.20.8

2 release files

3.20.7

2 release files

3.20.6

2 release files

3.20.5

2 release files

3.20.4

2 release files

3.20.3

2 release files

3.20.2

2 release files

3.20.1

2 release files

3.20.0

2 release files

3.19.2

2 release files

3.18.3

2 release files

3.18.1

2 release files

3.18.0

2 release files

3.16.4

2 release files

3.16.3

2 release files

3.14.3

2 release files

3.14.1

2 release files

3.14.0

2 release files

3.13.3

2 release files

3.12.3

2 release files

3.12.2

2 release files

3.12.1

2 release files

3.12.0

2 release files

3.11.1

2 release files

3.10.2

2 release files

3.10.1

2 release files

3.10.0

2 release files

3.9.0

1 release file

3.8.0

1 release file

3.7.1

1 release file

3.7.0

1 release file

3.6.1

1 release file

3.6.0

1 release file

3.5.1

1 release file

3.5.0

1 release file

3.4.3

1 release file

3.4.2

1 release file

3.4.1

1 release file

3.4.0

1 release file

This release

3.3.0 This release

1 release file

3.2.0

1 release file

3.1.0

1 release file

3.0.0

1 release file

2.4.0

1 release file

2.3.0

1 release file

2.2.3

1 release file

2.2.2

1 release file

2.2.1

1 release file

2.2.0

1 release file

2.1.1

1 release file

2.1.0

1 release file

2.0.0

1 release file

1.3

1 release file

1.2.1

1 release file

1.2

1 release file

1.1.6

1 release file

1.1.5

1 release file

1.1.4

1 release file

1.1.3

1 release file

1.1.2

1 release file

1.1.1

2 release files

1.1

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0

1 release file

0.3

1 release file

0.2

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page