Skip to main content

Add Correlation IDs to Django Requests and Responses

Project description

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"

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_correlation_ids-0.1.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_correlation_ids-0.1.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file django_correlation_ids-0.1.0.tar.gz.

File metadata

  • Download URL: django_correlation_ids-0.1.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for django_correlation_ids-0.1.0.tar.gz
Algorithm Hash digest
SHA256 49c924f23864e4930c517a0c99b749ba645c88e227e941cf6528ffcee70e5481
MD5 023fa011a1411876c50fc17a4f53db61
BLAKE2b-256 29549451bb93ddf49946c62f2f7ef695e3852c9fac10be447fdaaa9ab769f1c0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_correlation_ids-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 841cef1a52a5a3a1fb35d6705c01dfae58368093d22f13481dbfffcd6f3e9c3b
MD5 8df375a4215ed6b03a43ec55d9eb633a
BLAKE2b-256 b20c6ae0e49e84d52677eb88dfc9562d999208f38e8ee21fbb858ba00d1f109d

See more details on using hashes here.

Supported by

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