Skip to content

mythmon/python-dockerflow

 
 

Repository files navigation

Python Dockerflow

This package implements a few helpers and tools for Mozilla's Dockerflow pattern.

Build Status Codecov

Installation

Please install the package using your favorite package installer:

pip install dockerflow

Configuration

This package implements various tools to implement the Dockerflow requirements:

  • Provide views for health monitoring:
    • /__version__ - Serves a version.json file
    • /__heartbeat__ - Run Django checks as configured in the DOCKERFLOW_CHECKS setting
    • /__lbheartbeat__ - Retuns a HTTP 200 response

Django

To install python-dockerflow's Django support please follow these steps:

  1. Add dockerflow.django to your INSTALLED_APPS setting

  2. Add the URL patterns to your project's URL patterns:

    urlpatterns = [
        url(r'^', include('dockerflow.django.urls', namespace='dockerflow')),
        # ...
    ]
    
  3. Define a BASE_DIR setting that is the root path of your Django project. This will be used to locate the version.json file that is generated by CircleCI or another process during deployment.

Settings

DOCKERFLOW_VERSION_CALLBACK - The dotted import path for the callable that returns the content to return under /__version__. Defaults to dockerflow.version.get_version which will be passed the BASE_DIR setting by default.

DOCKERFLOW_CHECKS - A list of dotted import paths to register during Django setup, to be used in the rendering of the /__heartbeat__ view. Defaults to:

[
    'dockerflow.django.checks.check_database_connected',
    'dockerflow.django.checks.check_migrations_applied',
]

Signals

During the rendering of the /__heartbeat__ view two signals are being sent to hook into the result of the checks:

  • dockerflow.django.signals.heartbeat_passed - Sent when the heartbeat checks passed successfully.
  • dockerflow.django.signals.heartbeat_failed - Sent when the heartbeat checks raised either a warning or worse (error, critical).

Both signals receive an additional level parameter that indicates the maximum check level that failed during the rendering.

E.g. to hook into those signals to send data to statsd, do this:

from django.dispatch import receiver
from dockerflow.django.signals import heartbeat_passed, heartbeat_failed
from statsd.defaults.django import statsd

@receiver(heartbeat_passed)
def heartbeat_passed_handler(sender, level, **kwargs):
    statsd.incr('heartbeat.pass')

@receiver(heartbeat_failed)
def heartbeat_failed_handler(sender, level, **kwargs):
    statsd.incr('heartbeat.fail')

TODO

  • Port mozilla-cloud-services-logger to dockerflow.logging and dockerflow.django.middleware mozilla-cloud-services-logger
  • Document how to log to stdout and stderr
  • Document how to use Whitenoise to serve static content
  • Ship Dockerfile files for Python 2 and 3
  • Document how to use python-decouple or django-configurations to read environment variables for configuration values

Changelog

2016.11.0 (unreleased)

  • Added initial implementation for Django health checks based on Normandy and ATMO code. Many thanks to Mike Cooper for inspiration and majority of implementation.

About

A Python package to implement tools and helpers for Mozilla Dockerflow

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Python 100.0%