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.

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.

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.2.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

File details

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

File metadata

File hashes

Hashes for django-middleware-request-id-0.1.2.tar.gz
Algorithm Hash digest
SHA256 4e6189695cb7f13e989d23f4660b5456b169067d6ff331f71d770daf27504832
MD5 b1395a513390ad9ee4240958e24bfa4b
BLAKE2b-256 eada97ba01bcaf82f44a7c32143ced32b049dfd89d81c479816b1cc28d121bc6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_middleware_request_id-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c2af1fd2c213a0e444582868be2f6b2c6ca946a27feb2590b6a15f43198f43dd
MD5 9647bb27704277cfbfac1053478816a2
BLAKE2b-256 1246b059a5c820668c7a20dc507662cbe019aab2536c9a9131bdc1777bffc99b

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