Added statsd integration into the API

- new client for statsd, follows conventions used elsewhere for configuration
- client wraps underlying library so we can use a config property to send/not send statsd

Added statsd metrics for:
- count of API successful calls SMS/Email
- count of successful task execution for SMS/Email
- count of errors from Client libraries
- timing of API calls to third party clients
- timing of how long messages live on the SQS queue
This commit is contained in:
Martyn Inglis
2016-05-13 17:15:39 +01:00
parent 3c8e45093c
commit 3f7559b286
17 changed files with 234 additions and 74 deletions

View File

@@ -39,10 +39,11 @@ class AwsSesClient(EmailClient):
Amazon SES email client.
'''
def init_app(self, region, *args, **kwargs):
def init_app(self, region, statsd_client, *args, **kwargs):
self._client = boto3.client('ses', region_name=region)
super(AwsSesClient, self).__init__(*args, **kwargs)
self.name = 'ses'
self.statsd_client = statsd_client
def get_name(self):
return self.name
@@ -88,7 +89,9 @@ class AwsSesClient(EmailClient):
ReplyToAddresses=reply_to_addresses)
elapsed_time = monotonic() - start_time
current_app.logger.info("AWS SES request finished in {}".format(elapsed_time))
self.statsd_client.timing("notifications.clients.ses.request-time", elapsed_time)
return response['MessageId']
except Exception as e:
# TODO logging exceptions
self.statsd_client.incr("notifications.clients.ses.error")
raise AwsSesClientException(str(e))

View File

@@ -50,11 +50,12 @@ class FiretextClient(SmsClient):
FireText sms client.
'''
def init_app(self, config, *args, **kwargs):
def init_app(self, config, statsd_client, *args, **kwargs):
super(SmsClient, self).__init__(*args, **kwargs)
self.api_key = config.config.get('FIRETEXT_API_KEY')
self.from_number = config.config.get('FIRETEXT_NUMBER')
self.name = 'firetext'
self.statsd_client = statsd_client
def get_name(self):
return self.name
@@ -90,8 +91,10 @@ class FiretextClient(SmsClient):
api_error.message
)
)
self.statsd_client.incr("notifications.clients.firetext.error")
raise api_error
finally:
elapsed_time = monotonic() - start_time
current_app.logger.info("Firetext request finished in {}".format(elapsed_time))
self.statsd_client.timing("notifications.clients.firetext.request-time", elapsed_time)
return response

View File

@@ -11,8 +11,9 @@ class LoadtestingClient(FiretextClient):
Loadtest sms client.
'''
def init_app(self, config, *args, **kwargs):
def init_app(self, config, statsd_client, *args, **kwargs):
super(FiretextClient, self).__init__(*args, **kwargs)
self.api_key = config.config.get('LOADTESTING_API_KEY')
self.from_number = config.config.get('LOADTESTING_NUMBER')
self.name = 'loadtesting'
self.statsd_client = statsd_client

View File

@@ -39,11 +39,12 @@ class MMGClient(SmsClient):
MMG sms client
'''
def init_app(self, config, *args, **kwargs):
def init_app(self, config, statsd_client, *args, **kwargs):
super(SmsClient, self).__init__(*args, **kwargs)
self.api_key = config.get('MMG_API_KEY')
self.from_number = config.get('MMG_FROM_NUMBER')
self.name = 'mmg'
self.statsd_client = statsd_client
def get_name(self):
return self.name
@@ -78,8 +79,10 @@ class MMGClient(SmsClient):
api_error.message
)
)
self.statsd_client.incr("notifications.clients.mmg.error")
raise api_error
finally:
elapsed_time = monotonic() - start_time
self.statsd_client.timing("notifications.clients.mmg.request-time", elapsed_time)
current_app.logger.info("MMG request finished in {}".format(elapsed_time))
return response

View File

@@ -1,54 +0,0 @@
from monotonic import monotonic
from app.clients.sms import (
SmsClient, SmsClientException)
from twilio.rest import TwilioRestClient
from twilio import TwilioRestException
from flask import current_app
class TwilioClientException(SmsClientException):
pass
class TwilioClient(SmsClient):
'''
Twilio sms client.
'''
def init_app(self, config, *args, **kwargs):
super(TwilioClient, self).__init__(*args, **kwargs)
self.client = TwilioRestClient(
config.config.get('TWILIO_ACCOUNT_SID'),
config.config.get('TWILIO_AUTH_TOKEN'))
self.from_number = config.config.get('TWILIO_NUMBER')
self.name = 'twilio'
def get_name(self):
return self.name
def send_sms(self, to, content):
start_time = monotonic()
try:
response = self.client.messages.create(
body=content,
to=to,
from_=self.from_number
)
return response.sid
except TwilioRestException as e:
current_app.logger.exception(e)
raise TwilioClientException(e)
finally:
elapsed_time = monotonic() - start_time
current_app.logger.info("Twilio request finished in {}".format(elapsed_time))
def status(self, message_id):
try:
response = self.client.messages.get(message_id)
if response.status in ('delivered', 'failed'):
return response.status
elif response.status == 'undelivered':
return 'sending'
return None
except TwilioRestException as e:
current_app.logger.exception(e)
raise TwilioClientException(e)

View File

View File

@@ -0,0 +1,26 @@
from statsd import StatsClient
class StatsdClient(StatsClient):
def init_app(self, app, *args, **kwargs):
StatsClient.__init__(
self,
app.config.get('STATSD_HOST'),
app.config.get('STATSD_PORT'),
prefix=app.config.get('STATSD_PREFIX')
)
self.active = app.config.get('STATSD_ENABLED')
def incr(self, stat, count=1, rate=1):
if self.active:
super(StatsClient, self).incr(stat, count, rate)
def timing(self, stat, delta, rate=1):
if self.active:
super(StatsClient, self).timing(stat, delta, rate)
def timing_with_dates(self, stat, start, end, rate=1):
if self.active:
delta = (start - end).total_seconds() * 1000
super(StatsClient, self).timing(stat, delta, rate)