mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-20 23:41:17 -05:00
Removed all existing statsd logging and replaced with: - statsd decorator. Infers the stat name from the decorated function call. Delegates statsd call to statsd client. Calls incr and timing for each decorated method. This is applied to all tasks and all dao methods that touch the notifications/notification_history tables - statsd client changed to prefix all stats with "notification.api." - Relies on https://github.com/alphagov/notifications-utils/pull/61 for request logging. Once integrated we pass the statsd client to the logger, allowing us to statsd all API calls. This passes in the start time and the method to be called (NOT the url) onto the global flask object. We then construct statsd counters and timers in the following way notifications.api.POST.notifications.send_notification.200 This should allow us to aggregate to the level of - API or ADMIN - POST or GET etc - modules - methods - status codes Finally we count the callbacks received from 3rd parties to mapped status.
31 lines
993 B
Python
31 lines
993 B
Python
import functools
|
|
|
|
from app import statsd_client
|
|
from flask import current_app
|
|
from monotonic import monotonic
|
|
|
|
|
|
def statsd(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('{namespace}.{func}'.format(
|
|
namespace=namespace, func=func.__name__)
|
|
)
|
|
statsd_client.timing('{namespace}.{func}'.format(
|
|
namespace=namespace, func=func.__name__), elapsed_time
|
|
)
|
|
return res
|
|
wrapper.__wrapped__.__name__ = func.__name__
|
|
return wrapper
|
|
|
|
return time_function
|