analytics.py |
|
---|---|
Record basic performance statistics. |
|
Imports |
from __future__ import unicode_literals
import time
from web.core.compat import unicode
|
Module Globals |
log = __import__('logging').getLogger(__name__)
|
Extension |
|
Record performance statistics about each request, and potentially a lot more. By default this extension adds a |
class AnalyticsExtension(object):
__slots__ = ('header', 'log')
first = True # We need this processing to happen as early as possible.
provides = ['analytics'] # Expose this symbol for other extensions to depend upon.
|
Executed to configure the extension. |
def __init__(self, header='X-Generation-Time', level='debug'):
super(AnalyticsExtension, self).__init__()
|
Record settings. |
self.header = header
self.log = getattr(log, level) if level else None
|
Request-Local Callabacks |
|
Executed during request set-up. |
def prepare(self, context):
context._start_time = None
|
Executed after all extension prepare methods have been called, prior to dispatch. |
def before(self, context):
context._start_time = time.time()
|
Executed after dispatch has returned and the response populated, prior to anything being sent to the client. |
def after(self, context, exc=None):
duration = time.time() - context._start_time
delta = unicode(round(duration, 5))
|
Default response augmentation. |
if self.header:
context.response.headers[self.header] = delta
if self.log:
self.log("Response generated in " + delta + " seconds.", extra=dict(
duration = duration,
request = id(context)
))
|