Skip to main content

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 3.2+ on Python 3.6+.

Install

pip install django-alive

Configure

Add this to your project's urlpatterns:

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

If you wish to use the healthcheck management command, add django_alive to the INSTALLED_APPS.

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",
    },
    "django_alive.checks.check_migrations": {},
}

Built-in Checks

Defined in django_alive.checks.

def check_cache(key="django-alive", cache="default")

Fetch a cache key against the specified cache.

Parameters:

  • key (str): Cache key to fetch (does not need to exist)
  • cache (str): Cache alias to execute against

def check_database(query="SELECT 1", database="default")

Run a SQL query against the specified database.

Parameters:

  • query (str): SQL to execute
  • database (str): Database alias to execute against

def check_migrations(alias=None)

Verify all defined migrations have been applied

Parameters:

  • alias (str): An optional database alias (default: check all defined databases)

def check_staticfile(filename)

Verify a static file is reachable

Parameters:

  • filename (str): static file to verify

Management Command

In addition to the view, the configured healthchecks can also be run via a management command with manage.py healthcheck. This will exit with an error code if all the healthchecks do not pass.

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.2.3 (unreleased)

  • Nothing changed yet.

1.2.2 (2024-08-14)

  • Add GitHub Action for running tests against supported versions
  • Release from GitHub Actions

1.2.1 (2021-07-23)

  • Update PyPI metadata

1.2.0 (2021-07-23)

  • Updated test matrix. Python 2 no longer "officially" supported.
  • Prevent Traceback in middleware if URLs are not setup

1.1.0 (2019-11-06)

  • Added healthcheck management command
  • Added optional check_migrations healthcheck

1.0.1 (2018-09-10)

  • Documentation improvements
  • Python 3.7 support

1.0.0 (2018-08-21)

  • Initial release

Metadata

Release files for django-alive 1.2.2

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-alive 1.2.2
File Size Uploaded
django_alive-1.2.2.tar.gz 10.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-alive 1.2.2
File Interpreter ABI Platform
django_alive-1.2.2-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 22.0 kB

Release files / django_alive-1.2.2.tar.gz

Download URL django_alive-1.2.2.tar.gz
Size 10.5 kB
Tags Source
SHA-256 checksum
How to use checksums
00246da76b83737cb8fc3ebcc4fa4ed2af5de5446ec27a7325a836eff8c480c0
BLAKE2b-256 checksum
How to use checksums
ee1ebe3f31bc8d5f0d48ae2879047e6a88e97ee81faa670e11157c8bc8c8f981
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.0 CPython/3.12.5

Release files / django_alive-1.2.2-py2.py3-none-any.whl

Download URL django_alive-1.2.2-py2.py3-none-any.whl
Size 11.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
0805d724e5037b636af965488156e99da7fd883f94f41eb01c4d1701af13f85e
BLAKE2b-256 checksum
How to use checksums
2e5c54ea49f6df1ccdaf0c3b930da9f8b73965334557250de5b706d07220cc9c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.0 CPython/3.12.5

Release history Release notifications | RSS feed

2.0.0

2 release files

This release

1.2.2 This release

2 release files

1.2.1

2 release files

1.2.0

1 release file

1.1.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

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