From 284a264616c582f9d93263bd5d2be67b29996ca0 Mon Sep 17 00:00:00 2001 From: kingbuzzman Date: Mon, 13 Apr 2020 18:59:19 -0400 Subject: [PATCH 1/3] Adds the ability to change the default value of missing request_id --- README.md | 6 ++++++ log_request_id/__init__.py | 6 +++--- log_request_id/filters.py | 8 ++++++-- log_request_id/middleware.py | 6 +++--- log_request_id/session.py | 8 ++++---- log_request_id/tests.py | 21 ++++++++++++++++++++- 6 files changed, 42 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 5ae4761..556eaf1 100644 --- a/README.md +++ b/README.md @@ -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 -------------------- diff --git a/log_request_id/__init__.py b/log_request_id/__init__.py index 3b5b831..9a748ce 100644 --- a/log_request_id/__init__.py +++ b/log_request_id/__init__.py @@ -1,7 +1,6 @@ import threading - -__version__ = "1.4.1" +__version__ = "1.5.0" local = threading.local() @@ -9,7 +8,8 @@ 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' diff --git a/log_request_id/filters.py b/log_request_id/filters.py index 589811a..3fa035d 100644 --- a/log_request_id/filters.py +++ b/log_request_id/filters.py @@ -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 diff --git a/log_request_id/middleware.py b/log_request_id/middleware.py index 5ec63a0..1e715ac 100644 --- a/log_request_id/middleware.py +++ b/log_request_id/middleware.py @@ -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__) @@ -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 diff --git a/log_request_id/session.py b/log_request_id/session.py index 7abef35..f661edf 100644 --- a/log_request_id/session.py +++ b/log_request_id/session.py @@ -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): @@ -24,9 +24,9 @@ def prepare_request(self, request): try: request_id = local.request_id except AttributeError: - request_id = NO_REQUEST_ID + request_id = DEFAULT_NO_REQUEST_ID - if self.request_id_header and request_id != NO_REQUEST_ID: + if self.request_id_header and request_id != DEFAULT_NO_REQUEST_ID: request.headers[self.request_id_header] = request_id return super(Session, self).prepare_request(request) diff --git a/log_request_id/tests.py b/log_request_id/tests.py index ffd125d..04a55d0 100644 --- a/log_request_id/tests.py +++ b/log_request_id/tests.py @@ -1,8 +1,10 @@ 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.middleware import RequestIDMiddleware from testproject.views import test_view @@ -14,6 +16,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() @@ -32,6 +40,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('/') From 8e3419816357676c46b4281331fbc93cbc6a4138 Mon Sep 17 00:00:00 2001 From: Javier Buzzi Date: Thu, 16 Apr 2020 13:45:57 -0400 Subject: [PATCH 2/3] Update session.py --- log_request_id/session.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/log_request_id/session.py b/log_request_id/session.py index f661edf..55fd2ed 100644 --- a/log_request_id/session.py +++ b/log_request_id/session.py @@ -2,7 +2,8 @@ from django.core.exceptions import ImproperlyConfigured from requests import Session as BaseSession -from log_request_id import DEFAULT_NO_REQUEST_ID, OUTGOING_REQUEST_ID_HEADER_SETTING, REQUEST_ID_HEADER_SETTING, local +from log_request_id import DEFAULT_NO_REQUEST_ID, LOG_REQUESTS_NO_SETTING, OUTGOING_REQUEST_ID_HEADER_SETTING, \ + REQUEST_ID_HEADER_SETTING, local class Session(BaseSession): @@ -24,7 +25,7 @@ def prepare_request(self, request): try: request_id = local.request_id except AttributeError: - request_id = DEFAULT_NO_REQUEST_ID + request_id = getattr(settings, LOG_REQUESTS_NO_SETTING, DEFAULT_NO_REQUEST_ID) if self.request_id_header and request_id != DEFAULT_NO_REQUEST_ID: request.headers[self.request_id_header] = request_id From fb01ff1cfe656053f3a899442ca114a9c22563b4 Mon Sep 17 00:00:00 2001 From: kingbuzzman Date: Thu, 16 Apr 2020 14:17:33 -0400 Subject: [PATCH 3/3] Reworks logic --- log_request_id/session.py | 10 ++++------ log_request_id/tests.py | 4 +--- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/log_request_id/session.py b/log_request_id/session.py index 55fd2ed..e17f1b0 100644 --- a/log_request_id/session.py +++ b/log_request_id/session.py @@ -2,8 +2,7 @@ from django.core.exceptions import ImproperlyConfigured from requests import Session as BaseSession -from log_request_id import DEFAULT_NO_REQUEST_ID, LOG_REQUESTS_NO_SETTING, OUTGOING_REQUEST_ID_HEADER_SETTING, \ - REQUEST_ID_HEADER_SETTING, local +from log_request_id import DEFAULT_NO_REQUEST_ID, OUTGOING_REQUEST_ID_HEADER_SETTING, REQUEST_ID_HEADER_SETTING, local class Session(BaseSession): @@ -24,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 = getattr(settings, LOG_REQUESTS_NO_SETTING, DEFAULT_NO_REQUEST_ID) - - if self.request_id_header and request_id != DEFAULT_NO_REQUEST_ID: - request.headers[self.request_id_header] = request_id + pass return super(Session, self).prepare_request(request) diff --git a/log_request_id/tests.py b/log_request_id/tests.py index 04a55d0..ee93269 100644 --- a/log_request_id/tests.py +++ b/log_request_id/tests.py @@ -5,6 +5,7 @@ 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 @@ -101,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() @@ -117,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() @@ -133,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)