Skip to content

Commit

Permalink
Merge pull request #42 from snok/django31asgi
Browse files Browse the repository at this point in the history
ASGI and async support
  • Loading branch information
JonasKs committed Oct 28, 2020
2 parents 499a423 + 3c67976 commit b04199a
Show file tree
Hide file tree
Showing 37 changed files with 1,171 additions and 361 deletions.
11 changes: 4 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: [ "3.6", "3.7", "3.8", "3.9" ]
django-version: [ "2.2", "3.0", "3.1" ]
python-version: [ "3.7", "3.8", "3.9" ]
django-version: [ "3.1.1", "3.1.2" ]
steps:
- name: Check out repository
uses: actions/checkout@v2
Expand All @@ -48,13 +48,12 @@ jobs:
uses: sondrelg/install-poetry@v0.2.0
with:
virtualenvs-create: true
virtualenvs-in-project: true
- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v2
with:
path: .venv
key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}-4
key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}-5
- name: Install dependencies
run: |
source $HOME/.poetry/env
Expand All @@ -63,11 +62,9 @@ jobs:
- name: Install django ${{ matrix.django-version }}
run: |
source $HOME/.poetry/env
python -m pip install "Django==${{ matrix.django-version }}" --upgrade --force-reinstall
poetry run pip install "Django==${{ matrix.django-version }}"
- name: Run tests
run: |
source $HOME/.poetry/env
poetry run pytest --cov=django_guid tests/
poetry run coverage report
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
.idea/*
env/
venv/
.venv/
build/
dist/
*.egg-info/
notes
.pytest_cache
.coverage
htmlcov/

# Sphinx documentation
docs/_build/
3 changes: 2 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/ambv/black
rev: 20.8b1
rev: '20.8b1'
hooks:
- id: black
args: ['--quiet']
Expand Down Expand Up @@ -29,6 +29,7 @@ repos:
'flake8-type-annotations==0.1.0', # Looks for misconfigured type annotations
'flake8-annotations==2.4.0', # Enforces type annotation
]
args: ['--enable-extensions=G']
- repo: https://github.com/asottile/pyupgrade
rev: v2.7.2
hooks:
Expand Down
41 changes: 28 additions & 13 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@ Changelog
=========


`3.0.0`_ - 2020-11-04 - Full Django3.1+(ASGI/async) support!
------------------------------------------------------------
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`_.


`2.2.0`_ - 2020-11-04
---------------------
**Features**
Expand Down Expand Up @@ -177,16 +190,18 @@ Changelog
* Initial release


.. _0.2.0: https://github.com/jonasks/django-guid/compare/0.1.2...0.2.0
.. _0.2.1: https://github.com/jonasks/django-guid/compare/0.2.0...0.2.1
.. _0.2.2: https://github.com/jonasks/django-guid/compare/0.2.1...0.2.2
.. _0.2.3: https://github.com/jonasks/django-guid/compare/0.2.2...0.2.3
.. _0.3.0: https://github.com/jonasks/django-guid/compare/0.2.3...0.3.0
.. _0.3.1: https://github.com/jonasks/django-guid/compare/0.3.0...0.3.1
.. _1.0.0: https://github.com/jonasks/django-guid/compare/0.3.0...1.0.0
.. _1.0.1: https://github.com/jonasks/django-guid/compare/1.0.0...1.0.1
.. _1.1.0: https://github.com/jonasks/django-guid/compare/1.0.1...1.1.0
.. _1.1.1: https://github.com/jonasks/django-guid/compare/1.1.0...1.1.1
.. _2.0.0: https://github.com/jonasks/django-guid/compare/1.1.1...2.0.0
.. _2.1.0: https://github.com/jonasks/django-guid/compare/2.0.0...2.1.0
.. _2.2.0: https://github.com/jonasks/django-guid/compare/2.1.0...2.2.0
.. _0.2.0: https://github.com/snok/django-guid/compare/0.1.2...0.2.0
.. _0.2.1: https://github.com/snok/django-guid/compare/0.2.0...0.2.1
.. _0.2.2: https://github.com/snok/django-guid/compare/0.2.1...0.2.2
.. _0.2.3: https://github.com/snok/django-guid/compare/0.2.2...0.2.3
.. _0.3.0: https://github.com/snok/django-guid/compare/0.2.3...0.3.0
.. _0.3.1: https://github.com/snok/django-guid/compare/0.3.0...0.3.1
.. _1.0.0: https://github.com/snok/django-guid/compare/0.3.0...1.0.0
.. _1.0.1: https://github.com/snok/django-guid/compare/1.0.0...1.0.1
.. _1.1.0: https://github.com/snok/django-guid/compare/1.0.1...1.1.0
.. _1.1.1: https://github.com/snok/django-guid/compare/1.1.0...1.1.1
.. _2.0.0: https://github.com/snok/django-guid/compare/1.1.1...2.0.0
.. _2.1.0: https://github.com/snok/django-guid/compare/2.0.0...2.1.0
.. _2.2.0: https://github.com/snok/django-guid/compare/2.1.0...2.2.0
.. _3.0.0: https://github.com/snok/django-guid/compare/2.2.0...3.0.0
.. _upgrading docs: https://django-guid.readthedocs.io/en/latest/upgrading.html
6 changes: 6 additions & 0 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ Contributing to Django GUID
This package is open to contributions. To contribute, please follow these steps:

1. Fork the upstream django-guid repository into a personal account.

2. Install poetry running ``pip install poetry`` or ``curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -``

3. Install dependencies by running ``poetry install``

3. Install pre-commit (for linting) by running ``pre-commit install``

4. Create a new branch for you changes

5. Push the topic branch to your personal fork

6. Create a pull request to the django-guid repository with a detailed explanation of why you propose the changes should be made
6 changes: 5 additions & 1 deletion CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ Project Owner

* Jonas Krüger Svensson | `@JonasKs <https://github.com/JonasKs>`_ | `hotfix.guru <https://hotfix.guru>`_

Project Members
---------------

* Sondre Lillebø Gundersen | `@sondrelg <https://github.com/sondrelg>`_

Awesome Contributors
--------------------

* Sondre Lillebø Gundersen | `@sondrelg <https://github.com/sondrelg>`_
* Ingvald Lorentzen | `@ingvaldlorentzen <https://github.com/ingvaldlorentzen>`_
* Iftah Haimovitch | `@Iftahh <https://github.com/Iftahh>`_
108 changes: 80 additions & 28 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,39 +1,90 @@
###########
Django GUID
###########

.. image:: https://img.shields.io/pypi/v/django-guid.svg
:target: https://pypi.python.org/pypi/django-guid
.. image:: https://img.shields.io/pypi/pyversions/django-guid.svg
:target: https://pypi.python.org/pypi/django-guid#downloads
.. image:: https://img.shields.io/pypi/djversions/django-guid.svg
:target: https://pypi.python.org/pypi/django-guid
.. image:: https://readthedocs.org/projects/django-guid/badge/?version=latest
:target: https://django-guid.readthedocs.io/en/latest/?badge=latest
.. image:: https://codecov.io/gh/snok/django-guid/branch/master/graph/badge.svg
:target: https://codecov.io/gh/snok/django-guid
.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
:target: https://django-guid.readthedocs.io/en/latest/?badge=latest
.. image:: https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white
:target: https://github.com/pre-commit/pre-commit


Django GUID attaches a unique correlation ID to all your log outputs for every requests you handle. In other words, every error, and really every log now has an ID connecting it to all other relevant logs, making
debugging simple.

The package stores a GUID to an object, making it accessible by using the ID of the current thread. This makes integrations possible, as the ID can be returned as a header (built in setting) or forwarded manually to other systems (built in API), making it possible to extend the reach of correlation IDs to whole systems.
.. raw:: html

<p align="center">
<h1 align="center">Django GUID</h1>
</p>
<p align="center">
<em>Now with ASGI support!</em>
</p>

.. raw:: html

<p align="center">
<a href="https://pypi.org/pypi/django-guid">
<img src="https://img.shields.io/pypi/v/django-guid.svg" alt="Package version">
</a>
<a href="https://pypi.python.org/pypi/django-guid#downloads">
<img src="https://img.shields.io/badge/python-3.6+-blue.svg" alt="Downloads">
</a>
<a href="https://pypi.python.org/pypi/django-guid">
<img src="https://img.shields.io/badge/django-2.2%20|%203.0%20|%203.1%20-blue.svg" alt="Django versions">
</a>
</a>
<a href="https://img.shields.io/badge/ASGI-supported-brightgreen.svg">
<img src="https://img.shields.io/badge/ASGI-supported-brightgreen.svg" alt="ASGI">
</a>
<a href="https://img.shields.io/badge/WSGI-supported-brightgreen.svg">
<img src="https://img.shields.io/badge/WSGI-supported-brightgreen.svg" alt="WSGI">
</a>
</p>
<p align="center">
<a href="https://django-guid.readthedocs.io/en/latest/?badge=latest">
<img src="https://readthedocs.org/projects/django-guid/badge/?version=latest" alt="Docs">
</a>

<a href="https://codecov.io/gh/snok/django-guid">
<img src="https://codecov.io/gh/snok/django-guid/branch/master/graph/badge.svg" alt="Codecov">
</a>

<a href="https://github.com/psf/black">
<img src="https://img.shields.io/badge/code%20style-black-000000.svg" alt="Black">
</a>
<a href="https://github.com/pre-commit/pre-commit">
<img src="https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white" alt="Pre-commit">
</a>
</p>


--------------


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**:

* Free software: BSD License
* Documentation: https://django-guid.readthedocs.io
* Homepage: https://github.com/JonasKs/django-guid

--------------


**Examples**

Log output with a GUID:

.. code-block::
.. code-block:: flex
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
Expand All @@ -45,7 +96,7 @@ Log output with a GUID:
Log output without a GUID:

.. code-block::
.. code-block:: flex
INFO ... project.views This is a DRF view log, and should have a GUID.
WARNING ... project.services.file Some warning in a function
Expand All @@ -54,6 +105,7 @@ Log output without a GUID:
WARNING ... project.services.file Some warning in a function
WARNING ... project.services.file Some warning in a function
See the `documentation <https://django-guid.readthedocs.io>`_ for more examples.

************
Expand Down Expand Up @@ -146,12 +198,12 @@ Add :code:`django_guid` to your :code:`INSTALLED_APPS`:
2. Middleware
=============

Add the :code:`django_guid.middleware.GuidMiddleware` to your ``MIDDLEWARE``:
Add the :code:`django_guid.middleware.guid_middleware` to your ``MIDDLEWARE``:

.. code-block:: python
MIDDLEWARE = [
'django_guid.middleware.GuidMiddleware',
'django_guid.middleware.guid_middleware',
...
]
Expand Down
16 changes: 16 additions & 0 deletions demoproj/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for demoproj_asgi project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.1/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'demoproj.settings')

application = get_asgi_application()
16 changes: 16 additions & 0 deletions demoproj/services/async_services.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import asyncio
import logging

logger = logging.getLogger(__name__)


async def useless_function() -> bool:
"""
Useless function to demonstrate a function log message.
:return: True
"""
logger.info('Going to sleep for a sec')
await asyncio.sleep(1)

logger.warning('Warning, I am awake!')
return True
File renamed without changes.
4 changes: 2 additions & 2 deletions demoproj/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
]

MIDDLEWARE = [
'django_guid.middleware.GuidMiddleware', # <-- Add middleware here
'django_guid.middleware.guid_middleware', # <-- Add middleware at the top of your middlewares
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
Expand Down Expand Up @@ -90,7 +90,7 @@
'GUID_HEADER_NAME': 'Correlation-ID',
'VALIDATE_GUID': True,
'INTEGRATIONS': [],
'IGNORE_URLS': ['no_guid'],
'IGNORE_URLS': ['no-guid'],
}

# Set up logging for the project
Expand Down
8 changes: 6 additions & 2 deletions demoproj/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
"""
from django.urls import path

from demoproj.views import index_view, no_guid, rest_view
from demoproj.views.sync_views import index_view, no_guid, rest_view
from demoproj.views.async_views import index_view as asgi_index_view
from demoproj.views.async_views import django_guid_api_usage

urlpatterns = [
path('', index_view, name='index'),
path('api', rest_view, name='drf'),
path('no_guid', no_guid, name='no_guid'),
path('no-guid', no_guid, name='no_guid'),
path('asgi', asgi_index_view, name='asgi_index'),
path('api-usage', django_guid_api_usage, name='django_guid_api_usage'),
]
Empty file added demoproj/views/__init__.py
Empty file.

0 comments on commit b04199a

Please sign in to comment.