Rewiring how we do statsd

- decorater added to the DAO for notifications
This commit is contained in:
Martyn Inglis
2016-08-02 14:23:47 +01:00
parent daf85e7787
commit 61aaa36f9b
5 changed files with 85 additions and 6 deletions

30
app/statsd_decorators.py Normal file
View File

@@ -0,0 +1,30 @@
import functools
from app import statsd_client
from flask import current_app
from monotonic import monotonic
def statsd_timer(namespace):
def time_function(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
start_time = monotonic()
res = func(*args, **kwargs)
elapsed_time = monotonic() - start_time
current_app.logger.info(
"{namespace} call {func} took {time}".format(
namespace=namespace, func=func.__name__, time="{0:.4f}".format(elapsed_time)
)
)
statsd_client.incr('notifications.{namespace}.{func}'.format(
namespace=namespace, func=func.__name__)
)
statsd_client.timing('notifications.{namespace}.{func}'.format(
namespace=namespace, func=func.__name__), elapsed_time
)
return res
wrapper.__wrapped__.__name__ = func.__name__
return wrapper
return time_function