2012-07-25 17:55:23 -07:00
|
|
|
from django.core.exceptions import MiddlewareNotUsed
|
|
|
|
from django.conf import settings
|
|
|
|
from django.db import connection
|
|
|
|
from redis.connection import Connection
|
2022-06-24 14:04:36 -04:00
|
|
|
from redis.client import Redis, Pipeline
|
2012-07-25 17:55:23 -07:00
|
|
|
from time import time
|
2022-06-24 14:04:36 -04:00
|
|
|
from pprint import pprint
|
2012-07-25 17:55:23 -07:00
|
|
|
|
2020-06-17 03:24:16 -04:00
|
|
|
class RedisDumpMiddleware(object):
|
|
|
|
|
2020-07-01 18:38:37 -04:00
|
|
|
def __init__(self, get_response=None):
|
2020-06-17 03:24:16 -04:00
|
|
|
self.get_response = get_response
|
|
|
|
|
2014-03-18 19:19:43 -07:00
|
|
|
def activated(self, request):
|
2014-05-20 12:21:17 -07:00
|
|
|
return (settings.DEBUG_QUERIES or
|
2014-03-19 14:59:07 -07:00
|
|
|
(hasattr(request, 'activated_segments') and
|
|
|
|
'db_profiler' in request.activated_segments))
|
2020-06-30 19:46:32 -04:00
|
|
|
|
2012-07-25 17:55:23 -07:00
|
|
|
def process_view(self, request, callback, callback_args, callback_kwargs):
|
2014-03-18 19:19:43 -07:00
|
|
|
if not self.activated(request): return
|
|
|
|
if not getattr(Connection, '_logging', False):
|
2012-07-25 17:55:23 -07:00
|
|
|
# save old methods
|
2013-07-31 11:17:04 -07:00
|
|
|
setattr(Connection, '_logging', True)
|
2022-03-07 15:57:51 -05:00
|
|
|
connection.queriesx = []
|
2022-06-24 14:04:36 -04:00
|
|
|
Redis.execute_command = \
|
|
|
|
self._instrument(Redis.execute_command)
|
|
|
|
Pipeline._execute_transaction = \
|
|
|
|
self._instrument_pipeline(Pipeline._execute_transaction)
|
2020-06-30 19:46:32 -04:00
|
|
|
|
2015-12-16 17:31:41 -08:00
|
|
|
def process_celery(self, profiler):
|
|
|
|
if not self.activated(profiler): return
|
|
|
|
if not getattr(Connection, '_logging', False):
|
|
|
|
# save old methods
|
|
|
|
setattr(Connection, '_logging', True)
|
2022-06-24 14:04:36 -04:00
|
|
|
Redis.execute_command = \
|
|
|
|
self._instrument(Redis.execute_command)
|
|
|
|
Pipeline._execute_transaction = \
|
|
|
|
self._instrument_pipeline(Pipeline._execute_transaction)
|
2020-06-30 19:46:32 -04:00
|
|
|
|
2012-07-25 17:55:23 -07:00
|
|
|
def process_response(self, request, response):
|
2013-07-31 11:17:04 -07:00
|
|
|
# if settings.DEBUG and hasattr(self, 'orig_pack_command'):
|
|
|
|
# # remove instrumentation from redis
|
|
|
|
# setattr(Connection, '_logging', False)
|
|
|
|
# Connection.pack_command = \
|
|
|
|
# self.orig_pack_command
|
2012-07-25 17:55:23 -07:00
|
|
|
return response
|
2020-06-30 19:46:32 -04:00
|
|
|
|
2012-07-25 17:55:23 -07:00
|
|
|
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
|
2020-06-26 15:56:06 -04:00
|
|
|
if not getattr(connection, 'queriesx', False):
|
|
|
|
connection.queriesx = []
|
|
|
|
connection.queriesx.append({
|
2021-08-03 16:05:48 -04:00
|
|
|
message['redis_server_name']: message,
|
2021-07-02 12:42:02 -04:00
|
|
|
'time': '%.6f' % duration,
|
2012-07-25 17:55:23 -07:00
|
|
|
})
|
|
|
|
return result
|
|
|
|
return instrumented_method
|
2022-06-24 14:04:36 -04:00
|
|
|
|
|
|
|
def _instrument_pipeline(self, original_method):
|
|
|
|
def instrumented_method(*args, **kwargs):
|
|
|
|
message = self.process_pipeline(*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({
|
|
|
|
message['redis_server_name']: message,
|
|
|
|
'time': '%.6f' % duration,
|
|
|
|
})
|
|
|
|
return result
|
|
|
|
return instrumented_method
|
2020-06-17 03:24:16 -04:00
|
|
|
|
2012-07-25 17:55:23 -07:00
|
|
|
def process_message(self, *args, **kwargs):
|
2013-10-08 10:05:28 -07:00
|
|
|
query = []
|
2021-08-03 16:05:48 -04:00
|
|
|
redis_server_name = None
|
2013-10-08 10:05:28 -07:00
|
|
|
for a, arg in enumerate(args):
|
2022-06-24 14:04:36 -04:00
|
|
|
if isinstance(arg, Redis):
|
2021-08-03 16:05:48 -04:00
|
|
|
redis_connection = arg
|
2022-06-24 14:04:36 -04:00
|
|
|
redis_server_name = redis_connection.connection_pool.connection_kwargs['host']
|
2021-08-03 16:51:11 -04:00
|
|
|
if 'db-redis-user' in redis_server_name:
|
|
|
|
redis_server_name = 'redis_user'
|
|
|
|
elif 'db-redis-session' in redis_server_name:
|
|
|
|
redis_server_name = 'redis_session'
|
|
|
|
elif 'db-redis-story' in redis_server_name:
|
|
|
|
redis_server_name = 'redis_story'
|
2021-08-03 17:19:45 -04:00
|
|
|
elif 'db-redis-pubsub' in redis_server_name:
|
|
|
|
redis_server_name = 'redis_pubsub'
|
2022-05-10 20:58:47 -04:00
|
|
|
elif 'db_redis' in redis_server_name:
|
|
|
|
redis_server_name = 'redis_user'
|
2013-10-08 10:05:28 -07:00
|
|
|
continue
|
2013-10-08 10:10:07 -07:00
|
|
|
if len(str(arg)) > 100:
|
2014-12-01 16:31:13 -08:00
|
|
|
arg = "[%s bytes]" % len(str(arg))
|
2013-10-08 10:05:28 -07:00
|
|
|
query.append(str(arg).replace('\n', ''))
|
2022-06-24 14:04:36 -04:00
|
|
|
return { 'query': f"{redis_server_name}: {' '.join(query)}", 'redis_server_name': redis_server_name }
|
|
|
|
|
|
|
|
def process_pipeline(self, *args, **kwargs):
|
|
|
|
queries = []
|
|
|
|
redis_server_name = None
|
|
|
|
for a, arg in enumerate(args):
|
|
|
|
if isinstance(arg, Connection):
|
|
|
|
continue
|
|
|
|
if isinstance(arg, Pipeline):
|
|
|
|
redis_connection = arg
|
|
|
|
redis_server_name = redis_connection.connection_pool.connection_kwargs['host']
|
|
|
|
if 'db-redis-user' in redis_server_name:
|
|
|
|
redis_server_name = 'redis_user'
|
|
|
|
elif 'db-redis-session' in redis_server_name:
|
|
|
|
redis_server_name = 'redis_session'
|
|
|
|
elif 'db-redis-story' in redis_server_name:
|
|
|
|
redis_server_name = 'redis_story'
|
|
|
|
elif 'db-redis-pubsub' in redis_server_name:
|
|
|
|
redis_server_name = 'redis_pubsub'
|
|
|
|
elif 'db_redis' in redis_server_name:
|
|
|
|
redis_server_name = 'redis_user'
|
|
|
|
continue
|
|
|
|
if not isinstance(arg, list):
|
|
|
|
continue
|
|
|
|
for command in arg:
|
|
|
|
command_query = " ".join([str(c) for c in command[0]])
|
|
|
|
queries.append(command_query)
|
|
|
|
if len(str(arg)) > 10000:
|
|
|
|
arg = "[%s bytes]" % len(str(arg))
|
|
|
|
# query.append(str(arg).replace('\n', ''))
|
|
|
|
queries_str = '\n\t\t\t\t\t\t~FC'.join(queries)
|
|
|
|
return { 'query': f"{redis_server_name}: {queries_str}", 'redis_server_name': redis_server_name }
|
2016-05-16 17:57:02 -07:00
|
|
|
|
2020-06-17 03:24:16 -04:00
|
|
|
def __call__(self, request):
|
2020-06-30 19:46:32 -04:00
|
|
|
response = None
|
|
|
|
if hasattr(self, 'process_request'):
|
|
|
|
response = self.process_request(request)
|
|
|
|
if not response:
|
|
|
|
response = self.get_response(request)
|
|
|
|
if hasattr(self, 'process_response'):
|
|
|
|
response = self.process_response(request, response)
|
2020-06-17 03:24:16 -04:00
|
|
|
|
2020-06-30 19:46:32 -04:00
|
|
|
return response
|