Skip to main content

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.

Project description

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.

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

django-middleware-request-id-0.1.0.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

File details

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

File metadata

File hashes

Hashes for django-middleware-request-id-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e9baa2cb36158bb52bc6fff9fc15a459c790194e798d0143fe86165aaedce5b0
MD5 79566cc67030e0a885a8fa11dedeaef3
BLAKE2b-256 1d58aa1520c4d34a9fe5ad881dd07b13a0b6b5cdcec8632bf30e8a3b34db81bd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_middleware_request_id-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c3d2e0a7c8f8e9d28c2382a49e1a21a746a4d6113051d26484d9678495a398b8
MD5 11b93a44eeea6b52baa1674019cc1ea8
BLAKE2b-256 03067c28f6c1cbd1eee11b05e5d40329e56e18c8fd32eae81a1ac01809b0bf04

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page