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;
            ...
        }
        ...
    }
}

Test Passed

  • python27:~=django1.11.29
  • python34:~=django1.11.29
  • python34:~=django2.0.13
  • python35:~=django1.11.29
  • python35:~=django2.0.13
  • python35:~=django2.1.15
  • python35:~=django2.2.28
  • python36:~=django2.0.13
  • python36:~=django2.1.15
  • python36:~=django2.2.28
  • python36:~=django3.0.14
  • python36:~=django3.1.14
  • python36:~=django3.2.21
  • python37:~=django2.0.13
  • python37:~=django2.1.15
  • python37:~=django2.2.28
  • python37:~=django3.0.14
  • python37:~=django3.1.14
  • python37:~=django3.2.21
  • python38:~=django2.0.13
  • python38:~=django2.1.15
  • python38:~=django2.2.28
  • python38:~=django3.0.14
  • python38:~=django3.1.14
  • python38:~=django3.2.21
  • python38:~=django4.0.10
  • python38:~=django4.1.11
  • python38:~=django4.2.5
  • python39:~=django2.0.13
  • python39:~=django2.1.15
  • python39:~=django2.2.28
  • python39:~=django3.0.14
  • python39:~=django3.1.14
  • python39:~=django3.2.21
  • python39:~=django4.0.10
  • python39:~=django4.1.11
  • python39:~=django4.2.5
  • python310:~=django2.1.15
  • python310:~=django2.2.28
  • python310:~=django3.0.14
  • python310:~=django3.1.14
  • python310:~=django3.2.21
  • python310:~=django4.0.10
  • python310:~=django4.1.11
  • python310:~=django4.2.5
  • python311:~=django2.2.28
  • python311:~=django3.0.14
  • python311:~=django3.1.14
  • python311:~=django3.2.21
  • python311:~=django4.0.10
  • python311:~=django4.1.11
  • python311:~=django4.2.5

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.

v0.1.4

  • Unit test all passed.

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

Uploaded Source

Built Distribution

File details

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

File metadata

File hashes

Hashes for django-middleware-request-id-0.1.4.tar.gz
Algorithm Hash digest
SHA256 581dfc937f81a31aff3a45a0e2ca2dabba499c28dc71393d854bec3a3385016a
MD5 fef089953d57f504225f3760ed23da82
BLAKE2b-256 295d03e7da2f6d7773722db9608eccfbed8487a5fc0c306e1d6f2d162303ba12

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_middleware_request_id-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 4d49463e878159300dc96e45c1901d876c4efdc21d5b6f4da691bfc8f40589f7
MD5 16fb8b43fd927cb364e62bbf7f3d8430
BLAKE2b-256 4f01926bef1cd9bebf6c26a6a6b07c0cc08fadb11aea4f1ea1f5fe5344f4b193

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