
from __future__ import unicode_literals

import logging
import time

logger = logging.getLogger(__name__)


class RequestLoggerMiddleware(object):
    def process_request(self, request):
        self.request_time = time.time()
        logger.info("{method} {path}".format(
            method=request.method,
            path=request.get_full_path()),
            extra={
                'request_method': request.method,
                'http_host': request.META.get('HTTP_HOST'),
                'http_path': request.get_full_path(),
                'request_id': request.META.get('HTTP_REQUEST_ID')
            })

    def process_response(self, request, response):
        elapsed_time = time.time() - self.request_time
        logger.info("{method} {path} : {status} {secs:.6f}s".format(
            method=request.method,
            path=request.get_full_path(),
            status=response.status_code,
            secs=elapsed_time),
            extra={
                'request_method': request.method,
                'http_host': request.META.get('HTTP_HOST'),
                'http_path': request.get_full_path(),
                'status': response.status_code,
                'request_time': elapsed_time,
            })
        return response


from __future__ import unicode_literals

import logging
import time

logger = logging.getLogger(__name__)


class RequestLoggerMiddleware(object):
    def process_request(self, request):
        request.start_request_time = time.time()
        logger.info("{method} {path}".format(
            method=request.method,
            path=request.get_full_path()),
            extra={
                'request_method': request.method,
                'http_host': request.META.get('HTTP_HOST'),
                'http_path': request.get_full_path(),
                'request_id': request.META.get('HTTP_REQUEST_ID')
            })

    def process_response(self, request, response):
        if hasattr(request, 'start_request_time'):
            elapsed_time = time.time() - request.start_request_time
            logger.info("{method} {path} : {status} {secs:.6f}s".format(
                method=request.method,
                path=request.get_full_path(),
                status=response.status_code,
                secs=elapsed_time),
                extra={
                    'request_method': request.method,
                    'http_host': request.META.get('HTTP_HOST'),
                    'http_path': request.get_full_path(),
                    'status': response.status_code,
                    'request_time': elapsed_time,
                })
        return response

Fix thread-safety issue in stagecraft
Fix thread-safety issue in stagecraft

Django middleware is not thread-safe. We should store this context on
the request object instance.

Django always calls `process_response`, but it is possible that
`process_request` has been skipped. So we have a guard checking that
it’s safe to log the response time.

See https://docs.djangoproject.com/en/1.7/topics/http/middleware/#process_request
  
  from __future__ import unicode_literals
  
  import logging
  import time
  
  logger = logging.getLogger(__name__)
  
  
  class RequestLoggerMiddleware(object):
      def process_request(self, request):
-         self.request_time = time.time()
+         request.start_request_time = time.time()
          logger.info("{method} {path}".format(
              method=request.method,
              path=request.get_full_path()),
              extra={
                  'request_method': request.method,
                  'http_host': request.META.get('HTTP_HOST'),
                  'http_path': request.get_full_path(),
                  'request_id': request.META.get('HTTP_REQUEST_ID')
              })
  
      def process_response(self, request, response):
+         if hasattr(request, 'start_request_time'):
-         elapsed_time = time.time() - self.request_time
+             elapsed_time = time.time() - request.start_request_time
-         logger.info("{method} {path} : {status} {secs:.6f}s".format(
+             logger.info("{method} {path} : {status} {secs:.6f}s".format(
-             method=request.method,
+                 method=request.method,
-             path=request.get_full_path(),
+                 path=request.get_full_path(),
-             status=response.status_code,
+                 status=response.status_code,
-             secs=elapsed_time),
+                 secs=elapsed_time),
-             extra={
+                 extra={
-                 'request_method': request.method,
+                     'request_method': request.method,
-                 'http_host': request.META.get('HTTP_HOST'),
+                     'http_host': request.META.get('HTTP_HOST'),
-                 'http_path': request.get_full_path(),
+                     'http_path': request.get_full_path(),
-                 'status': response.status_code,
+                     'status': response.status_code,
-                 'request_time': elapsed_time,
+                     'request_time': elapsed_time,
-             })
+                 })
          return response
  
Fix thread-safety issue in stagecraft
## Code Before:

from __future__ import unicode_literals

import logging
import time

logger = logging.getLogger(__name__)


class RequestLoggerMiddleware(object):
    def process_request(self, request):
        self.request_time = time.time()
        logger.info("{method} {path}".format(
            method=request.method,
            path=request.get_full_path()),
            extra={
                'request_method': request.method,
                'http_host': request.META.get('HTTP_HOST'),
                'http_path': request.get_full_path(),
                'request_id': request.META.get('HTTP_REQUEST_ID')
            })

    def process_response(self, request, response):
        elapsed_time = time.time() - self.request_time
        logger.info("{method} {path} : {status} {secs:.6f}s".format(
            method=request.method,
            path=request.get_full_path(),
            status=response.status_code,
            secs=elapsed_time),
            extra={
                'request_method': request.method,
                'http_host': request.META.get('HTTP_HOST'),
                'http_path': request.get_full_path(),
                'status': response.status_code,
                'request_time': elapsed_time,
            })
        return response

## Instruction:
Fix thread-safety issue in stagecraft
## Code After:

from __future__ import unicode_literals

import logging
import time

logger = logging.getLogger(__name__)


class RequestLoggerMiddleware(object):
    def process_request(self, request):
        request.start_request_time = time.time()
        logger.info("{method} {path}".format(
            method=request.method,
            path=request.get_full_path()),
            extra={
                'request_method': request.method,
                'http_host': request.META.get('HTTP_HOST'),
                'http_path': request.get_full_path(),
                'request_id': request.META.get('HTTP_REQUEST_ID')
            })

    def process_response(self, request, response):
        if hasattr(request, 'start_request_time'):
            elapsed_time = time.time() - request.start_request_time
            logger.info("{method} {path} : {status} {secs:.6f}s".format(
                method=request.method,
                path=request.get_full_path(),
                status=response.status_code,
                secs=elapsed_time),
                extra={
                    'request_method': request.method,
                    'http_host': request.META.get('HTTP_HOST'),
                    'http_path': request.get_full_path(),
                    'status': response.status_code,
                    'request_time': elapsed_time,
                })
        return response
