Merge pull request #309 from alphagov/statsd-integration

Statsd integration
This commit is contained in:
minglis
2016-05-16 12:29:14 +01:00
17 changed files with 243 additions and 84 deletions

View File

@@ -2,8 +2,9 @@ import itertools
from datetime import datetime
from flask import current_app
from monotonic import monotonic
from sqlalchemy.exc import SQLAlchemyError
from app import clients
from app import clients, statsd_client
from app.clients.email import EmailClientException
from app.clients.sms import SmsClientException
from app.dao.services_dao import dao_fetch_service_by_id
@@ -112,6 +113,7 @@ def delete_invitations():
@notify_celery.task(name="process-job")
def process_job(job_id):
task_start = monotonic()
start = datetime.utcnow()
job = dao_get_job_by_id(job_id)
@@ -191,6 +193,8 @@ def process_job(job_id):
current_app.logger.info(
"Job {} created at {} started at {} finished at {}".format(job_id, job.created_at, start, finished)
)
statsd_client.incr("notifications.tasks.process-job")
statsd_client.timing("notifications.tasks.process-job.task-time", monotonic() - task_start)
@notify_celery.task(name="remove-job")
@@ -202,6 +206,7 @@ def remove_job(job_id):
@notify_celery.task(name="send-sms")
def send_sms(service_id, notification_id, encrypted_notification, created_at):
task_start = monotonic()
notification = encryption.decrypt(encrypted_notification)
service = dao_fetch_service_by_id(service_id)
@@ -237,7 +242,11 @@ def send_sms(service_id, notification_id, encrypted_notification, created_at):
sent_by=provider.get_name(),
content_char_count=template.replaced_content_count
)
statsd_client.timing_with_dates(
"notifications.tasks.send-sms.queued-for",
sent_at,
datetime.strptime(created_at, DATETIME_FORMAT)
)
dao_create_notification(notification_db_object, TEMPLATE_TYPE_SMS, provider.get_name())
if restricted:
@@ -261,12 +270,15 @@ def send_sms(service_id, notification_id, encrypted_notification, created_at):
current_app.logger.info(
"SMS {} created at {} sent at {}".format(notification_id, created_at, sent_at)
)
statsd_client.incr("notifications.tasks.send-sms")
statsd_client.timing("notifications.tasks.send-sms.task-time", monotonic() - task_start)
except SQLAlchemyError as e:
current_app.logger.exception(e)
@notify_celery.task(name="send-email")
def send_email(service_id, notification_id, from_address, encrypted_notification, created_at):
task_start = monotonic()
notification = encryption.decrypt(encrypted_notification)
service = dao_fetch_service_by_id(service_id)
@@ -296,6 +308,11 @@ def send_email(service_id, notification_id, from_address, encrypted_notification
)
dao_create_notification(notification_db_object, TEMPLATE_TYPE_EMAIL, provider.get_name())
statsd_client.timing_with_dates(
"notifications.tasks.send-email.queued-for",
sent_at,
datetime.strptime(created_at, DATETIME_FORMAT)
)
if restricted:
return
@@ -312,7 +329,9 @@ def send_email(service_id, notification_id, from_address, encrypted_notification
body=template.replaced_govuk_escaped,
html_body=template.as_HTML_email,
)
update_notification_reference_by_id(notification_id, reference)
except EmailClientException as e:
current_app.logger.exception(e)
notification_db_object.status = 'failed'
@@ -321,6 +340,8 @@ def send_email(service_id, notification_id, from_address, encrypted_notification
current_app.logger.info(
"Email {} created at {} sent at {}".format(notification_id, created_at, sent_at)
)
statsd_client.incr("notifications.tasks.send-email")
statsd_client.timing("notifications.tasks.send-email.task-time", monotonic() - task_start)
except SQLAlchemyError as e:
current_app.logger.exception(e)