-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_logger.py
124 lines (100 loc) · 3.21 KB
/
redis_logger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# -*- coding: utf-8 -*-
from datetime import datetime
from django.db.backends import BaseDatabaseWrapper
from debug_toolbar.panels import DebugPanel
from debug_toolbar.middleware import DebugToolbarMiddleware
from debug_toolbar.utils import ms_from_timedelta
from debug_toolbar.utils.tracking import replace_call
import types
import inspect
import redis
from redis import StrictRedis
# def replace_call(func):
# def inner(callback):
# def wrapped(*args,**kwargs):
# print 'wrapped'
# print 'callback',callback
# print 'f',func
# print 'args',args
# print 'kwargs',kwargs
# print '**************'
# return callback(func,*args,**kwargs)
# actual = getattr(func,'__wrapped__',func)
# wrapped.__wrapped__ = actual
# wrapped.__doc__ = getattr(actual,'__doc__',None)
# wrapped.__name__ = actual.__name__
# print 'actual',actual
# print 'wrapped',wrapped
# _replace_function(func,wrapped)
# return wrapped
# return inner
#
# def _replace_function(func,wrapped):
# print 'replace'
# print 'func',func
# print dir(func)
# if isinstance(func,types.FunctionType):
# pass
# elif getattr(func,'im_self',None):
# pass
# elif hasattr(func,'im_class'):
# setattr(func.im_class,func.__name__,wrapped)
# else:
# raise
# print '*******'
def _get_func_info():
curframe = inspect.currentframe()
calframe = inspect.getouterframes(curframe,2)
return calframe
@replace_call(redis.StrictRedis.execute_command)
def execute_command(func,self,*args,**options):
command = args[0]
start = datetime.now()
result = func(self,*args,**options)
stop = datetime.now()
duration = ms_from_timedelta(stop - start)
#TODO find more better way to get the calling func info
calframe = _get_func_info()
params = {
'func':calframe[4][3],
'func_path':"{}:{}".format(calframe[4][1],calframe[4][2]),
'command':command,
'result':result,
'start_time':start,
'stop_time':stop,
'duration':duration,
'is_slow':None,
}
#TODO more better way to loggging?
djdt = DebugToolbarMiddleware.get_current()
if not djdt:
return result
logger = djdt.get_panel(RedisDebugPanel)
logger.record(**params)
return result
class RedisDebugPanel(DebugPanel):
name = 'Redis'
template = 'debug_toolbar/panels/redis.html'
has_content = True
def __init__(self,*args,**kwargs):
super(RedisDebugPanel, self).__init__(*args, **kwargs)
self._keys = list()
self._num_commands = 0
self._total_time = 0
def record(self,**kwargs):
self._keys.append(kwargs)
self._total_time += kwargs['duration']
self._num_commands += 1
def title(self):
return 'REDIS LOGGER'
def nav_title(self):
return 'REDIS LOGGER'
def url(self):
return ''
def process_response(self,request,response):
if self._keys:
self.record_stats({
'keys':self._keys,
'total_time':self._total_time,
'commands_num':self._num_commands,
})