|
|
@@ -22,6 +22,8 @@ This assumes a single-threaded server.
|
|
|
|
|
|
import logging
|
|
|
import re
|
|
|
+import resource
|
|
|
+import sys
|
|
|
import threading
|
|
|
import time
|
|
|
|
|
|
@@ -81,15 +83,29 @@ class AccessInfo(dict):
|
|
|
self['agent'] = request.META.get('HTTP_USER_AGENT', '-')
|
|
|
self['time'] = time.time()
|
|
|
self['duration'] = None
|
|
|
+ self['memory'] = None
|
|
|
+
|
|
|
+ def memory_usage_resource(self):
|
|
|
+ """
|
|
|
+ This is a lightweight way to get the total peak memory as
|
|
|
+ doing the diffing before/after request with guppy was too inconsistent and memory intensive.
|
|
|
+ """
|
|
|
+ rusage_denom = 1024
|
|
|
+ if sys.platform == 'darwin':
|
|
|
+ rusage_denom = rusage_denom * 1024
|
|
|
+ # get peak memory usage, bytes on OSX, Kilobytes on Linux
|
|
|
+ return resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / rusage_denom
|
|
|
|
|
|
def log(self, level, msg=None, start_time=None):
|
|
|
- self['duration'] = ' returned in %dms' % ((time.time() - start_time) * 1000) if start_time is not None else ''
|
|
|
+ is_instrumentation = desktop.conf.INSTRUMENTATION.get()
|
|
|
+ self['duration'] = ' returned in %dms' % ((time.time() - start_time) * 1000) if start_time is not None and is_instrumentation else ''
|
|
|
+ self['memory'] = ' (mem: %dmb)' % self.memory_usage_resource() if is_instrumentation else ''
|
|
|
|
|
|
if msg is not None:
|
|
|
self['msg'] = msg
|
|
|
- ACCESS_LOG.log(level, '%(remote_ip)s %(username)s - "%(method)s %(path)s %(proto)s"%(duration)s -- %(msg)s' % self)
|
|
|
+ ACCESS_LOG.log(level, '%(remote_ip)s %(username)s - "%(method)s %(path)s %(proto)s"%(duration)s%(memory)s-- %(msg)s' % self)
|
|
|
else:
|
|
|
- ACCESS_LOG.log(level, '%(remote_ip)s %(username)s - "%(method)s %(path)s %(proto)s"%(duration)s' % self)
|
|
|
+ ACCESS_LOG.log(level, '%(remote_ip)s %(username)s - "%(method)s %(path)s %(proto)s"%(duration)s%(memory)s' % self)
|
|
|
|
|
|
def add_to_access_history(self, app):
|
|
|
"""Record this user access to the recent access map"""
|