Django GUID attaches a unique correlation ID/request ID to all your log outputs for every request.

Overview

Django GUID

Now with ASGI support!

Package version Downloads Django versions ASGI WSGI

Docs Codecov Black Pre-commit


Django GUID attaches a unique correlation ID/request ID to all your log outputs for every request. In other words, all logs connected to a request now has a unique ID attached to it, making debugging simple.

Which version of Django GUID you should use depends on your Django version and whether you run ASGI or WSGI servers. To determine which Django-GUID version you should use, please see the table below.

Django version Django-GUID version
3.1.1 or above 3.x.x - ASGI and WSGI
3.0.0 - 3.1.0 2.x.x - Only WSGI
2.2.x 2.x.x - Only WSGI

Django GUID >= 3.0.0 uses ContextVar to store and access the GUID. Previous versions stored the GUID to an object, making it accessible by using the ID of the current thread. (Version 2 of Django GUID is supported until Django2.2 LTS is over.)


Resources:


Examples

Log output with a GUID:

INFO ... [773fa6885e03493498077a273d1b7f2d] project.views This is a DRF view log, and should have a GUID.
WARNING ... [773fa6885e03493498077a273d1b7f2d] project.services.file Some warning in a function
INFO ... [0d1c3919e46e4cd2b2f4ac9a187a8ea1] project.views This is a DRF view log, and should have a GUID.
INFO ... [99d44111e9174c5a9494275aa7f28858] project.views This is a DRF view log, and should have a GUID.
WARNING ... [0d1c3919e46e4cd2b2f4ac9a187a8ea1] project.services.file Some warning in a function
WARNING ... [99d44111e9174c5a9494275aa7f28858] project.services.file Some warning in a function

Log output without a GUID:

INFO ... project.views This is a DRF view log, and should have a GUID.
WARNING ... project.services.file Some warning in a function
INFO ... project.views This is a DRF view log, and should have a GUID.
INFO ... project.views This is a DRF view log, and should have a GUID.
WARNING ... project.services.file Some warning in a function
WARNING ... project.services.file Some warning in a function

See the documentation for more examples.

Installation

Install using pip:

pip install django-guid

Settings

Package settings are added in your settings.py:

DJANGO_GUID = {
    'GUID_HEADER_NAME': 'Correlation-ID',
    'VALIDATE_GUID': True,
    'RETURN_HEADER': True,
    'EXPOSE_HEADER': True,
    'INTEGRATIONS': [],
    'IGNORE_URLS': [],
    'UUID_LENGTH': 32,
}

Optional Parameters

  • GUID_HEADER_NAME

    The name of the GUID to look for in a header in an incoming request. Remember that it's case insensitive.

    Default: Correlation-ID

  • VALIDATE_GUID

    Whether the GUID_HEADER_NAME should be validated or not. If the GUID sent to through the header is not a valid GUID (uuid.uuid4).

    Default: True

  • RETURN_HEADER

    Whether to return the GUID (Correlation-ID) as a header in the response or not. It will have the same name as the GUID_HEADER_NAME setting.

    Default: True

  • EXPOSE_HEADER

    Whether to return Access-Control-Expose-Headers for the GUID header if RETURN_HEADER is True, has no effect if RETURN_HEADER is False. This is allows the JavaScript Fetch API to access the header when CORS is enabled.

    Default: True

  • INTEGRATIONS

    Whether to enable any custom or available integrations with django_guid. As an example, using SentryIntegration() as an integration would set Sentry's transaction_id to match the GUID used by the middleware.

    Default: []

  • IGNORE_URLS

    URL endpoints where the middleware will be disabled. You can put your health check endpoints here.

    Default: []

  • UUID_LENGTH

    Lets you optionally trim the length of the package generated UUIDs.

    Default: 32

Configuration

Once settings have set up, add the following to your projects' settings.py:

1. Installed Apps

Add django_guid to your INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    'django_guid',
]

2. Middleware

Add the django_guid.middleware.guid_middleware to your MIDDLEWARE:

MIDDLEWARE = [
    'django_guid.middleware.guid_middleware',
    ...
 ]

It is recommended that you add the middleware at the top, so that the remaining middleware loggers include the requests GUID.

3. Logging Configuration

Add django_guid.log_filters.CorrelationId as a filter in your LOGGING configuration:

LOGGING = {
    ...
    'filters': {
        'correlation_id': {
            '()': 'django_guid.log_filters.CorrelationId'
        }
    }
}

