-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…21) * - Added a setting called ``RETURN_HEADER`` which defaults to true. It will return the GUID used in the application as a header in the outgoing package. - Improved tests to also check for headers in the response - Added tests for the new setting - Added some more information about the package in the README and in the docs - Added an API page to the docs, showing how to pass on the current header in a custom request to another application - Fixed read the docs menu - Bumped version to 1.0.0 * Added unittest for invalid RETURN_HEADER * Apply suggestions from code review Co-Authored-By: sondrelg <sondrelg@live.no> * Update CHANGELOG.rst based on code review Co-Authored-By: sondrelg <sondrelg@live.no> * - Added an extended example to the readthedocs - Referenced the example in the README.rst - Added wsgi so users of this package can try it out them self Co-authored-by: sondrelg <sondrelg@live.no>
- Loading branch information
Showing
20 changed files
with
357 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import os | ||
|
||
from django.core.wsgi import get_wsgi_application | ||
|
||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'demoproj.settings') | ||
|
||
# This application object is used by any WSGI server configured to use this | ||
# file. This includes Django's development server, if the WSGI_APPLICATION | ||
# setting points here. | ||
|
||
application = get_wsgi_application() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,4 @@ | |
Adding imports here will break setup.py | ||
""" | ||
|
||
__version__ = '0.3.1' | ||
__version__ = '1.0.0' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
API | ||
=== | ||
|
||
Getting started | ||
--------------- | ||
To use the API import the GuidMiddleware: | ||
|
||
.. code-block:: python | ||
from django_guid.middleware import GuidMiddleware | ||
get_guid() | ||
---------- | ||
* **Parameters**: ``default`` = ``None`` - What to return if no ``GUID`` is found. | ||
|
||
* **Returns**: ``str`` or ``default`` | ||
|
||
Fetches the GUID. | ||
|
||
.. code-block:: python | ||
guid = GuidMiddleware.get_guid(default=None) | ||
set_guid() | ||
---------- | ||
* **Parameters**: ``guid``: ``str`` | ||
|
||
Sets the GUID to the given input | ||
|
||
.. code-block:: python | ||
GuidMiddleware.set_guid('My GUID') | ||
delete_guid() | ||
------------- | ||
Deletes the stored GUID | ||
|
||
.. code-block:: python | ||
GuidMiddleware.delete_guid() | ||
Example usage | ||
------------- | ||
|
||
.. code-block:: python | ||
import requests | ||
from django.conf import settings | ||
from django_guid.middleware import GuidMiddleware | ||
requests.get( | ||
url='http://localhost/api', | ||
headers={ | ||
'Accept': 'application/json', | ||
settings.DJANGO_GUID['GUID_HEADER_NAME']: GuidMiddleware.get_guid(), | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.. include:: ../CONTRIBUTING.rst |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
.. _extended_example: | ||
|
||
Extended example | ||
================ | ||
|
||
Using tools like ``ab`` (`Apache Benchmark <https://httpd.apache.org/docs/2.4/programs/ab.html>`_) we can benchmark our application with concurrent requests, simulating | ||
heavy load. This is an easy way to display the strength of ``django-guid``. | ||
|
||
Experiment | ||
---------- | ||
|
||
First, we run our application like we would in a production environment: | ||
|
||
.. code-block:: bash | ||
gunicorn demoproj.wsgi:application --bind 127.0.0.1:8080 -k gthread -w 4 | ||
Then, we do 3 concurrent requests to one of our endpoints: | ||
|
||
.. code-block:: bash | ||
ab -c 3 -n 3 http://127.0.0.1:8080/api | ||
This results in these logs: | ||
|
||
.. code-block:: bash | ||
django-guid git:(master) ✗ gunicorn demoproj.wsgi:application --bind 127.0.0.1:8080 -k gthread -w 4 | ||
[2020-01-14 16:36:15 +0100] [8624] [INFO] Starting gunicorn 20.0.4 | ||
[2020-01-14 16:36:15 +0100] [8624] [INFO] Listening at: http://127.0.0.1:8080 (8624) | ||
[2020-01-14 16:36:15 +0100] [8624] [INFO] Using worker: gthread | ||
[2020-01-14 16:36:15 +0100] [8627] [INFO] Booting worker with pid: 8627 | ||
[2020-01-14 16:36:15 +0100] [8629] [INFO] Booting worker with pid: 8629 | ||
[2020-01-14 16:36:15 +0100] [8630] [INFO] Booting worker with pid: 8630 | ||
[2020-01-14 16:36:15 +0100] [8631] [INFO] Booting worker with pid: 8631 | ||
# First request | ||
INFO 2020-01-14 15:40:48,953 [None] django_guid.middleware No Correlation-ID found in the header. Added Correlation-ID: 773fa6885e03493498077a273d1b7f2d | ||
INFO 2020-01-14 15:40:48,954 [773fa6885e03493498077a273d1b7f2d] demoproj.views This is a DRF view log, and should have a GUID. | ||
WARNING 2020-01-14 15:40:48,954 [773fa6885e03493498077a273d1b7f2d] demoproj.services.useless_file Some warning in a function | ||
DEBUG 2020-01-14 15:40:48,954 [773fa6885e03493498077a273d1b7f2d] django_guid.middleware Deleting 773fa6885e03493498077a273d1b7f2d from _guid | ||
# Second and third request arrives at the same time | ||
INFO 2020-01-14 15:40:48,955 [None] django_guid.middleware No Correlation-ID found in the header. Added Correlation-ID: 0d1c3919e46e4cd2b2f4ac9a187a8ea1 | ||
INFO 2020-01-14 15:40:48,955 [None] django_guid.middleware No Correlation-ID found in the header. Added Correlation-ID: 99d44111e9174c5a9494275aa7f28858 | ||
INFO 2020-01-14 15:40:48,955 [0d1c3919e46e4cd2b2f4ac9a187a8ea1] demoproj.views This is a DRF view log, and should have a GUID. | ||
INFO 2020-01-14 15:40:48,955 [99d44111e9174c5a9494275aa7f28858] demoproj.views This is a DRF view log, and should have a GUID. | ||
WARNING 2020-01-14 15:40:48,955 [0d1c3919e46e4cd2b2f4ac9a187a8ea1] demoproj.services.useless_file Some warning in a function | ||
WARNING 2020-01-14 15:40:48,955 [99d44111e9174c5a9494275aa7f28858] demoproj.services.useless_file Some warning in a function | ||
DEBUG 2020-01-14 15:40:48,955 [0d1c3919e46e4cd2b2f4ac9a187a8ea1] django_guid.middleware Deleting 0d1c3919e46e4cd2b2f4ac9a187a8ea1 from _guid | ||
DEBUG 2020-01-14 15:40:48,955 [99d44111e9174c5a9494275aa7f28858] django_guid.middleware Deleting 99d44111e9174c5a9494275aa7f28858 from _guid | ||
If we have a close look, we can see that the first request is completely done before the second and third arrives. | ||
How ever, the second and third request arrives at the exact same time, and since ``gunicorn`` is run with multiple workers, | ||
they are also handled concurrently. The result is logs that get mixed together, making them impossible to differentiate. | ||
|
||
Now, depending on how you view your logs you can easily track a single request down. In these docs, try using ``ctrl`` + ``f`` | ||
and search for ``99d44111e9174c5a9494275aa7f28858`` | ||
|
||
If you're logging to a file you could use ``grep``: | ||
|
||
|
||
.. code-block:: bash | ||
➜ ~ cat demoproj/logs.log | grep 99d44111e9174c5a9494275aa7f28858 | ||
INFO 2020-01-14 15:40:48,955 [None] django_guid.middleware No Correlation-ID found in the header. Added Correlation-ID: 99d44111e9174c5a9494275aa7f28858 | ||
INFO 2020-01-14 15:40:48,955 [99d44111e9174c5a9494275aa7f28858] demoproj.views This is a DRF view log, and should have a GUID. | ||
WARNING 2020-01-14 15:40:48,955 [99d44111e9174c5a9494275aa7f28858] demoproj.services.useless_file Some warning in a function | ||
DEBUG 2020-01-14 15:40:48,955 [99d44111e9174c5a9494275aa7f28858] django_guid.middleware Deleting 99d44111e9174c5a9494275aa7f28858 from _guid |
Oops, something went wrong.