Registry / web-framework / django-request-id

django-request-id

JSON →
library1.0.0pypypi✓ verified 85d ago

django-request-id is a Python library for Django that augments each HTTP request with a unique ID, primarily for improved logging and tracing across services. It is currently at version 1.0.0 and maintains a stable release cadence, focusing on core functionality and compatibility with recent Django versions.

pip install django-request-id
INSTALL
IMPORT
SIG · DJANGO-REQUEST-ID
D
django-request-id
web-frameworkpythonv1.0.0
Install
3.4s avg
Import
Disk
66MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.0.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.915 runs
installs and imports cleanly · install 0.0s · import 0.000s · 66.4MB
glibc
py 3.103.915 runs
installs and imports cleanly · install 3.4s · import 0.000s · 67MB
66MB installed
● package 66MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

generate_request_id
from request_id import generate_request_id
from request_id.middleware import RequestIDMiddleware
get_current_request_id
from request_id import get_current_request_id
Local
from request_id import Local

To quickly integrate `django-request-id`, add `request_id.middleware.RequestIDMiddleware` to your `MIDDLEWARE` setting. Crucially, configure your Django `LOGGING` settings to include `request_id.logging.RequestIDFilter` and apply it to your desired log handlers. Also, ensure your log formatters include `{request_id}` to display the unique ID.

import os # settings.py # Add the middleware to your Django project MIDDLEWARE = [ # ... other middleware (e.g., 'django.middleware.security.SecurityMiddleware') 'request_id.middleware.RequestIDMiddleware', # ... other middleware (e.g., 'django.contrib.sessions.middleware.SessionMiddleware') ] # Configure logging to include the request ID LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'filters': { 'request_id': { '()': 'request_id.logging.RequestIDFilter' } }, 'formatters': { 'verbose': { 'format': '{levelname} {asctime} {request_id} {module} {process:d} {thread:d} {message}', 'style': '{', }, }, 'handlers': { 'console': { 'class': 'logging.StreamHandler', 'filters': ['request_id'], # Apply the filter to a handler 'formatter': 'verbose', } }, 'root': { 'handlers': ['console'], 'level': 'INFO', }, 'loggers': { 'django': { 'handlers': ['console'], 'level': 'INFO', 'propagate': True, }, }, } # Example usage in a Django view or signal (for testing, not part of quickstart setup itself) # import logging # logger = logging.getLogger(__name__) # def my_view(request): # logger.info('This message should have a request ID.') # return HttpResponse('Hello world')
Debug
Known issues
gotchaThe request ID will not appear in your logs unless you explicitly configure the logging system to use `RequestIDFilter` and include `{request_id}` in your formatter strings.
fix
Ensure `LOGGING['filters']['request_id']['()'] = 'request_id.logging.RequestIDFilter'` is set and `'filters': ['request_id']` is applied to your `LOGGING['handlers']` entries. Also, modify your formatter `format` strings to include `{request_id}`.
affects: All versions
gotchaWhen integrating with upstream proxies or load balancers, be aware that `django-request-id` will use an existing `X-Request-ID` header if present. If you need it to always generate a new ID, you might need to ensure the header is not passed from the proxy or configure the proxy to override it.
fix
Consult your proxy/load balancer documentation (e.g., Nginx, AWS ELB/ALB) to understand how `X-Request-ID` headers are handled and configure them to either pass through or strip/override as per your requirements.
affects: All versions
gotchaThe default header used for injecting/extracting the request ID is `X-Request-ID`. If your infrastructure uses a different header name, you'll need to configure `REQUEST_ID_HEADER` and `REQUEST_ID_RESPONSE_HEADER` in your Django settings.
fix
Add `REQUEST_ID_HEADER = 'Your-Custom-Header'` and `REQUEST_ID_RESPONSE_HEADER = 'Your-Custom-Header'` to your `settings.py`.
affects: All versions
Upgrade
Version history
1.0.0latest on PyPI · released Oct 23, 2016
Audit
Dependencies
DjangorequiredThis is a Django middleware library, requiring Django to function.
Agent activity
23 hits · last 30 days
node
20
OpenAI (training)
2
Resources
django-request-id — pip install django-request-id · libregistry