Inject an ID into every log message from a Django request. ASGI compatible, integrates with Sentry, and works with Celery

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 organization
snok
A django model and form field for normalised phone numbers using python-phonenumbers

django-phonenumber-field A Django library which interfaces with python-phonenumbers to validate, pretty print and convert phone numbers. python-phonen

Stefan Foulis 1.3k Dec 31, 2022
The magical reactive component framework for Django ✨

Unicorn The magical full-stack framework for Django ✨ Unicorn is a reactive component framework that progressively enhances a normal Django view, make

Adam Hill 1.4k Jan 05, 2023
Django Federated Login provides an authentication bridge between Django projects and OpenID-enabled identity providers.

Django Federated Login Django Federated Login provides an authentication bridge between Django projects and OpenID-enabled identity providers. The bri

Bouke Haarsma 18 Dec 29, 2020
Easy thumbnails for Django

Easy Thumbnails A powerful, yet easy to implement thumbnailing application for Django 1.11+ Below is a quick summary of usage. For more comprehensive

Chris Beaven 1.3k Dec 30, 2022
This is a repository for collecting global custom management extensions for the Django Framework.

Django Extensions Django Extensions is a collection of custom extensions for the Django Framework. Getting Started The easiest way to figure out what

Django Extensions 6k Dec 26, 2022
Keep track of failed login attempts in Django-powered sites.

django-axes Axes is a Django plugin for keeping track of suspicious login attempts for your Django based website and implementing simple brute-force a

Jazzband 1.1k Dec 30, 2022
Django application and library for importing and exporting data with admin integration.

django-import-export django-import-export is a Django application and library for importing and exporting data with included admin integration. Featur

2.6k Dec 26, 2022
Source files for a free pyRevit toolbar.

pyRoovit (WIP) What is this? PyRoovit is/will be a toolbar for the use with pyRevit built by Gavin Crump (aka Aussie BIM Guru). Having used and taught

Gavin Crump 11 Nov 10, 2022
Analytics services for Django projects

django-analytical The django-analytical application integrates analytics services into a Django project. Using an analytics service with a Django proj

Jazzband 1.1k Dec 31, 2022
ProjectManagementWebsite - Project management website for CMSC495 built using the Django stack

ProjectManagementWebsite A minimal project management website for CMSC495 built

Justin 1 May 23, 2022
English dictionary using Django based on freecodecamp

English Dictionary Hi there, i made this english dictionary using Django based on freecodecamp.org tutorial :) Table of Contents Preview Technologies

Aline Alencar 3 May 09, 2022
Create a netflix-like service using Django, React.js, & More.

Create a netflix-like service using Django. Learn advanced Django techniques to achieve amazing results like never before.

Coding For Entrepreneurs 67 Dec 08, 2022
Automatic caching and invalidation for Django models through the ORM.

Cache Machine Cache Machine provides automatic caching and invalidation for Django models through the ORM. For full docs, see https://cache-machine.re

846 Nov 26, 2022
A small Django app to easily broadcast an announcement across a website.

django-site-broadcasts The site broadcast application allows users to define short messages and announcements that should be displayed across a site.

Ben Lopatin 12 Jan 21, 2020
An orgizational tool to keep track of tasks/projects and the time spent on them.

Django-Task-Manager Task Tracker using Python Django About The Project This project is an orgizational tool to keep track of tasks/projects and the ti

Nick Newton 1 Dec 21, 2021
This Django app will be used to host Source.Python plugins, sub-plugins, and custom packages.

Source.Python Project Manager This Django app will be used to host Source.Python plugins, sub-plugins, and custom packages. Want to help develop this

2 Sep 24, 2022
Django backend of Helium's planner application

Helium Platform Project Prerequisites Python (= 3.6) Pip (= 9.0) MySQL (= 5.7) Redis (= 3.2) Getting Started The Platform is developed using Pytho

Helium Edu 17 Dec 14, 2022
The new Python SDK for Sentry.io

Bad software is everywhere, and we're tired of it. Sentry is on a mission to help developers write better software faster, so we can get back to enjoy

Sentry 1.4k Jan 05, 2023
Utilities to make function-based views cleaner, more efficient, and better tasting.

django-fbv Utilities to make Django function-based views cleaner, more efficient, and better tasting. 💥 📖 Complete documentation: https://django-fbv

Adam Hill 49 Dec 30, 2022
Advanced school management system written in Django :)

Advanced school management system written in Django :) ⚙️ Config the project First you should make venv for this project. So in the main root of proje

AminAli Mazarian 72 Dec 05, 2022