| 
									
										
										
										
											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 | 
					
						
							|  |  |  | from time import time | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-18 19:19:43 -07:00
										 |  |  | class RedisDumpMiddleware(object):     | 
					
						
							|  |  |  |     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)) | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							| 
									
										
										
										
											2015-12-16 17:31:41 -08:00
										 |  |  |             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) | 
					
						
							| 
									
										
										
										
											2012-07-25 17:55:23 -07:00
										 |  |  |             Connection.pack_command = \ | 
					
						
							|  |  |  |                     self._instrument(Connection.pack_command) | 
					
						
							|  |  |  |     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 | 
					
						
							|  |  |  |     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 | 
					
						
							|  |  |  |             connection.queries.append({ | 
					
						
							|  |  |  |                 'redis': message, | 
					
						
							|  |  |  |                 'time': '%.3f' % duration, | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |             return result | 
					
						
							|  |  |  |         return instrumented_method | 
					
						
							|  |  |  |     def process_message(self, *args, **kwargs): | 
					
						
							| 
									
										
										
										
											2013-10-08 10:05:28 -07:00
										 |  |  |         query = [] | 
					
						
							|  |  |  |         for a, arg in enumerate(args): | 
					
						
							|  |  |  |             if isinstance(arg, Connection): | 
					
						
							|  |  |  |                 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', '')) | 
					
						
							| 
									
										
										
										
											2016-05-16 17:57:02 -07:00
										 |  |  |         return { 'query': ' '.join(query) } | 
					
						
							|  |  |  | 
 |