Skip to content

Commit 0424734

Browse files
authored
Update Django docs (#109)
* Specify that the middleware serves the views Since the DockerflowMiddleware serves the health monitoring views, it is unneccessary to add them to urls.py * Expand DOCKERFLOW_VERSION_CALLBACK docs Add links to other documentation. Fix the signature of the callback function. Move so that the section is alpha-sorted. Fixup version callback * Add DOCKERFLOW_REQUEST_ID_HEADER_NAME docs * Add DOCKERFLOW_SUMMARY_LOG_QUERYSTRING docs * Add DEBUG docs * Add SILENCED_SYSTEM_CHECKS docs * Add DOCKERFLOW_HEARTBEAT_FAILED_STATUS_CODE docs * MIDDLEWARE_CLASSES was removed in Django 2.0, 2017 * Add dockerflow.checks.register to log config * Use dockerflow as logger name These logs should propagate up, so use more general logging level. * Fix name of RequestIdLogFilter * Add SECURE_REDIRECT_EXEMPT docs * More MIDDLEWARE_CLASSES removal * Less bear 🐻 * Fix name of RequestIdLogFilter
1 parent f3ff9c3 commit 0424734

File tree

5 files changed

+128
-31
lines changed

5 files changed

+128
-31
lines changed

docs/django.rst

Lines changed: 120 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ Django projects that want to follow the Dockerflow specs:
77
- A Python logging formatter following the `mozlog`_ format to be used in
88
the ``LOGGING`` setting.
99

10-
- A middleware to emit `request.summary`_ log records based on request specific
11-
data.
12-
1310
- Views for health monitoring:
1411

1512
- ``/__version__`` - Serves a ``version.json`` file
@@ -19,6 +16,8 @@ Django projects that want to follow the Dockerflow specs:
1916

2017
- ``/__lbheartbeat__`` - Retuns a HTTP 200 response
2118

19+
- A middleware to emit `request.summary`_ log records based on request specific
20+
data, and to serve the health monitoring views.
2221

2322
- Signals for passed and failed heartbeats.
2423

@@ -43,20 +42,32 @@ To install ``python-dockerflow``'s Django support please follow these steps:
4342

4443
.. seealso:: :ref:`django-versions` for more information
4544

46-
#. Add the ``DockerflowMiddleware`` to your ``MIDDLEWARE_CLASSES`` or
47-
``MIDDLEWARE`` setting::
45+
#. Add the ``DockerflowMiddleware`` to your ``MIDDLEWARE`` setting::
4846

49-
MIDDLEWARE_CLASSES = (
47+
MIDDLEWARE = [
5048
# ...
49+
# 'django.middleware.security.SecurityMiddleware',
5150
'dockerflow.django.middleware.DockerflowMiddleware',
5251
# ...
53-
)
52+
]
53+
54+
#. (Optional) Add the healthcheck views to SECURE_REDIRECT_EXEMPT_, so they can
55+
be used as `Kubernetes liveness checks`_::
56+
57+
SECURE_REDIRECT_EXEMPT = [
58+
r"^__version__/?$",
59+
r"^__heartbeat__/?$",
60+
r"^__lbheartbeat__/?$",
61+
]
5462

5563
#. :ref:`Configure logging <django-logging>` to use the
5664
:class:`~dockerflow.logging.JsonLogFormatter`
5765
logging formatter for the ``request.summary`` logger (you may have to
5866
extend your existing logging configuration!).
5967

68+
.. _`Kubernetes liveness checks`: https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/
69+
.. _SECURE_REDIRECT_EXEMPT: https://docs.djangoproject.com/en/stable/ref/settings/#secure-redirect-exempt
70+
6071
.. _django-config:
6172

6273
Configuration
@@ -255,6 +266,7 @@ Health monitoring
255266
Health monitoring happens via three different views following the Dockerflow_
256267
spec:
257268

269+
.. _http_get_version:
258270
.. http:get:: /__version__
259271
260272
The view that serves the :ref:`version information <django-versions>`.
@@ -284,16 +296,25 @@ spec:
284296
:statuscode 200: no error
285297
:statuscode 404: a version.json wasn't found
286298

299+
.. _http_get_heartbeat:
287300
.. http:get:: /__heartbeat__
288301
289302
The heartbeat view will go through the list of configured Dockerflow
290303
checks in the :ref:`DOCKERFLOW_CHECKS` setting, run each check, and, if
291-
`settings.DEBUG` is `True`, add their results to a JSON response.
304+
``settings.DEBUG`` is ``True``, add their results to a JSON response.
292305

293306
The view will return HTTP responses with either a status code of 200 if
294307
all checks ran successfully or 500 if there was one or more warnings or
295308
errors returned by the checks.
296309

310+
The check processes will log to ``dockerflow.checks.register``. Failed
311+
checks that cause the heartbeat to fail are logged at ``ERROR`` level
312+
or higher. Successful checks are logged at ``INFO`` level and higher.
313+
The check setup process is logged at the ``DEBUG`` level. Since failure
314+
details are omitted with ``DEBUG=False``, this logger should emit logs
315+
at ``WARNING`` or ``ERROR`` level in production, so that the logs can
316+
be used to diagnose heartbeat failures.
317+
297318
**Custom Dockerflow checks:**
298319

299320
To write your own custom Dockerflow checks, please follow the documentation
@@ -311,7 +332,7 @@ spec:
311332
GET /__heartbeat__ HTTP/1.1
312333
Host: example.com
313334

314-
**Example response**:
335+
**Example response, DEBUG=True**:
315336

316337
.. sourcecode:: http
317338

@@ -336,6 +357,18 @@ spec:
336357
}
337358
}
338359

360+
**Example response, DEBUG=False**:
361+
362+
.. sourcecode:: http
363+
364+
HTTP/1.1 500 Internal Server Error
365+
Vary: Accept-Encoding
366+
Content-Type: application/json
367+
368+
{
369+
"status": "warning"
370+
}
371+
339372
:statuscode 200: no error, with potential warnings
340373
:statuscode 500: there was an error
341374

@@ -388,7 +421,7 @@ configure **at least** the ``request.summary`` logger that way::
388421
},
389422
'filters': {
390423
'request_id': {
391-
'()': 'dockerflow.logging.RequestIdFilter',
424+
'()': 'dockerflow.logging.RequestIdLogFilter',
392425
},
393426
},
394427
'handlers': {
@@ -404,10 +437,15 @@ configure **at least** the ``request.summary`` logger that way::
404437
'handlers': ['console'],
405438
'level': 'DEBUG',
406439
},
440+
'dockerflow': {
441+
'handlers': ['console'],
442+
'level': 'WARNING',
443+
},
407444
}
408445
}
409446

410-
In order to include querystrings in the request summary log, set this flag in settings:
447+
In order to include querystrings in the request summary log, set
448+
:ref:`this flag <DOCKERFLOW_SUMMARY_LOG_QUERYSTRING>` in settings:
411449

412450
.. code-block:: python
413451
@@ -427,15 +465,17 @@ The *MozLog* formatter will output ``Fields`` application-specific fields. It ca
427465
extra={"phase": "started", "host": host, "port": port}
428466
)
429467
468+
.. _requests_correlation_id:
430469

431470
Requests Correlation ID
432471
~~~~~~~~~~~~~~~~~~~~~~~
433472

434473
A unique request ID is read from the ``X-Request-ID`` request header, and a UUID4 value is generated if unset.
435474

436-
Leveraging the ``RequestIdFilter`` in logging configuration as shown above will add a ``rid`` field into the ``Fields`` entry of all log messages.
475+
Leveraging the ``RequestIdLogFilter`` in logging configuration as shown above will add a ``rid`` field into the ``Fields`` entry of all log messages.
437476

438-
The header name to obtain the request ID can be customized in settings:
477+
The header name to obtain the request ID can be
478+
`customized in settings <DOCKERFLOW_REQUEST_ID_HEADER_NAME>`_:
439479

440480
.. code-block:: python
441481
@@ -460,9 +500,9 @@ in your Django projet:
460500

461501
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
462502

