Merge pull request #671 from alphagov/statsd-time-taken-to-be-delivered

Statsd time taken to be delivered
This commit is contained in:
minglis
2016-09-14 14:53:01 +01:00
committed by GitHub
7 changed files with 240 additions and 105 deletions

View File

@@ -24,3 +24,8 @@ class StatsdClient(StatsClient):
def timing(self, stat, delta, rate=1):
if self.active:
super(StatsClient, self).timing(self.format_stat_name(stat), delta, rate)
def timing_with_dates(self, stat, start, end, rate=1):
if self.active:
delta = (start - end).total_seconds()
super(StatsClient, self).timing(stat, delta, rate)

View File

@@ -143,7 +143,7 @@ def _update_notification_status(notification, status):
status = _decide_permanent_temporary_failure(current_status=notification.status, status=status)
notification.status = status
dao_update_notification(notification)
return True
return notification
@statsd(namespace="dao")
@@ -156,7 +156,7 @@ def update_notification_status_by_id(notification_id, status):
Notification.status == 'pending')).first()
if not notification:
return False
return None
return _update_notification_status(
notification=notification,
@@ -173,7 +173,7 @@ def update_notification_status_by_reference(reference, status):
Notification.status == 'pending')).first()
if not notification:
return False
return None
return _update_notification_status(
notification=notification,

View File

@@ -1,4 +1,6 @@
import uuid
from datetime import datetime
from flask import current_app
from app import statsd_client
@@ -54,7 +56,8 @@ def process_sms_client_response(status, reference, client_name):
notification_success = response_dict['success']
# record stats
if not notifications_dao.update_notification_status_by_id(reference, notification_status):
notification = notifications_dao.update_notification_status_by_id(reference, notification_status)
if not notification:
status_error = "{} callback failed: notification {} either not found or already updated " \
"from sending. Status {}".format(client_name,
reference,
@@ -68,5 +71,11 @@ def process_sms_client_response(status, reference, client_name):
notification_status_message))
statsd_client.incr('callback.{}.{}'.format(client_name.lower(), notification_status))
if notification.sent_at:
statsd_client.timing_with_dates(
'callback.{}.elapsed-time'.format(client_name.lower()),
datetime.utcnow(),
notification.sent_at
)
success = "{} callback succeeded. reference {} updated".format(client_name, reference)
return success, errors

View File

@@ -79,10 +79,11 @@ def process_ses_response():
try:
reference = ses_message['mail']['messageId']
if not notifications_dao.update_notification_status_by_reference(
reference,
notification_status
):
notification = notifications_dao.update_notification_status_by_reference(
reference,
notification_status
)
if not notification:
error = "SES callback failed: notification either not found or already updated " \
"from sending. Status {}".format(notification_status)
raise InvalidRequest(error, status_code=404)
@@ -96,6 +97,12 @@ def process_ses_response():
)
statsd_client.incr('callback.ses.{}'.format(notification_status))
if notification.sent_at:
statsd_client.timing_with_dates(
'callback.ses.elapsed-time'.format(client_name.lower()),
datetime.utcnow(),
notification.sent_at
)
return jsonify(
result="success", message="SES callback succeeded"
), 200
@@ -231,8 +238,8 @@ def send_notification(notification_type):
raise InvalidRequest(errors, status_code=400)
if (
template_object.template_type == SMS_TYPE and
template_object.replaced_content_count > current_app.config.get('SMS_CHAR_COUNT_LIMIT')
template_object.template_type == SMS_TYPE and
template_object.replaced_content_count > current_app.config.get('SMS_CHAR_COUNT_LIMIT')
):
char_count = current_app.config.get('SMS_CHAR_COUNT_LIMIT')
message = 'Content has a character count greater than the limit of {}'.format(char_count)
@@ -240,14 +247,14 @@ def send_notification(notification_type):
raise InvalidRequest(errors, status_code=400)
if all((
api_user.key_type != KEY_TYPE_TEST,
service.restricted or api_user.key_type == KEY_TYPE_TEAM,
not allowed_to_send_to(
notification['to'],
itertools.chain.from_iterable(
[user.mobile_number, user.email_address] for user in service.users
)
api_user.key_type != KEY_TYPE_TEST,
service.restricted or api_user.key_type == KEY_TYPE_TEAM,
not allowed_to_send_to(
notification['to'],
itertools.chain.from_iterable(
[user.mobile_number, user.email_address] for user in service.users
)
)
)):
if (api_user.key_type == KEY_TYPE_TEAM):
message = 'Cant send to this recipient using a team-only API key'