Put that filter in your handler:

LOGGING = {
    ...
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'formatter': 'medium',
            'filters': ['correlation_id'],
        }
    }
}

And make sure to add the new correlation_id filter to one or all of your formatters:

LOGGING = {
    ...
    'formatters': {
        'medium': {
            'format': '%(levelname)s %(asctime)s [%(correlation_id)s] %(name)s %(message)s'
        }
    }
}

If these settings were confusing, please have a look in the demo projects' settings.py file for a complete example.

4. Django GUID Logger (Optional)

If you wish to see the Django GUID middleware outputs, you may configure a logger for the module. Simply add django_guid to your loggers in the project, like in the example below:

LOGGING = {
    ...
    'loggers': {
        'django_guid': {
            'handlers': ['console', 'logstash'],
            'level': 'WARNING',
            'propagate': False,
        }
    }
}

This is especially useful when implementing the package, if you plan to pass existing GUIDs to the middleware, as misconfigured GUIDs will not raise exceptions, but will generate warning logs.

Comments
  • Add IGNORE_URLS setting

    Add IGNORE_URLS setting

    This (optional) setting will allow a user to opt-out of the middleware for certain URLs.

    The setting is a list of strings, which are the paths of urls to ignore. The paths can be written with prefix '/' and/or suffix '/', or without them.

    Related to issue #37

    enhancement 
    opened by Iftahh 11
  • Conditionally use deprecated default_app_config

    Conditionally use deprecated default_app_config

    default_app_config is being removed in Django 4.1 and a new warning has shown up since django 3.2

    https://docs.djangoproject.com/en/dev/internals/deprecation/#deprecation-removed-in-4-1

    opened by dustinchilson 10
  • [BUG] - Missing CHANGELOG.rst from packaged tar.gz

    [BUG] - Missing CHANGELOG.rst from packaged tar.gz

    Describe the bug setup.py includes a dependency on CHANGELOG.rst which is not included in the .tar.gz packages

    To Reproduce Retrieve django-guid-.tar.gz and try to run setup.py

    I've tested this on 1.1.1, 2.0.0 2.1.0 & 2.20

    Full stack trace [[email protected] django-guid-2.2.0]# /opt/acme/bin/python3 setup.py install Traceback (most recent call last): File "setup.py", line 7, in with open('CHANGELOG.rst') as changelog_file: FileNotFoundError: [Errno 2] No such file or directory: 'CHANGELOG.rst'

    bug 
    opened by wibbit 9
  • Best way to pass on the correlation-id to child threads?

    Best way to pass on the correlation-id to child threads?

    I am running a django application using gunicorn server, and there are some child threads that will be created by the parent thread. I am getting the guid (contextvar) using get_guid method in the parent thread, passing it as an argument to the child threads, and setting the guid contextvar in the child threads using set_guid method (both of these methods are present in django_guid.api module). Only then the guid is visible in the log messages.

    Is this the best way to pass on the correlation-ids to child threads? Or is there an alternate better way?

    question 
    opened by rdpravin1895 8
  • Sentry integration

    Sentry integration

    I still need to:

    • Bump version
    • Write docs

    but the core logic for this PR is done, and I think it would be good to review that before writing docs, to make sure you agree with the some of the design choices.


    This PR introduces a new package setting, INTEGRATIONS which will be a list of pre-made integrations from the new integrations.py.

    The idea for this PR originally, was only to add some functionality for integrating the correlation ID with Sentry, but I think it might be a good idea to design this feature in a way that opens up for more integrations in the future.

    The second addition is the Sentry logic, which I've packaged in a class so that we can bundle validation logic and execution logic for each integration, and separate it from other code.

    Also made some adjustments to doc-strings and inline comments here and there, but feel free to change them back

    enhancement 
    opened by sondrelg 7
  • ASGI support for later Django versions (works with Django 3.0.x)

    ASGI support for later Django versions (works with Django 3.0.x)

    With ASGI being stable and more async support to Django is coming up, this package eventually have to change it's design to be ASGI compliant.

    • ASGI by protocol can handle multiple requests on one thread. Every time await is used, the control of the loop will be handed back to the event loop, queuing the resumption of the function. The next function will run until it calls on an await, and then the entire thing repeats. This means that a thread will by design be able to handle multiple requests.

    • Most of Django do not support awaitat the moment, so django-guid will work on ASGI today. The way it works right now is that when it gets an ASGI request on the event loop, it dispatches it to a thread pool where each thread again handles one request at the time.

    Essentially this means that in the future, in order to keep this package living we have to find another ID to use. Luckily the Django people has been kind to us and implemented a replacement for us.

    I will look into implementing this ASAP.

    Thanks to knbk on #freenode.

    enhancement in progress 
    opened by JonasKs 6
  • Provide error page views that add the guid to the HTML output

    Provide error page views that add the guid to the HTML output

    I would like an easy way to override the Django error page views so that the request guid is included as part of the HTML. This is useful so that customers can easily send the guid to a support team to get faster help.

    suggestion 
    opened by andrew-cybsafe 5
  • IGNORE_URL setting to opt-out for some URLs

    IGNORE_URL setting to opt-out for some URLs

    I have a few requests that I don't want to see in the logs

    eg. a load balancer sends a "ping" once in a while to see the server is up the logs are spammed with generated new GUID messages:

    django_guid - Header `Correlation-ID` was not found in the incoming request. Generated new GUID: cf306e6e90bc42238f025d8d3fddc8ac
    

    for this request I would prefer to opt out of the middleware - a decorator would work nicely

    @no_guid
    def ping(request):
        return HttpResponse("Ok\n")
    

    Edit: It turns out that to use a decorator for opt-out would require to use the process_view hook, which runs after all the middelwares __call__ methods have been run. If the GUID would only be added at that point, it wouldn't be useful for debugging of other middleware, which is not a good idea. For that reason, instead of a decorator, the plan is to use a "black list" of URLs to ignore. I edited the Issue title to reflect this change.

    suggestion 
    opened by Iftahh 5
  • Bump django from 3.2.10 to 3.2.11

    Bump django from 3.2.10 to 3.2.11

    Bumps django from 3.2.10 to 3.2.11.

    Commits
    • 6e499a2 [3.2.x] Bumped version for 3.2.11 release.
    • 8d2f7cf [3.2.x] Fixed CVE-2021-45452 -- Fixed potential path traversal in storage sub...
    • c7fe895 [3.2.x] Fixed CVE-2021-45116 -- Fixed potential information disclosure in dic...
    • a8b32fe [3.2.x] Fixed CVE-2021-45115 -- Prevented DoS vector in UserAttributeSimilari...
    • b0aa070 [3.2.x] Added stub release notes for 3.2.11, and 2.2.26 releases.
    • ae24223 [3.2.x] Refs #33365, Refs #30530 -- Doc'd re_path() behavior change in Django...
    • ecd2793 [3.2.x] Added CVE-2021-44420 to security archive.
    • 1cea03a [3.2.x] Post-release version bump.
    • See full diff in compare view

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    • @dependabot use these labels will set the current labels as the default for future PRs for this repo and language
    • @dependabot use these reviewers will set the current reviewers as the default for future PRs for this repo and language
    • @dependabot use these assignees will set the current assignees as the default for future PRs for this repo and language
    • @dependabot use this milestone will set the current milestone as the default for future PRs for this repo and language

    You can disable automated security fix PRs for this repo from the Security Alerts page.

    dependencies 
    opened by dependabot[bot] 4
  • Update test workflow

    Update test workflow

    Changes

    • Adds django matrix with supported package versions and python 3.9 to the test workflow 🚀
    • Removes unnecessary isort config
    • Updates package metadata to include python 3.9 and django 3.1
    opened by sondrelg 4
  • Pass the GUID of a request to the Celery workers

    Pass the GUID of a request to the Celery workers

    Celery tasks triggered by a request will not have the GUID (Correlation-ID) attached to it. Implement a solution to inject the GUID into the Celery logs.

    For Celery tasks that are not triggered by a request, use the task-id instead as a GUID.

    suggestion 
    opened by JonasKs 4
  • chore: Add example projects

    chore: Add example projects

    I really love the idea of small runnable examples. This sets up the starting point for that by adding

    • examples/sync-views
    • examples/async-views

    My goal here has been to remove anything that isn't relevant to the very narrow functionality the examples are meant to show. The sync-views example now has a regular view and an ignored view, and we might actually go one step further and make a dedicated example out of the ignored view.

    If you agree, we can do something like:

    • examples/async-view
    • examples/sync-view
    • examples/ignore-url
    • examples/celery-integration
    • examples/sentry-integration
    • examples/celery-and-sentry-integration

    For now, I just wanted to open this as a first WIP proof of concept


    Update: Added examples for the 6 things I consider important. We might be missing something though :slightly_smiling_face:

    opened by sondrelg 3
  • v4 planning

    v4 planning

    • [ ] Make ID generator and validator generic, implement transformer (mirror asgi-correlation-id)
    • [ ] Split celery functionality into a separate package
    • [ ] Split sentry functionality into a separate package
    • [ ] Create extras for both
    • [x] Update license
    • [ ] Update test project to handle Django deprecation warnings
    • [x] Update to Poetry v1.2
    • [ ] Update classifiers
    • [ ] Rework docs (drop sphinx and just have readmes per module?)
    • [ ] Remove get_guid and set_guid
    • [ ] Create example projects for each feature

    Essentially we want to de-bloat the core a little, making it possible to install the core middleware without anything else. It's possible to install the middleware today, without adding celery as a dependency, but the celery code is still contained in the wheel.

    There's also some Sentry + Celery logic to consider @JonasKs. Do we put that in its own third package? Something to think about 🤔

    suggestion 
    opened by sondrelg 3
