Skip to content

Instantly share code, notes, and snippets.

@z4r
Last active December 15, 2015 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save z4r/5246687 to your computer and use it in GitHub Desktop.
Save z4r/5246687 to your computer and use it in GitHub Desktop.
stopwatch core
from time import time
from django.conf import settings
from statsd import Connection, Timer
class StatsDStopWatchMiddleware(object):
def __init__(self):
connection = Connection(
host=getattr(settings, 'STATSD_HOST', None),
port=getattr(settings, 'STATSD_PORT', None)
)
self.timer = Timer(
name=getattr(settings, 'STATSD_PREFIX', 'stopwatch'),
connection=connection,
)
def process_request(self, request):
request._stopwatch_start = time()
def process_response(self, request, response):
key = '.'.join(map(unicode, [
request.resolver_match.namespace,
request.resolver_match.url_name,
request.method,
response.status_code,
]))
timing = time() - request._stopwatch_start
self.timer.send(key, timing)
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment