2016-08-02 14:23:47 +01:00
|
|
|
import functools
|
|
|
|
|
|
|
|
|
|
from app import statsd_client
|
|
|
|
|
from flask import current_app
|
|
|
|
|
from monotonic import monotonic
|
|
|
|
|
|
|
|
|
|
|
2016-08-05 10:44:43 +01:00
|
|
|
def statsd(namespace):
|
2016-08-02 14:23:47 +01:00
|
|
|
def time_function(func):
|
|
|
|
|
@functools.wraps(func)
|
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
|
start_time = monotonic()
|
2017-03-17 12:38:24 +00:00
|
|
|
try:
|
|
|
|
|
res = func(*args, **kwargs)
|
|
|
|
|
elapsed_time = monotonic() - start_time
|
2017-03-17 13:33:57 +00:00
|
|
|
statsd_client.incr('{namespace}.{func}'.format(
|
|
|
|
|
namespace=namespace, func=func.__name__)
|
|
|
|
|
)
|
|
|
|
|
statsd_client.timing('{namespace}.{func}'.format(
|
|
|
|
|
namespace=namespace, func=func.__name__), elapsed_time
|
|
|
|
|
)
|
2017-03-17 12:38:24 +00:00
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
current_app.logger.error(
|
|
|
|
|
"{namespace} call {func} failed".format(
|
|
|
|
|
namespace=namespace, func=func.__name__
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
raise e
|
|
|
|
|
else:
|
|
|
|
|
current_app.logger.info(
|
|
|
|
|
"{namespace} call {func} took {time}".format(
|
|
|
|
|
namespace=namespace, func=func.__name__, time="{0:.4f}".format(elapsed_time)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
return res
|
2016-08-02 14:23:47 +01:00
|
|
|
wrapper.__wrapped__.__name__ = func.__name__
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
|
return time_function
|