Releases(3.3.0)
  • 3.3.0(May 6, 2022)

    Features

    • Add UUID_FORMAT setting, allowing users to chose UUID/GUID format.
      • hex: 776336d4545e43e1bd3b017794af48e9 (default)
      • string: 776336d4-545e-43e1-bd3b-017794af48e9

    PR #81 by @Mdslino 🥇

    Source code(tar.gz)
    Source code(zip)
  • 3.2.2(Mar 7, 2022)

  • 3.2.1(Dec 13, 2021)

    Fixes:

    • Adjusted package requirements to allow Django >= v4
    • Added log input sanitation in the middleware
    • Added py.typed to enable type checking
    Source code(tar.gz)
    Source code(zip)
  • 2.2.1(Feb 24, 2021)

  • 3.2.0(Dec 3, 2020)

  • 3.1.0(Nov 19, 2020)

    Features

    • Added a new setting, UUID_LENGTH, which lets you crop the UUIDs generated for log filters.
    • Added a new integration for tracing with Celery
    Source code(tar.gz)
    Source code(zip)
  • 3.1.0-rc1(Nov 18, 2020)

    Features

    • Added a new setting, UUID_LENGTH, which lets you crop the UUIDs generated for log filters.
    • Added a new integration for tracing with Celery
    Source code(tar.gz)
    Source code(zip)
  • 3.0.1(Nov 12, 2020)

  • 3.0.0(Oct 28, 2020)

    Brings full async/ASGI (as well as the old WSGI) support to Django GUID using ContextVars instead of thread locals.

    Breaking changes

    This version requires Django>=3.1.1. For previous versions of Django, please use django-guid<3.0.0 (Such as django-guid==2.2.0).

    If you've already implemented django-guid in your project and are currently upgrading to Django>=3.1.1, please see the upgrading docs

    Source code(tar.gz)
    Source code(zip)
  • 2.2.0(Apr 12, 2020)

  • 2.1.0(Mar 11, 2020)

    Features

    • Integration module, which enables the users of django_guid to extend functionality.
    • Added a integration for Sentry, tagging the Sentry issue with the GUID used for the request.

    Other

    • Added docs for integrations
    Source code(tar.gz)
    Source code(zip)
  • 2.0.0(Mar 3, 2020)

    This version contains backwards incompatible changes. Read the entire changelog before upgrading

    Deprecated

    • SKIP_CLEANUP: After a request is finished, the Correlation ID is cleaned up using the request_finished Django signal.

    Incompatible changes

    • django_guid must be in INSTALLED_APPS due to usage of signals.

    Improvements

    • Restructured README and docs.
    Source code(tar.gz)
    Source code(zip)
  • 1.1.1(Feb 12, 2020)

  • 1.1.0(Feb 10, 2020)

    Features

    • Added a EXPOSE_HEADER setting, which will add the Access-Control-Expose-Headers with the RETURN_HEADER as value to the response. This is to allow the JavaScript Fetch API to access the header with the GUID
    Source code(tar.gz)
    Source code(zip)
  • 1.0.1(Feb 8, 2020)

    Bugfix

    • Fixed validation of incoming GUID

    Improvements

    • Changed the middleware.py logger name to django_guid

    • Added a WARNING-logger for when validation fails

    • Improved README

    Other

    • Added CONTRIBUTORS.rst
    Source code(tar.gz)
    Source code(zip)
