diff --git a/djangae/core/logging.py b/djangae/core/logging.py index b11849c3ac548114a606680850170f2f00552e5c..33c5c4c7e7adb8985f9c8ce02d4bb61e905674da 100644 --- a/djangae/core/logging.py +++ b/djangae/core/logging.py @@ -4,32 +4,22 @@ from django.conf import settings from django.core.exceptions import ImproperlyConfigured from google.cloud import logging -from google.cloud.logging_v2.handlers._helpers import ( - _DJANGO_TRACE_HEADER, - _parse_trace_span, -) -from google.cloud.logging_v2.handlers.app_engine import ( - _TRACE_ID_LABEL, - AppEngineHandler, -) - -from djangae.contrib.common import get_request +from google.cloud.logging_v2.handlers.handlers import CloudLoggingHandler _client = None _client_lock = threading.Lock() -_DJANGAE_MIDDLEWARE_NAME = "djangae.contrib.common.middleware.RequestStorageMiddleware" +_DJANGAE_MIDDLEWARE_NAME = 'google.cloud.logging_v2.handlers.middleware.request.RequestMiddleware' -class DjangaeLoggingHandler(AppEngineHandler): +class DjangaeLoggingHandler(CloudLoggingHandler): """ This grabs the trace_id from Djangae's request middleware for log grouping """ def __init__(self, *args, **kwargs): - global _client_lock global _client if _DJANGAE_MIDDLEWARE_NAME not in settings.MIDDLEWARE: @@ -37,38 +27,14 @@ class DjangaeLoggingHandler(AppEngineHandler): "You must install the %s middleware to use the DjangaeLoggingHandler" % _DJANGAE_MIDDLEWARE_NAME ) - # We use a lock here to avoid the potential race condition between + # We use a double-checked lock here to avoid the potential race condition between # checking to see if the client was initialised, and it actually being # initialized. - with _client_lock: - if not _client: - _client = logging.Client() - _client.setup_logging() + if not _client: + with _client_lock: + if not _client: + _client = logging.Client() + _client.setup_logging() kwargs.setdefault("client", _client) super().__init__(*args, **kwargs) - - def get_gae_labels(self): - gae_labels = {} - - trace_id, _ = self.get_trace_and_span_id_from_djangae() - if trace_id is not None: - gae_labels[_TRACE_ID_LABEL] = trace_id - - return gae_labels - - def get_trace_and_span_id_from_djangae(self): - request = get_request() - - if request is None: - return None, None - - # find trace id and span id - header = request.META.get(_DJANGO_TRACE_HEADER) - trace_id, span_id = _parse_trace_span(header) - - return trace_id, span_id - - def emit(self, record): - record.trace, record.span_id = self.get_trace_and_span_id_from_djangae() - return super().emit(record) diff --git a/setup.py b/setup.py index 3b22890d0dde96b7626370edffcbc7b41d6d695e..5f6f408c0f2b1ba644950f3b3c63983aa6782fa7 100644 --- a/setup.py +++ b/setup.py @@ -45,7 +45,7 @@ setup( 'django-gcloud-connectors>=0.3.5', 'google-api-python-client>=2.27.0', 'google-cloud-tasks>=1.5.0,<2.0.0', - 'google-cloud-logging>=2.7.0,<3.0.0', + 'google-cloud-logging>=3.0.0', 'psutil>=5.7.3', # requests required by cloud storage file backend 'requests>=2.22.0',