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.

v0.1.2

  • Fix problem that import get_request_id from the package root.
  • Fix problem that NOT using DJANGO_REQUEST_ID_HEADER setting.
  • Fix problem that call get_request_id() from none request context.

v0.1.3

  • Doc update.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-middleware-request-id-0.1.3.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_middleware_request_id-0.1.3-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file django-middleware-request-id-0.1.3.tar.gz.

File metadata

File hashes

Hashes for django-middleware-request-id-0.1.3.tar.gz
Algorithm Hash digest
SHA256 0e825c9aae6aab9c3f31f1637bde2f18ecf4a2a6c9b8718c4e32d34f634becf4
MD5 2399b4f572fba809a8ef412033b69099
BLAKE2b-256 80994a9200bdad555db26d61e4db70e4aa4ebefc6622c74061fc86957cc82519

See more details on using hashes here.

File details

Details for the file django_middleware_request_id-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for django_middleware_request_id-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d875b7402c33ef9c1c140c3ffefec1f62efe9062308ffd5ca129f43c285985ca
MD5 a931c39f1c6bfc1a159365e7c10408c8
BLAKE2b-256 55e9aed0cec04dcd758a66ce1fe39cf769baa4178f77311ed3e05d313b6238ab

See more details on using hashes here.

Release history Release notifications | RSS feed

0.1.6

2 files

0.1.5

2 files

0.1.4

2 files

This release

0.1.3 This release

2 files

0.1.2

2 files

0.1.0

2 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