463-
#. Add the middleware to your ``MIDDLEWARE`` (or ``MIDDLEWARE_CLASSES``) setting::
503+
#. Add the middleware to your ``MIDDLEWARE`` setting::
464504

465-
MIDDLEWARE_CLASSES = [
505+
MIDDLEWARE = [
466506
# 'django.middleware.security.SecurityMiddleware',
467507
'whitenoise.middleware.WhiteNoiseMiddleware',
468508
# ...
@@ -491,24 +531,28 @@ the section about `Using WhiteNoise with Django`_ in its documentation.
491531
Settings
492532
--------
493533

494-
.. _DOCKERFLOW_VERSION_CALLBACK:
534+
``DEBUG``
535+
~~~~~~~~~
495536

496-
``DOCKERFLOW_VERSION_CALLBACK``
497-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
537+
The standard Django setting DEBUG_ is referenced by the
538+
:ref:`__heartbeat__<http_get_heartbeat>` view. If it is set to ``True``, then:
539+
540+
- Django's deployment checks are run. These are the additional checks ran by
541+
including the ``--deploy`` flag, such as ``python manage.py check --deploy``.
498542

499-
The dotted import path for the callable that
500-
returns the content to return under ``/__version__``.
543+
- The ``checks`` and ``details`` objects are omitted from the JSON response,
544+
leaving only the ``status`` of ``ok``, ``warning`` or ``error``.
501545

502-
Defaults to ``'dockerflow.version.get_version'`` which will be passed the
503-
``BASE_DIR`` setting by default.
546+
.. _DEBUG: https://docs.djangoproject.com/en/stable/ref/settings/#debug
504547

505548
.. _DOCKERFLOW_CHECKS:
506549

507550
``DOCKERFLOW_CHECKS``
508551
~~~~~~~~~~~~~~~~~~~~~
509552

510553
A list of dotted import paths to register during
511-
Django setup, to be used in the rendering of the ``/__heartbeat__`` view.
554+
Django setup, to be used in the rendering of the
555+
:ref:`__heartbeat__<http_get_heartbeat>` view.
512556
Defaults to:
513557

514558
.. code-block:: python
@@ -517,3 +561,56 @@ Defaults to:
517561
'dockerflow.django.checks.check_database_connected',
518562
'dockerflow.django.checks.check_migrations_applied',
519563
]
564+
565+
.. _DOCKERFLOW_HEARTBEAT_FAILED_STATUS_CODE:
566+
567+
``DOCKERFLOW_HEARTBEAT_FAILED_STATUS_CODE``
568+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
569+
570+
In the :ref:`__heartbeat__<http_get_heartbeat>` view, this setting
571+
is used to set the status code when a check fails at ``error`` or higher.
572+
If unset, the default is ``500`` for an Internal Server Error.
573+
574+
.. _DOCKERFLOW_REQUEST_ID_HEADER_NAME:
575+
576+
``DOCKERFLOW_REQUEST_ID_HEADER_NAME``
577+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
578+
579+
The case-insenstive name of the HTTP header referenced for identifying a
580+
request. The default is `X-Request-ID`, used by the
581+
`Heroku router <https://devcenter.heroku.com/articles/http-request-id#how-it-works>`_.
582+
If the header is not set by the incoming request, a UUID is generated
583+
for the :ref:`requests correlation ID<requests_correlation_id>`.
584+
585+
A good value is the header name used by your deployment infrastructure.
586+
For example, the Google Cloud Platform sets the W3C standard ``traceparent``
587+
header as well as a legacy ``X-Cloud-Trace-Context`` header for
588+
`trace context <cloud.google.com/trace/docs/trace-context>`_.
589+
590+
.. _DOCKERFLOW_SUMMARY_LOG_QUERYSTRING:
591+
592+
``DOCKERFLOW_SUMMARY_LOG_QUERYSTRING``
593+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
594+
595+
If set to ``True``, then the request summary log will include the querystring.
596+
This defaults to ``False``, in case there is user-sensitive information in
597+
some querystrings.
598+
599+
.. _DOCKERFLOW_VERSION_CALLBACK:
600+
601+
602+
``DOCKERFLOW_VERSION_CALLBACK``
603+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
604+
605+
The dotted import path for the callable that takes a
606+
`HttpRequest <https://docs.djangoproject.com/en/stable/ref/request-response/#httprequest-objects>`_
607+
and returns the :ref:`version content<django-versions>` to return under
608+
:ref:`__version__<http_get_version>`. This defaults to ``dockerflow.version.get_version``.
609+
610+
``SILENCED_SYSTEM_CHECKS``
611+
~~~~~~~~~~~~~~~~~~~~~~~~~~
612+
613+
The standard Django setting SILENCED_SYSTEM_CHECKS_ is used by the
614+
:ref:`__heartbeat__<http_get_heartbeat>` view to omit the named checks.
615+
616+
.. _SILENCED_SYSTEM_CHECKS: https://docs.djangoproject.com/en/stable/ref/settings/#silenced-system-checks

docs/fastapi.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ for at least the ``request.summary`` logger:
300300
},
301301
'filters': {
302302
'request_id': {
303-
'()': 'dockerflow.logging.RequestIdFilter',
303+
'()': 'dockerflow.logging.RequestIdLogFilter',
304304
},
305305
},
306306
'handlers': {
@@ -346,7 +346,7 @@ Requests Correlation ID
346346

347347
A unique request ID is read from the ``X-Request-ID`` request header, and a UUID4 value is generated if unset.
348348

349-
Leveraging the ``RequestIdFilter`` in logging configuration as shown above will add a ``rid`` field into the ``Fields`` entry of all log messages.
349+
Leveraging the ``RequestIdLogFilter`` in logging configuration as shown above will add a ``rid`` field into the ``Fields`` entry of all log messages.
350350

351351
The header name to obtain the request ID can be customized in settings:
352352

docs/flask.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ for at least the ``request.summary`` logger::
443443
},
444444
'filters': {
445445
'request_id': {
446-
'()': 'dockerflow.logging.RequestIdFilter',
446+
'()': 'dockerflow.logging.RequestIdLogFilter',
447447
},
448448
},
449449
'handlers': {
@@ -486,7 +486,7 @@ Requests Correlation ID
486486

487487
A unique request ID is read from the ``X-Request-ID`` request header, and a UUID4 value is generated if unset.
488488

489-
Leveraging the ``RequestIdFilter`` in logging configuration as shown above will add a ``rid`` field into the ``Fields`` entry of all log messages.
489+
Leveraging the ``RequestIdLogFilter`` in logging configuration as shown above will add a ``rid`` field into the ``Fields`` entry of all log messages.
490490

491491
The header name to obtain the request ID can be customized in settings:
492492

docs/logging.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ this::
4141
},
4242
'filters': {
4343
'request_id': {
44-
'()': 'dockerflow.logging.RequestIdFilter',
44+
'()': 'dockerflow.logging.RequestIdLogFilter',
4545
},
4646
},
4747
'handlers': {
@@ -90,7 +90,7 @@ thing as the dictionary based configuratio above:
9090
keys = request_id
9191
9292
[filter_request_id]
93-
class = dockerflow.logging.RequestIdFilter
93+
class = dockerflow.logging.RequestIdLogFilter
9494
9595
[logger_root]
9696
level = INFO

docs/sanic.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ for at least the ``request.summary`` logger::
423423
},
424424
'filters': {
425425
'request_id': {
426-
'()': 'dockerflow.logging.RequestIdFilter',
426+
'()': 'dockerflow.logging.RequestIdLogFilter',
427427
},
428428
},
429429
'handlers': {
@@ -485,7 +485,7 @@ Requests Correlation ID
485485

486486
A unique request ID is read from the ``X-Request-ID`` request header, and a UUID4 value is generated if unset.
487487

488-
Leveraging the ``RequestIdFilter`` in logging configuration as shown above will add a ``rid`` field into the ``Fields`` entry of all log messages.
488+
Leveraging the ``RequestIdLogFilter`` in logging configuration as shown above will add a ``rid`` field into the ``Fields`` entry of all log messages.
489489

490490
The header name to obtain the request ID can be customized in settings:
491491

0 commit comments

Comments
 (0)