Owner
snok
Open source collaboration
snok
💨 Fast, Async-ready, Openapi, type hints based framework for building APIs

Fast to learn, fast to code, fast to run Django Ninja - Fast Django REST Framework Django Ninja is a web framework for building APIs with Django and P

Vitaliy Kucheryaviy 3.8k Jan 01, 2023
A Django app to initialize Sentry client for your Django applications

Dj_sentry This Django application intialize Sentry SDK to your Django application. How to install You can install this packaging by using: pip install

Gandi 1 Dec 09, 2021
django-reversion is an extension to the Django web framework that provides version control for model instances.

django-reversion django-reversion is an extension to the Django web framework that provides version control for model instances. Requirements Python 3

Dave Hall 2.8k Jan 02, 2023
Money fields for Django forms and models.

django-money A little Django app that uses py-moneyed to add support for Money fields in your models and forms. Django versions supported: 1.11, 2.1,

1.4k Jan 06, 2023
Add infinite scroll to any django app.

django-infinite-scroll Add infinite scroll to any django app. Features - Allows to add infinite scroll to any page.

Gustavo Teixeira 1 Dec 26, 2021
Thumbnails for Django

Thumbnails for Django. Features at a glance Support for Django 2.2, 3.0 and 3.1 following the Django supported versions policy Python 3 support Storag

