Skip to content

Commit 93d6d97

Browse files
committed
docs: Create markdown readme
Contains rewrites
1 parent 4d6fc81 commit 93d6d97

File tree

1 file changed

+205
-0
lines changed

1 file changed

+205
-0
lines changed

README.md

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
# Django GUID
2+
3+
[![package version](https://img.shields.io/pypi/v/django-guid.svg)](https://pypi.org/pypi/django-guid)
4+
[![codecov](https://codecov.io/gh/snok/django-guid/branch/master/graph/badge.svg)](https://codecov.io/gh/snok/django-guid)
5+
[![downloads](https://img.shields.io/badge/python-3.7+-blue.svg)](https://pypi.python.org/pypi/django-guid#downloads)
6+
[![django versions](https://img.shields.io/pypi/djversions/django-guid?color=0C4B33&logo=django&logoColor=white&label=django)](https://pypi.python.org/pypi/django-guid)
7+
[![asgi](https://img.shields.io/badge/ASGI-supported-brightgreen.svg)](https://img.shields.io/badge/ASGI-supported-brightgreen.svg)
8+
[![wsgi](https://img.shields.io/badge/WSGI-supported-brightgreen.svg)](https://img.shields.io/badge/WSGI-supported-brightgreen.svg)
9+
10+
11+
The middleware adds an ID to your logs that is unique to each incoming request. [Correlation IDs](https://en.wikipedia.org/wiki/List_of_HTTP_header_fields?highlight=x-request-id#:~:text=Csrf%2DToken%3A%20i8XNjC4b8KVok4uw5RftR38Wgp2BFwql-,X%2DRequest%2DID,-%2C%5Bstackoverflow2%201)
12+
(also knows as request IDs) make it easy to correlate logs from a single HTTP request, and makes debugging simple.
13+
14+
Django GUID also includes ways of extending correlation IDs to Celery workers and Sentry issues.
15+
16+
For the purposes of this package, a GUID (globally unique identifier) is equivalent
17+
to a UUID (universally unique identifier).
18+
19+
## Examples
20+
21+
Let's assume we have three outgoing requests happening at the same time across our application instances:
22+
23+
```
24+
INFO project.views Fetching resource
25+
INFO project.views Fetching resource
26+
INFO project.views Fetching resource
27+
INFO project.services Finished successfully
28+
INFO project.services Finished successfully
29+
ERROR project.services Something went wrong!
30+
```
31+
32+
Without a correlation-id we have no way of knowing which logs belong to which request.
33+
34+
Using a log filter, we can do a little better:
35+
36+
```
37+
INFO [773fa6885e03493498077a273d1b7f2d] project.views Fetching resource
38+
INFO [0d1c3919e46e4cd2b2f4ac9a187a8ea1] project.views Fetching resource
39+
INFO [99d44111e9174c5a9494275aa7f28858] project.views Fetching resource
40+
INFO [99d44111e9174c5a9494275aa7f28858] project.services Finished successfully
41+
INFO [773fa6885e03493498077a273d1b7f2d] project.services Finished successfully
42+
ERROR [0d1c3919e46e4cd2b2f4ac9a187a8ea1] project.services Something went wrong!
43+
```
44+
45+
With the filter, we now know which logs belong to which request and can start debugging.
46+
47+
## Installation
48+
49+
```shell
50+
pip install django-guid
51+
```
52+
53+
## Settings
54+
55+
Package settings are added in your `settings.py`:
56+
57+
```python
58+
DJANGO_GUID = {
59+
'GUID_HEADER_NAME': 'X-Request-ID',
60+
'VALIDATE_GUID': True,
61+
'RETURN_HEADER': True,
62+
'EXPOSE_HEADER': True,
63+
'INTEGRATIONS': [],
64+
'IGNORE_URLS': [],
65+
'UUID_LENGTH': 32,
66+
}
67+
```
68+
69+
70+
**Optional Parameters**
71+
72+
* `GUID_HEADER_NAME`
73+
74+
> The name of the GUID to look for in a header in an incoming request. Remember that it's case insensitive.
75+
76+
Default: `Correlation-ID`
77+
78+
* `VALIDATE_GUID`
79+
> Whether the `GUID_HEADER_NAME` should be validated or not.
80+
If the GUID sent to through the header is not a valid GUID (`uuid.uuid4`).
81+
82+
Default: `True`
83+
84+
* `RETURN_HEADER`
85+
> Whether to return the GUID (Correlation-ID) as a header in the response or not.
86+
It will have the same name as the `GUID_HEADER_NAME` setting.
87+
88+
Default: `True`
89+
90+
* `EXPOSE_HEADER`
91+
> Whether to return `Access-Control-Expose-Headers` for the GUID header if
92+
`RETURN_HEADER` is `True`, has no effect if `RETURN_HEADER` is `False`.
93+
This is allows the JavaScript Fetch API to access the header when CORS is enabled.
94+
95+
Default: `True`
96+
97+
* `INTEGRATIONS`
98+
> Whether to enable any custom or available integrations with `django_guid`.
99+
As an example, using `SentryIntegration()` as an integration would set Sentry's `transaction_id` to
100+
match the GUID used by the middleware.
101+
102+
Default: `[]`
103+
104+
* `IGNORE_URLS`
105+
> URL endpoints where the middleware will be disabled. You can put your health check endpoints here.
106+
107+
Default: `[]`
108+
109+
* `UUID_LENGTH`
110+
> Lets you optionally trim the length of the package generated UUIDs.
111+
112+
Default: `32`
113+
114+
## Configuration
115+
116+
117+
Once settings have set up, add the following to your projects' `settings.py`:
118+
119+
### 1. Installed apps
120+
121+
Add `django_guid` to your `INSTALLED_APPS`:
122+
123+
```python
124+
INSTALLED_APPS = [
125+
'django_guid',
126+
]
127+
```
128+
129+
### 2. Middleware
130+
131+
Add the `django_guid.middleware.guid_middleware` to your `MIDDLEWARE`:
132+
133+
```python
134+
MIDDLEWARE = [
135+
'django_guid.middleware.guid_middleware',
136+
...
137+
]
138+
```
139+
140+
It is recommended that you add the middleware at the top, so that the remaining middleware loggers include the requests GUID.
141+
142+
### 3. Logging configuration
143+
144+
Add `django_guid.log_filters.CorrelationId` as a filter in your `LOGGING` configuration:
145+
146+
```python
147+
LOGGING = {
148+
...
149+
'filters': {
150+
'correlation_id': {
151+
'()': 'django_guid.log_filters.CorrelationId'
152+
}
153+
}
154+
}
155+
```
156+
157+
Put that filter in your handler:
158+
159+
```python
160+
LOGGING = {
161+
...
162+
'handlers': {
163+
'console': {
164+
'class': 'logging.StreamHandler',
165+
'formatter': 'medium',
166+
'filters': ['correlation_id'],
167+
}
168+
}
169+
}
170+
```
171+
172+
And make sure to add the new `correlation_id` filter to one or all of your formatters:
173+
174+
```python
175+
LOGGING = {
176+
...
177+
'formatters': {
178+
'medium': {
179+
'format': '%(levelname)s %(asctime)s [%(correlation_id)s] %(name)s %(message)s'
180+
}
181+
}
182+
}
183+
```
184+
185+
If these settings were confusing, you might find the repo examples helpful.
186+
187+
### 4. Django GUID logger (optional)
188+
189+
If you wish to see the Django GUID middleware outputs, you may configure a logger for the module.
190+
Simply add django_guid to your loggers in the project, like in the example below:
191+
192+
```python
193+
LOGGING = {
194+
...
195+
'loggers': {
196+
'django_guid': {
197+
'handlers': ['console', 'logstash'],
198+
'level': 'WARNING',
199+
'propagate': False,
200+
}
201+
}
202+
}
203+
```
204+
205+
This could be useful for debugging problems with request ID propagation. If a received request header containing a request ID is misconfigured, we will not raise exceptions, but will generate warning logs.

0 commit comments

Comments
 (0)