Skip to main content

Django Correlation IDs

Django Correlation IDs adds Correlation IDs into your log records to enhace their traceability, especially for distributed systems.

Each request is assigned a unique identifier--its Correlation ID. As the request is processed by your project, each Django subsystem and your apps' loggers have the ability to produce log messages that include this identifier:

2025-12-01T16:25:34 12345678-1234-1234-1234-123456789abc INFO: handling POST /foo/
2025-12-01T16:25:34 87654321-4321-4321-4321-fed987654321 INFO: handling GET /bar/
2025-12-01T16:25:34 87654321-4321-4321-4321-fed987654321 DEBUG: executing query "SELECT ..." with params (...)
2025-12-01T16:25:35 12345678-1234-1234-1234-123456789abc WARNING: found condition in payload data
2025-12-01T16:25:35 87654321-4321-4321-4321-fed987654321 INFO: responding 200 to GET /bar/
2025-12-01T16:25:36 12345678-1234-1234-1234-123456789abc DEBUG: executing query "SELECT ..." with params (...)
2025-12-01T16:25:36 12345678-1234-1234-1234-123456789abc DEBUG: executing query "INSERT ..." with params (...)
2025-12-01T16:25:36 12345678-1234-1234-1234-123456789abc INFO: reponding 201 to POST /foo/

Installation

Django Correlation IDs can be installed from PyPI:

uv add django-correlation-ids

Then, add the app to the list of installed apps in your project's settings.py file:

INSTALLED_APPS = [
    # ... other apps
    "django_correlation_ids",
]

Next, add the appropriate middleware to your project's middleware list in settings.py:

MIDDLEWARE = [
    # ... other middleware
    "django_correlation_ids.middleware.CorrelationMiddleware",
    # or if your project is running on ASGI:
    # "django_correlation_ids.middleware.AsyncCorrelationMiddleware"
]

Settings

Django Correlation IDs does not require any additional settings to be used by your project. It does have optional settings that can be configured in your project's settings.py file.

The default settings for Django Correlation IDs are:

DJANGO_CORRELATION_IDS = {
    # Name of the HTTP header for responses. Will also be used as the name of
    # the HTTP header for requests when 'accept_correlation_id' is True.
    "correlation_id_header": "X-Correlation-Id",
    
    # Name of the HTTP header for requests and responses.
    "request_id_header": "X-Request-Id",
    
    # If a request has a Correlation ID header, should that value be used as
    # the Correlation ID by this software? This can be useful if your app is
    # part of a collection of APIs; e.g. a microservice architecture.
    "accept_correlation_id": True,
    
    # If the request has a Request ID header, should that value be used as
    # the Correlation ID by this software?
    "recycle_request_id": False,
    
    # If, for whatever reason, the Correlation ID is empty when returning a
    # response, should an empty header value be allowed to be sent?
    "allow_response_empty_correlation_id": False,
    
    # If the Request ID is empty when returning a response, should an empty
    # header value be allowed to be sent?
    "allow_response_empty_request_id": False,
}

Usage

The main interface of Django Correlation IDs is via logging. This software provides a custom logging filter object that will add the current request's Correlation ID, and optionally the Request ID, to log records.

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "filters": {
        "correlation": {
            "()": "django_correlation_ids.logging.CorrelationFilter",
        },
    },
    "formatters": {
        "correlation": {
            "format": "{asctime} {correlation_id} {levelname}: {message}",
            "style": "{",
            "datefmt": "%Y-%m-%dT%H:%M:%S%z",
        },
    },
    "handlers": {
        "console": {
            "class": "logging.StreamHandler",
            "filters": ["correlation"],
            "formatter": "correlation",
        },
    },
    "loggers": {
        "your_app": {
            "handlers": ["console"],
            "propagate": True,
            "level": "DEBUG",
        },
        "django": {
            "handlers": ["console"],
            "propagate": True,
            "level": "INFO",
        },
    },
}

Django Correlation IDs also provides a simple interface to access the current Correlation and Request IDs:

>>> from django_correlation_ids import context
>>> context.get_correlation_id()
"some-correlation-id-string"
>>> context.get_request_id()
"some-request-id-string"

Metadata

Release files for django-correlation-ids 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-correlation-ids 0.1.0
File Size Uploaded Provenance
django_correlation_ids-0.1.0.tar.gz 4.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for django-correlation-ids 0.1.0
File Interpreter ABI Platform
django_correlation_ids-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.9 kB

Release files / django_correlation_ids-0.1.0.tar.gz

Download URL django_correlation_ids-0.1.0.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
49c924f23864e4930c517a0c99b749ba645c88e227e941cf6528ffcee70e5481
BLAKE2b-256 checksum
How to use checksums
29549451bb93ddf49946c62f2f7ef695e3852c9fac10be447fdaaa9ab769f1c0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.0

Release files / django_correlation_ids-0.1.0-py3-none-any.whl

Download URL django_correlation_ids-0.1.0-py3-none-any.whl
Size 7.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
841cef1a52a5a3a1fb35d6705c01dfae58368093d22f13481dbfffcd6f3e9c3b
BLAKE2b-256 checksum
How to use checksums
b20c6ae0e49e84d52677eb88dfc9562d999208f38e8ee21fbb858ba00d1f109d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.0

Security for django-correlation-ids 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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