From d9592fc422172e2bb22a490e3858780491610aad Mon Sep 17 00:00:00 2001 From: cahrens Date: Thu, 8 Jun 2017 15:38:55 -0400 Subject: [PATCH] Make the code robust to the request cache being cleared. --- openedx/core/djangoapps/monitoring_utils/middleware.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/openedx/core/djangoapps/monitoring_utils/middleware.py b/openedx/core/djangoapps/monitoring_utils/middleware.py index 9db1006813..f0f5145ca8 100644 --- a/openedx/core/djangoapps/monitoring_utils/middleware.py +++ b/openedx/core/djangoapps/monitoring_utils/middleware.py @@ -114,7 +114,11 @@ class MonitoringMemoryMiddleware(object): """ Returns a formatted prefix for logging for the given request. """ - return u"{} request '{} {} {}'".format(prefix, request.method, request.path, self._cache[self.guid_key]) + # After a celery task runs, the request cache is cleared. So if celery + # tasks are running synchronously (CELERY_ALWAYS _EAGER), "guid_key" + # will no longer be in the request cache when process_response executes. + cached_guid = self._cache.get(self.guid_key) or u"without_guid" + return u"{} request '{} {} {}'".format(prefix, request.method, request.path, cached_guid) def _memory_data(self, log_prefix): """