1
Tutorial Part 2: Auto-generating conn-check config for a Django app
2
===================================================================
7
Let's assume that you've actually created the ``Hello World`` service from
8
:doc:`part 1 </tutorial-part-1>` as a
9
`Django app <https://www.djangoproject.com/>`_, and you think to yourself:
11
*"Hang on, aren't all these connections I want conn-check to check for me
12
already defined in my Django settings module?"*
17
Yes, yes they are, and with the handy-dandy
18
`conn-check-configs <https://pypi.python.org/pypi/conn-check-configs>`_
19
package you can automatically generate conn-check config YAML from a range of
20
standard Django settings values (in theory from other environments
21
too, such as `Juju <https://jujucharms.com/>`_, but for now just Django).
26
Given the following ``settings.py`` in our *HWaaS* app:
28
.. code-block:: python
35
'ENGINE': 'django.db.backends.postgresql_psycopg2',
36
'HOST': 'gibson.hwass.internal',
37
'NAME': 'hwaas_production',
38
'PASSWORD': '123456asdf',
44
'LOCATION': 'freeside.hwaas.internal:11211',
45
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
48
PROXY_HOST = 'countzero.hwaas.internal'
50
TRANSLATE_API_KEY = 'BLAH'
52
We can create a ``settings-to-conn-check.py`` script with the least possible
55
.. code-block:: python
58
from conn_check_configs.django import run
60
if __name__ == '__main__':
63
This will output *postgresql* and *memcached* checks to similar our
68
$ chmod +x settings-to-conn-check.py
69
$ ./settings-to-conn-check.py -f cc-config.yaml -m hwaas.settings
75
database: hwaas_production
76
host: gibson.hwaas.internal
81
host: freeside.hwaas.internal
84
Customising generated checks
85
----------------------------
87
In order to generate the checks we need for Squid / Google Translate API, we
88
can add some custom callbacks:
90
.. code-block:: python
93
from conn_check_configs.django import run, EXTRA_CHECK_MAKERS
96
def make_proxied_translate_check(settings, options):
98
if settings['PROXY_HOST']:
101
'url': 'https://www.googleapis.com/language/translate/v2?q='
102
'Hello%20World&target=de&source=en&key={}'.format(
103
settings['TRANSLATE_API_KEY']),
104
'proxy_host': settings['PROXY_HOST'],
105
'proxy_port': int(settings.get('PROXY_PORT', 8080)),
106
'expected_code': 200,
110
EXTRA_CHECK_MAKERS.append(make_proxied_translate_check)
113
if __name__ == '__main__':
117
In the above we define a callable which takes 2 params, ``settings`` which
118
is a wrapper around the Django settings module, and ``options`` which is
119
an object containing the command line arguments that were passed to the script.
121
The ``settings`` module is not the direct settings module but a dict-like
122
wrapper so that you can access the settings just a like a dict (using indices,
123
``.get`` method, etc.)
125
To ensure ``make_proxied_translate_check`` is collected and called by the main
126
``run`` function we add it to the ``EXTRA_CHECK_MAKERS`` list.
128
The above generates our required HTTP check:
133
url: https://www.googleapis.com/language/translate/v2?q=Hello%20World&target=de&source=en&key=BLAH
134
proxy_host: countzero.hwaas.internal
138
A note on statstd checks
139
------------------------
141
Getting more operational visibility on how *HWaaS* runs would be great,
144
So let's add some metrics collection using
145
`StatsD <https://github.com/etsy/statsd>`_, and as luck would have it we can
146
get a lot for *nearly free* with the
147
`django-statsd <https://django-statsd.readthedocs.org/>`_, after adding it to
148
our dependencies we update our ``settings.py`` to include:
150
.. code-block:: python
156
MIDDLEWARE_CLASSES = [
157
'django_statsd.middleware.GraphiteMiddleware',
159
STATSD_CLIENT = 'django_statsd.clients.normal'
160
STATSD_HOST = 'bigend.hwaas.internal'
163
**Note**: You don't actually need the django-statsd app to have
164
conn-check-configs generate statsd checks, only the use of ``STATSD_HOST``
165
and ``STATSD_PORT`` in your settings module matters.
167
Another run of our ``settings-to-conn-check.py`` script will result in the
173
host: bigend.hwaas.internal
175
send: conncheck.test:1|c
178
As you can see this is just a generic UDP check that attempts to send an
179
incremental counter metric to the statsd host.
181
Unfortunately the fire-and-forget nature of this use of statsd/UDP will not
182
error in a number of common situations (the simplest being that statsd is not
183
running on the target host, or even a routing issue along the way).
185
It will catch simple problems such as not being able to open up the local UDP
186
port to send from, but that's usually not enough.
188
If you use a third-party implementation of statsd, such as
189
`txStatsD <https://launchpad.net/txstatsd>`_ then you might have the ability
190
to define a pair of health check strings, for example by changing the send
191
and expect values in the ``STATSD_CHECK`` dict we can send and expect different
194
.. code-block:: python
196
#!/usr/bin/env python
197
from conn_check_configs.django import run, STATSD_CHECK
199
STATSD_CHECK['send'] = 'Hakuna'
200
STATSD_CHECK['expect'] = 'Matata'
202
if __name__ == '__main__':
205
Which generates this check:
210
host: bigend.hwaas.internal
215
In the above we would configure our txStatD (for example) instance to respond
216
to the string ``Hakuna`` with the string ``Matata``, which would catch pretty
217
much all the possible issues with contacting our metrics service.