Last active
December 15, 2015 10:38
-
-
Save z4r/5246687 to your computer and use it in GitHub Desktop.
stopwatch core
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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