Skip to main content

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.

Metadata

Release files for django-middleware-request-id 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-middleware-request-id 0.1.0
File Size Uploaded
django-middleware-request-id-0.1.0.tar.gz 4.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-middleware-request-id 0.1.0
File Interpreter ABI Platform
django_middleware_request_id-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.6 kB

Release files / django-middleware-request-id-0.1.0.tar.gz

Download URL django-middleware-request-id-0.1.0.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
e9baa2cb36158bb52bc6fff9fc15a459c790194e798d0143fe86165aaedce5b0
BLAKE2b-256 checksum
How to use checksums
1d58aa1520c4d34a9fe5ad881dd07b13a0b6b5cdcec8632bf30e8a3b34db81bd
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.0-py3-none-any.whl

Download URL django_middleware_request_id-0.1.0-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c3d2e0a7c8f8e9d28c2382a49e1a21a746a4d6113051d26484d9678495a398b8
BLAKE2b-256 checksum
How to use checksums
03067c28f6c1cbd1eee11b05e5d40329e56e18c8fd32eae81a1ac01809b0bf04
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.4

Release history Release notifications | RSS feed

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page