Skip to content

Commit

Permalink
Merge pull request #40 from kingbuzzman/master
Browse files Browse the repository at this point in the history
Adds the ability to change the default value of missing request_id
  • Loading branch information
RealOrangeOne authored Apr 17, 2020
2 parents ad6e71c + fb01ff1 commit 54d1afc
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 18 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ If you wish to include the request id in the response headers, add the following
REQUEST_ID_RESPONSE_HEADER = "RESPONSE_HEADER_NAME"
```

If you wish to change the default `request_id` in the log output, the the following settings, where `none` (default) is the value you want to be the default value in case it's missing.

```python
NO_REQUEST_ID = "none"
```

Logging all requests
--------------------

Expand Down
6 changes: 3 additions & 3 deletions log_request_id/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import threading


__version__ = "1.4.1"
__version__ = "1.5.0"


local = threading.local()


REQUEST_ID_HEADER_SETTING = 'LOG_REQUEST_ID_HEADER'
LOG_REQUESTS_SETTING = 'LOG_REQUESTS'
NO_REQUEST_ID = "none" # Used if no request ID is available
LOG_REQUESTS_NO_SETTING = 'NO_REQUEST_ID'
DEFAULT_NO_REQUEST_ID = "none" # Used if no request ID is available
REQUEST_ID_RESPONSE_HEADER_SETTING = 'REQUEST_ID_RESPONSE_HEADER'
OUTGOING_REQUEST_ID_HEADER_SETTING = 'OUTGOING_REQUEST_ID_HEADER'
GENERATE_REQUEST_ID_IF_NOT_IN_HEADER_SETTING = 'GENERATE_REQUEST_ID_IF_NOT_IN_HEADER'
8 changes: 6 additions & 2 deletions log_request_id/filters.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import logging
from log_request_id import local, NO_REQUEST_ID

from django.conf import settings

from log_request_id import DEFAULT_NO_REQUEST_ID, LOG_REQUESTS_NO_SETTING, local


class RequestIDFilter(logging.Filter):

def filter(self, record):
record.request_id = getattr(local, 'request_id', NO_REQUEST_ID)
default_request_id = getattr(settings, LOG_REQUESTS_NO_SETTING, DEFAULT_NO_REQUEST_ID)
record.request_id = getattr(local, 'request_id', default_request_id)
return True
6 changes: 3 additions & 3 deletions log_request_id/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from django.utils.deprecation import MiddlewareMixin
except ImportError:
MiddlewareMixin = object
from log_request_id import local, REQUEST_ID_HEADER_SETTING, LOG_REQUESTS_SETTING, NO_REQUEST_ID, \
REQUEST_ID_RESPONSE_HEADER_SETTING, GENERATE_REQUEST_ID_IF_NOT_IN_HEADER_SETTING
from log_request_id import local, REQUEST_ID_HEADER_SETTING, LOG_REQUESTS_SETTING, DEFAULT_NO_REQUEST_ID, \
REQUEST_ID_RESPONSE_HEADER_SETTING, GENERATE_REQUEST_ID_IF_NOT_IN_HEADER_SETTING, LOG_REQUESTS_NO_SETTING


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -55,7 +55,7 @@ def _get_request_id(self, request):
if request_id_header:
# fallback to NO_REQUEST_ID if settings asked to use the
# header request_id but none provided
default_request_id = NO_REQUEST_ID
default_request_id = getattr(settings, LOG_REQUESTS_NO_SETTING, DEFAULT_NO_REQUEST_ID)

# unless the setting GENERATE_REQUEST_ID_IF_NOT_IN_HEADER
# was set, in which case generate an id as normal if it wasn't
Expand Down
11 changes: 5 additions & 6 deletions log_request_id/session.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from requests import Session as BaseSession
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from requests import Session as BaseSession

from log_request_id import local, REQUEST_ID_HEADER_SETTING, NO_REQUEST_ID, OUTGOING_REQUEST_ID_HEADER_SETTING
from log_request_id import DEFAULT_NO_REQUEST_ID, OUTGOING_REQUEST_ID_HEADER_SETTING, REQUEST_ID_HEADER_SETTING, local


class Session(BaseSession):
Expand All @@ -23,10 +23,9 @@ def prepare_request(self, request):
"""Include the request ID, if available, in the outgoing request"""
try:
request_id = local.request_id
if self.request_id_header:
request.headers[self.request_id_header] = request_id
except AttributeError:
request_id = NO_REQUEST_ID

if self.request_id_header and request_id != NO_REQUEST_ID:
request.headers[self.request_id_header] = request_id
pass

return super(Session, self).prepare_request(request)
25 changes: 21 additions & 4 deletions log_request_id/tests.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import logging
from django.test import TestCase, RequestFactory

from django.core.exceptions import ImproperlyConfigured
from django.test import RequestFactory, TestCase, override_settings
from requests import Request

from log_request_id import DEFAULT_NO_REQUEST_ID, local
from log_request_id.session import Session
from log_request_id.middleware import RequestIDMiddleware
from testproject.views import test_view

Expand All @@ -14,6 +17,12 @@ def setUp(self):
self.handler = logging.getLogger('testproject').handlers[0]
self.handler.messages = []

# Ensure that there is nothing lurking around from previous tests
try:
del local.request_id
except AttributeError:
pass

def test_id_generation(self):
request = self.factory.get('/')
middleware = RequestIDMiddleware()
Expand All @@ -32,6 +41,17 @@ def test_external_id_in_http_header(self):
test_view(request)
self.assertTrue('some_request_id' in self.handler.messages[0])

def test_default_no_request_id_is_used(self):
request = self.factory.get('/')
test_view(request)
self.assertTrue(DEFAULT_NO_REQUEST_ID in self.handler.messages[0])

@override_settings(NO_REQUEST_ID='-')
def test_custom_request_id_is_used(self):
request = self.factory.get('/')
test_view(request)
self.assertTrue('[-]' in self.handler.messages[0])

def test_external_id_missing_in_http_header_should_fallback_to_generated_id(self):
with self.settings(LOG_REQUEST_ID_HEADER='REQUEST_ID_HEADER', GENERATE_REQUEST_ID_IF_NOT_IN_HEADER=True):
request = self.factory.get('/')
Expand Down Expand Up @@ -82,7 +102,6 @@ def setUp(self):

def test_request_id_passthrough_with_custom_header(self):
with self.settings(LOG_REQUEST_ID_HEADER='REQUEST_ID_HEADER', OUTGOING_REQUEST_ID_HEADER='OUTGOING_REQUEST_ID_HEADER'):
from log_request_id.session import Session
request = self.factory.get('/')
request.META['REQUEST_ID_HEADER'] = 'some_request_id'
middleware = RequestIDMiddleware()
Expand All @@ -98,7 +117,6 @@ def test_request_id_passthrough_with_custom_header(self):

def test_request_id_passthrough(self):
with self.settings(LOG_REQUEST_ID_HEADER='REQUEST_ID_HEADER'):
from log_request_id.session import Session
request = self.factory.get('/')
request.META['REQUEST_ID_HEADER'] = 'some_request_id'
middleware = RequestIDMiddleware()
Expand All @@ -114,6 +132,5 @@ def test_request_id_passthrough(self):

def test_misconfigured_for_sessions(self):
def inner():
from log_request_id.session import Session # noqa
Session()
self.assertRaises(ImproperlyConfigured, inner)

0 comments on commit 54d1afc

Please sign in to comment.