Skip to main content

Healtchecks for Django

Project description

django-alive 🕺

tests coverage PyPI Python Versions

Provides two healthcheck endpoints for your Django application:

Alive

Verifies the WSGI server is responding.

  • Default URL: /-/alive/
  • Success:
    • status code: 200
    • content: ok
  • Failure: This view never returns a failure. A failure would mean your WSGI server is not running.

Health

Verifies services are ready.

  • Default URL: /-/health/
  • Success:
    • status_code: 200
    • content: {"healthy": true}
  • Failure:
    • status_code: 503
    • content: {"healthy": false, "errors": ["error 1", "error 2"]}

By default the health endpoint will test the database connection, but can be configured to check the cache, staticfiles, or any additional custom checks.

Supports Django 1.10+ on both Python 2 & 3.

Install

pip install django-alive

Configure

Add this to your project's urlpatterns:

path("-/", include("django_alive.urls"))

For versions before Django 2.0, use:

url(r"-/", include("django_alive.urls"))

Enabling Checks

The default "health" endpoint will test a simple SELECT 1 query on the database. Additional checks can be enabled in your Django settings.

Use the ALIVE_CHECKS setting to configure the checks to include. It is a dictionary with the path to a Python function as a key and any keyword arguments to pass to that function as a value. A full example:

ALIVE_CHECKS = {
    "django_alive.checks.check_database": {},
    "django_alive.checks.check_staticfile": {
        "filename": "img/favicon.ico",
    },
    "django_alive.checks.check_cache": {
        "cache": "session",
        "key": "test123",
    },
}

Custom Checks

django-alive is designed to easily extend with your own custom checks. Simply define a function which performs your check and raises a django_alive.HealthcheckFailure exception in the event of a failure. See checks.py for some examples on how to write a check.

Disabling ALLOWED_HOSTS for Healthchecks

Often, load balancers will not pass a Host header when probing a healthcheck endpoint. This presents a problem for Django's host header validation. A middleware is included that will turn off the host checking only for the healthcheck endpoints. This is safe since these views never do anything with the Host header.

Enable the middleware by inserting this at the beginning of your MIDDLEWARE:

MIDDLEWARE = [
    "django_alive.middleware.healthcheck_bypass_host_check",
    # ...
]

Handling SECURE_SSL_REDIRECT

If your load balancer is doing HTTPS termination and you have SECURE_SSL_REDIRECT=True in your settings, you want to make sure that your healtcheck URLs are not also redirected to HTTPS. In that case, add the following to your settings:

SECURE_REDIRECT_EXEMPT = [r"^-/"]  # django-alive URLs

1.0.1 (2018-09-10)

  • Documentation improvements
  • Python 3.7 support

1.0.0 (2018-08-21)

  • Initial release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-alive-1.0.1.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

django_alive-1.0.1-py2.py3-none-any.whl (7.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-alive-1.0.1.tar.gz.

File metadata

  • Download URL: django-alive-1.0.1.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.7.0

File hashes

Hashes for django-alive-1.0.1.tar.gz
Algorithm Hash digest
SHA256 38627f71d08fa096fbb7a95932af16af3996681147d2af29af0acbe94bc71288
MD5 2c60fbbd73ada08c97c03f970d44b9d2
BLAKE2b-256 d854dc452b28b2d422dec848ad7163c9a6040c7a7121db17590f5e4f25e6be36

See more details on using hashes here.

Provenance

File details

Details for the file django_alive-1.0.1-py2.py3-none-any.whl.

File metadata

  • Download URL: django_alive-1.0.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.7.0

File hashes

Hashes for django_alive-1.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 736101cb61079390af6fcef6541ccd2dab2d9cbf7a63cf50fec9d81c2c879152
MD5 0742d104be61d144a5bf549aaed44177
BLAKE2b-256 8ed3ab93e51c502ecf68fdd5a295727b39cb4ca832e780342b77dd061779e7f4

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page