Canonical log lines for Django applications using Gunicorn sync workers
Project description
Gunicorn Django Canonical Logs
gunicorn-django-canonical-logs provides extensible canonical log lines for Gunicorn/Django applications.
Table of Contents
Caveats
This is alpha software. It has not (yet!) been battle-tested and does several risky things worth highlighting:
- Overrides Django settings to include custom middleware to gather request/response context
- Modifies Django template rendering and database query execution to gather template exception/database query context
- Runs a separate timeout thread for every request to gather timeout context
- Leverages shared memory between the Gunicorn arbiter and workers to gather saturation context
- There's currently no cleanup and processes that receive
SIGKILLwill leak memory
- There's currently no cleanup and processes that receive
Installation
pip install gunicorn-django-canonical-logs
Usage
Add the following to your Gunicorn configuration file:
from gunicorn_django_canonical_logs.glogging import Logger
from gunicorn_django_canonical_logs.gunicorn_hooks import * # register Gunicorn hooks and instrumenters
accesslog = "-"
logger_class = Logger
NB Only
syncGunicorn worker types are supported
Overview
The goal is to enhance obersvability by providing reasonable defaults and extensibility to answer two questions:
- If a request was processed, what did it do?
- If a request timed out, what had it done and what was it doing?
A request will generate exactly one of these two event_types:
request- the worker process was able to successfully process the request and return a responsetimeout- the worker process timed out before returning a response- timeout events include a
timeout_loc/timeout_cause_loc
- timeout events include a
Example logs
Examples can be generated from the app used for integration testing:
cd tests/serverDJANGO_SETTINGS_MODULE=settings python app.py migrateDJANGO_SETTINGS_MODULE=settings gunicorn -c gunicorn_config.py app
And then, from another shell:
curl http://localhost:8080/db_queries/curl http://localhost:8080/rude_sleep/?duration=10
Request log
event_type=request req_method=GET req_path=/db_queries/ req_referrer= req_user_agent=curl/7.88.1 req_view=app.db_queries resp_time=0.026 resp_cpu_time=0.011 resp_status=200 db_queries=3 db_time=0.005 db_dup_queries=2 db_dup_time=0.001 g_w_count=1 g_w_active=0 g_backlog=0 app_key=val
Timeout log
event_type=timeout req_method=GET req_path=/rude_sleep/ req_referrer= req_user_agent=curl/7.88.1 resp_time=0.8 timeout_loc=app.py:73:rude_sleep timeout_cause_loc=app.py:93:simulate_blocking_and_ignoring_signals db_queries=0 db_time=0.000 db_dup_queries=0 db_dup_time=0.000 g_w_count=1 g_w_active=0 g_backlog=0 app_key=val
Default instrumenters
Request instrumenter
req_method(string) - HTTP method (e.g.GET/POST)req_path(string) - URL pathreq_referer(string) -ReferrerHTTP headerreq_user_agent(string) -User-AgentHTTP headerresp_time(float) - wall time spent processing the request (in seconds)resp_view(string) - Django view that generated the responseresp_cpu_time(float) - CPU time (i.e. ignoring sleep/wait) spent processing the request (in seconds)resp_status(int) - HTTP status code of the response
Exception instrumenter
exc_type(string) -typeof the exceptionexc_message(string) - exception messageexc_loc(string) -{module}:{line_number}:{name}of the top of the stack (i.e. the last place the exception could've been handled)exc_cause_loc(string) -{module}:{line_number}:{name}of the frame that threw the exceptionexc_template(string) -{template_name}:{line_number}(if raised during template rendering)
NB There's some subtlety in how
loc/cause_locwork; they attempt to provide application-relevant info by ignoring frames in library code if application frames are available.
Database instrumenter
db_queries(int) - total number of queries executeddb_time(float) - total time spent executing queries (in seconds)db_dup_queries(int) - total number of non-unique queries; could indicate N+1 issuesdb_dup_time(float) - total time spent executing non-unique queries (in seconds); could indicate N+1 issues
Saturation instrumenter
g_w_count(int) - total number of Gunicorn workersg_w_active(int) - number of active Gunicorn workersg_w_backlog(int) - number of queued requests
NB These values are sampled about once a second, and represent a snapshot. To derive useful data, average the values over time.
Default monitors
Saturation monitor
The saturation monitor samples and aggregates Gunicorn data; it provides data on the current number of active/idle workers as well as the number of queued requests that have not been assigned to a worker.
Timeout monitor
The timeout monitor wakes up slightly before the Gunicorn timeout in order to emit stack frame and instrumenter data before Gunicorn recycles the worker.
Extending gunicorn-django-canonical-logs
Application-specific context
from anywhere in your application, use
from gunicorn_django_canonical_logs import Context
Context.set("custom", "my_value")
This would add app_custom="my_value" to the log for the current request; context is cleared between requests.
Application-specific timing
from anywhere in your application, use
from gunicorn_django_canonical_logs import Context
Context.time("custom"):
do_thing_that_takes_time()
This would add app_custom_time="{wall time in seconds}" to the log for the current request based on the execution
time of do_thing_that_takes_time(); multiple timings using the same key are summed.
Custom instrumenters
from gunicorn_django_canonical_logs import Context, register_instrumenter
@register_instrumenter
class MyInstrumenter:
def setup(self):
pass # called once after forking a Gunicorn worker
def call(self):
pass # called every time an event is emitted
NB The application must import the instrumenter for it to register itself.
License
gunicorn-django-canonical-logs is distributed under the terms of the MIT license.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file gunicorn_django_canonical_logs-0.0.9.tar.gz.
File metadata
- Download URL: gunicorn_django_canonical_logs-0.0.9.tar.gz
- Upload date:
- Size: 19.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b67d4d6094b83ddddfdf0c1035041ef05dda89e62bb5128310b933ccabb6f062
|
|
| MD5 |
43a736b2fdc80db12b3cd14afb67a0a6
|
|
| BLAKE2b-256 |
bb54150c216dc371771678d5affdf48227bb1797e64fa1e77c70ea606b7a76dd
|
Provenance
The following attestation bundles were made for gunicorn_django_canonical_logs-0.0.9.tar.gz:
Publisher:
release.yml on bradshjg/gunicorn-django-canonical-logs
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
gunicorn_django_canonical_logs-0.0.9.tar.gz -
Subject digest:
b67d4d6094b83ddddfdf0c1035041ef05dda89e62bb5128310b933ccabb6f062 - Sigstore transparency entry: 497445284
- Sigstore integration time:
-
Permalink:
bradshjg/gunicorn-django-canonical-logs@8cc80ac1a1e3264e5e5793043236592b5591f4d9 -
Branch / Tag:
refs/tags/0.0.9 - Owner: https://github.com/bradshjg
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@8cc80ac1a1e3264e5e5793043236592b5591f4d9 -
Trigger Event:
push
-
Statement type:
File details
Details for the file gunicorn_django_canonical_logs-0.0.9-py3-none-any.whl.
File metadata
- Download URL: gunicorn_django_canonical_logs-0.0.9-py3-none-any.whl
- Upload date:
- Size: 19.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
93a3a73c11414c34aa78e48deac3590cfba87bdf26ef094b767dc1e82e5172c7
|
|
| MD5 |
10a18d7e31acbf6d413018bc8fb6e96b
|
|
| BLAKE2b-256 |
96bad54729defa5310edec42f3f309854d2d5caa319eb17b5f8fcdfae48eac59
|
Provenance
The following attestation bundles were made for gunicorn_django_canonical_logs-0.0.9-py3-none-any.whl:
Publisher:
release.yml on bradshjg/gunicorn-django-canonical-logs
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
gunicorn_django_canonical_logs-0.0.9-py3-none-any.whl -
Subject digest:
93a3a73c11414c34aa78e48deac3590cfba87bdf26ef094b767dc1e82e5172c7 - Sigstore transparency entry: 497445305
- Sigstore integration time:
-
Permalink:
bradshjg/gunicorn-django-canonical-logs@8cc80ac1a1e3264e5e5793043236592b5591f4d9 -
Branch / Tag:
refs/tags/0.0.9 - Owner: https://github.com/bradshjg
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@8cc80ac1a1e3264e5e5793043236592b5591f4d9 -
Trigger Event:
push
-
Statement type: