NewsBlur-viq/utils/redis_raw_log_middleware.py
Samuel Clay 0d6cb69548 Merge branch 'django1.11' into django2.0
* django1.11: (73 commits)
  Switching to new celery 4 standalone binary.
  Fixing various mongo data calls.
  Upgrading to latest celery 4 (holy moly), which required some big changes to project layout. Still needs supervisor scripts updated.
  Removing unused log on cookies.
  I believe this Context wrapping is still preserved. See this django ticket: https://code.djangoproject.com/ticket/28125. Reverting this fixes the error, so I'm assuming this is that type of render.
  Have to revert 3f122d5e03 because this broke existing sessions (logged me out) because the model has changed and the serialized model stored in redis no longer matches. Whew, this took a while to figure out.
  Upgrading redis cache.
  Adding cookies to path inspector.
  Removing dupe db log.
  Fixing missing DB logs (redis and mongo) due to this change in django 1.8: "connections.queries is now a read-only attribute."
  Removing migrations that set a default date of 2020-05-08. Not sure why this was committed. I thought we resolved the issue with default datetimes?
  Fixing CallableBool.
  Missing import
  Fixing runtime errors on django 1.10
  Fixing OAuth connect.
  Fixing various django1.9 issues, mainly around templates.
  BASE_DIR
  Not every story is from a feed.
  Styling background colors for newsletters.
  Styling more newsletter elements.
  ...
2020-06-30 12:34:59 -04:00

68 lines
No EOL
2.6 KiB
Python

from django.core.exceptions import MiddlewareNotUsed
from django.conf import settings
from django.db import connection
from redis.connection import Connection
from time import time
class RedisDumpMiddleware(object):
def __init__(self, get_response):
self.get_response = get_response
def activated(self, request):
return (settings.DEBUG_QUERIES or
(hasattr(request, 'activated_segments') and
'db_profiler' in request.activated_segments))
def process_view(self, request, callback, callback_args, callback_kwargs):
if not self.activated(request): return
if not getattr(Connection, '_logging', False):
# save old methods
setattr(Connection, '_logging', True)
Connection.pack_command = \
self._instrument(Connection.pack_command)
def process_celery(self, profiler):
if not self.activated(profiler): return
if not getattr(Connection, '_logging', False):
# save old methods
setattr(Connection, '_logging', True)
Connection.pack_command = \
self._instrument(Connection.pack_command)
def process_response(self, request, response):
# if settings.DEBUG and hasattr(self, 'orig_pack_command'):
# # remove instrumentation from redis
# setattr(Connection, '_logging', False)
# Connection.pack_command = \
# self.orig_pack_command
return response
def _instrument(self, original_method):
def instrumented_method(*args, **kwargs):
message = self.process_message(*args, **kwargs)
if not message:
return original_method(*args, **kwargs)
start = time()
result = original_method(*args, **kwargs)
stop = time()
duration = stop - start
if not getattr(connection, 'queriesx', False):
connection.queriesx = []
connection.queriesx.append({
'redis': message,
'time': '%.3f' % duration,
})
return result
return instrumented_method
def process_message(self, *args, **kwargs):
query = []
for a, arg in enumerate(args):
if isinstance(arg, Connection):
continue
if len(str(arg)) > 100:
arg = "[%s bytes]" % len(str(arg))
query.append(str(arg).replace('\n', ''))
return { 'query': ' '.join(query) }
def __call__(self, request):
response = self.get_response(request)
return response