Jazzband 1.6k Jan 03, 2023
Django React Project Setup

Django-React-Project-Setup INSTALLATION: python -m pip install drps USAGE: in your cmd: python -m drps Starting fullstack project with Django and Reac

Ghazi Zabalawi 7 Feb 06, 2022
Sistema de tratamento e análise de grandes volumes de dados através de técnicas de Data Science

Sistema de tratamento e análise de grandes volumes de dados através de técnicas de data science Todos os scripts, gráficos e relatórios de todas as at

Arthur Quintanilha Neto 1 Sep 05, 2022
An URL Shortener with Basic Features.

Simple Url Shortener What is that? Yet another url shortener built with Django framework. Preview HOW TO RUN? 1. Virtual Environment First create a vi

Ethem Turgut 6 Jan 25, 2022
Zendesk Assignment - Django Based Ticket Viewer

Zendesk-Coding-Challenge Django Based Ticket Viewer The assignment has been made using Django. Important methods have been scripted in views.py. Excep

Akash Sampurnanand Pandey 0 Dec 23, 2021
The pytest framework makes it easy to write small tests, yet scales to support complex functional testing

The pytest framework makes it easy to write small tests, yet scales to support complex functional testing for applications and libraries. An example o

pytest-dev 9.6k Jan 06, 2023
WeatherApp - Simple Python Weather App

Weather App Please star this repo if you like ⭐ It's motivates me a lot! Stack A

Ruslan Shvetsov 3 Apr 18, 2022
Plug and play continuous integration with django and jenkins

django-jenkins Plug and play continuous integration with Django and Jenkins Installation From PyPI: $ pip install django-jenkins Or by downloading th

Mikhail Podgurskiy 941 Oct 22, 2022
A slick ORM cache with automatic granular event-driven invalidation.

Cacheops A slick app that supports automatic or manual queryset caching and automatic granular event-driven invalidation. It uses redis as backend for

Alexander Schepanovski 1.7k Jan 03, 2023
A prettier way to see Django requests while developing

A prettier way to see Django requests while developing

Adam Hill 35 Dec 02, 2022
A Django app that creates automatic web UIs for Python scripts.

Wooey is a simple web interface to run command line Python scripts. Think of it as an easy way to get your scripts up on the web for routine data anal

Wooey 1.9k Jan 08, 2023
This repository contains django library management system project.

Library Management System Django ** INSTALLATION** First of all install python on your system. Then run pip install -r requirements.txt to required se

whoisdinanath 1 Dec 26, 2022
MAC address Model Field & Form Field for Django apps

django-macaddress MAC Address model and form fields for Django We use netaddr to parse and validate the MAC address. The tests aren't complete yet. Pa

49 Sep 04, 2022
pytest-django allows you to test your Django project/applications with the pytest testing tool.

pytest-django allows you to test your Django project/applications with the pytest testing tool.

pytest-dev 1.1k Dec 14, 2022
A Minimalistic Modern Django Boilerplate

A Minimalistic Modern Django Boilerplate This boilerplate is mainly for educational purposes. It is meant to be cloned as a starter code for future tu

Jonathan Adly 21 Nov 02, 2022