Skip to content

Instantly share code, notes, and snippets.

@dcramer
Created May 22, 2013 05:40
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 dcramer/5625467 to your computer and use it in GitHub Desktop.
Save dcramer/5625467 to your computer and use it in GitHub Desktop.
import inspect
import time
from django_statsd.clients import statsd
from django.http import Http404
class GraphiteResponseCodeMiddleware(object):
def process_response(self, request, response):
statsd.incr('response.%s' % response.status_code)
return response
def process_exception(self, request, exception):
if not isinstance(exception, Http404):
statsd.incr('response.500')
class GraphiteRequestTimingMiddleware(object):
"""statsd's timing data per view."""
allowed_methods = ('POST', 'GET')
allowed_paths = (
'sentry.web.api.StoreView',
# 'sentry.web.frontend.generic.dashboard',
# 'sentry.web.frontend.groups.dashboard',
# 'sentry.web.frontend.groups.group',
# 'sentry.web.frontend.groups.group_list',
)
def process_view(self, request, view_func, view_args, view_kwargs):
if request.method not in self.allowed_methods:
return
view = view_func
if not inspect.isfunction(view_func):
view = view.__class__
try:
path = '%s.%s' % (view.__module__, view.__name__)
except AttributeError:
pass
if not path.startswith(self.allowed_paths):
return
request._view_path = path
request._start_time = time.time()
def process_response(self, request, response):
self._record_time(request, response.status_code)
return response
def process_exception(self, request, exception):
self._record_time(request, 500)
def _record_time(self, request, status_code):
if not hasattr(request, '_view_path'):
return
statsd.incr('view.{path}.{status_code}'.format(
path=request._view_path, status_code=status_code), 1)
if not hasattr(request, '_start_time'):
return
ms = int((time.time() - request._start_time) * 1000)
statsd.timing('view.{path}.{method}'.format(
path=request._view_path, method=request.method), ms)
statsd.timing('view', ms)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment