django-middleware-request-id
The middleware detect if a client or the front reverse proxy server provides a X-Request-ID header, and get it as the request_id. If no such header is provided, it can provide a random value.
Install
pip install django-middleware-request-id
Usage
pro/settings.py
INSTALLED_APPS = [
"django_middleware_global_request",
"django_middleware_request_id",
]
MIDDLEWARE = [
...
"django_middleware_global_request.middleware.GlobalRequestMiddleware",
"django_middleware_request_id.middlewares.DjangoMiddlewareRequestId",
...
]
app/views.py
from django.http import HttpResponse
from django_middleware_request_id import get_request_id
def get_request_id_view(request):
request_id = get_request_id()
return HttpResponse(request_id)
app/urls.py
from django.contrib import admin
from django.urls import path
froml . import views
urlpatterns = [
path('get_request_id', views.get_request_id_view),
]
Set the request id at nginx
http {
...
# -------------------------------------------------------------------------------
# Set variable $reqid.
# If you trust client or front nginx's header, you can use `map block` here.
# -------------------------------------------------------------------------------
map $http_x_request_id $reqid {
default $http_x_request_id;
"" $request_id;
}
# -------------------------------------------------------------------------------
server {
# -------------------------------------------------------------------------------
# If you don't trust client and front nginx's header, or you are sure this
# is the first front nginx, just set the variable $reqid to a new random value.
# Use `map block` above, or use `set line` below, but don't use both.
# Uncomment the `set line` below to `set new random value`.
# -------------------------------------------------------------------------------
# set $reqid $request_id;
# -------------------------------------------------------------------------------
...
location /api/ {
proxy_pass http://backend/api/;
proxy_set_header X-Request-Id $reqid;
...
}
...
}
}
Releases
v0.1.0
- First release.
v0.1.2
- Fix problem that import
get_request_idfrom the package root. - Fix problem that NOT using DJANGO_REQUEST_ID_HEADER setting.
- Fix problem that call get_request_id() from none request context.
Metadata
Release files for django-middleware-request-id 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django-middleware-request-id-0.1.2.tar.gz | 5.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_middleware_request_id-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.0 kB
Release files / django-middleware-request-id-0.1.2.tar.gz
| Download URL | django-middleware-request-id-0.1.2.tar.gz |
|---|---|
| Size | 5.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4e6189695cb7f13e989d23f4660b5456b169067d6ff331f71d770daf27504832
|
|
BLAKE2b-256 checksum How to use checksums |
eada97ba01bcaf82f44a7c32143ced32b049dfd89d81c479816b1cc28d121bc6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.4
|
Release files / django_middleware_request_id-0.1.2-py3-none-any.whl
| Download URL | django_middleware_request_id-0.1.2-py3-none-any.whl |
|---|---|
| Size | 7.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c2af1fd2c213a0e444582868be2f6b2c6ca946a27feb2590b6a15f43198f43dd
|
|
BLAKE2b-256 checksum How to use checksums |
1246b059a5c820668c7a20dc507662cbe019aab2536c9a9131bdc1777bffc99b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.4
|