Skip to content

Instantly share code, notes, and snippets.

@sivy
Created March 24, 2011 19:16
Show Gist options
  • Save sivy/885656 to your computer and use it in GitHub Desktop.
Save sivy/885656 to your computer and use it in GitHub Desktop.
from functools import wraps
def recordstats(stat_prefix=None):
"""Record that this function was called, with duration
Usage:
@recordstats('stats_prefix')
def my_view(request):
return HTTPResponse(the_content)
This will send a metric to statsd with the name 'stats_prefix.my_view'. Omitting the
argument to recordstats() will result in a metric simply named after the wrapped
function, in this case 'my_view'.
This will also send a time metric to statsd with the name 'stats_prefix.my_view' (or
'my_view') with the duration of the call.
"""
print "recordstats"
def stat_name_wrapper(*args, **kwargs):
print "stat_name_wrapper"
print args
print kwargs
f = args[0]
def stat_wrapper(*args, **kwargs):
print "stat_wrapper"
print args
print kwargs
if stat_prefix:
stat_name = "%s.%s" % (stat_prefix, f.__name__)
else:
stat_name = f.__name__
start = datetime.now()
stats.incrstat(stat_name)
result = f(*args, **kwargs)
end = datetime.now()
millis = (end - start).microseconds/1000
stats.timestat(stat_name, millis)
return result
return wraps(f)(stat_wrapper)
return stat_name_wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment