mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-23 17:01:35 -05:00
Add logging around 3rd party delivery calls
- time SES, Twilio, fire text calls - use monotonic for accuracy
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import boto3
|
import boto3
|
||||||
|
from flask import current_app
|
||||||
|
from monotonic import monotonic
|
||||||
from app.clients.email import (EmailClientException, EmailClient)
|
from app.clients.email import (EmailClientException, EmailClient)
|
||||||
|
|
||||||
|
|
||||||
@@ -34,6 +35,7 @@ class AwsSesClient(EmailClient):
|
|||||||
elif reply_to_addresses is None:
|
elif reply_to_addresses is None:
|
||||||
reply_to_addresses = []
|
reply_to_addresses = []
|
||||||
|
|
||||||
|
start_time = monotonic()
|
||||||
response = self._client.send_email(
|
response = self._client.send_email(
|
||||||
Source=source,
|
Source=source,
|
||||||
Destination={
|
Destination={
|
||||||
@@ -50,6 +52,8 @@ class AwsSesClient(EmailClient):
|
|||||||
'Data': body}}
|
'Data': body}}
|
||||||
},
|
},
|
||||||
ReplyToAddresses=reply_to_addresses)
|
ReplyToAddresses=reply_to_addresses)
|
||||||
|
elapsed_time = monotonic() - start_time
|
||||||
|
current_app.logger.info("AWS SES request finished in {}".format(elapsed_time))
|
||||||
return response['MessageId']
|
return response['MessageId']
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# TODO logging exceptions
|
# TODO logging exceptions
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
import logging
|
import logging
|
||||||
|
from monotonic import monotonic
|
||||||
from app.clients.sms import (
|
from app.clients.sms import (
|
||||||
SmsClient,
|
SmsClient,
|
||||||
SmsClientException
|
SmsClientException
|
||||||
)
|
)
|
||||||
|
from flask import current_app
|
||||||
from requests import request, RequestException, HTTPError
|
from requests import request, RequestException, HTTPError
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -36,6 +38,7 @@ class FiretextClient(SmsClient):
|
|||||||
}
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
start_time = monotonic()
|
||||||
response = request(
|
response = request(
|
||||||
"POST",
|
"POST",
|
||||||
"https://www.firetext.co.uk/api/sendsms",
|
"https://www.firetext.co.uk/api/sendsms",
|
||||||
@@ -53,4 +56,7 @@ class FiretextClient(SmsClient):
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
raise api_error
|
raise api_error
|
||||||
|
finally:
|
||||||
|
elapsed_time = monotonic() - start_time
|
||||||
|
current_app.logger.info("Firetext request finished in {}".format(elapsed_time))
|
||||||
return response
|
return response
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
import logging
|
from monotonic import monotonic
|
||||||
from app.clients.sms import (
|
from app.clients.sms import (
|
||||||
SmsClient, SmsClientException)
|
SmsClient, SmsClientException)
|
||||||
from twilio.rest import TwilioRestClient
|
from twilio.rest import TwilioRestClient
|
||||||
from twilio import TwilioRestException
|
from twilio import TwilioRestException
|
||||||
|
from flask import current_app
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
class TwilioClientException(SmsClientException):
|
class TwilioClientException(SmsClientException):
|
||||||
@@ -28,6 +26,7 @@ class TwilioClient(SmsClient):
|
|||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
def send_sms(self, to, content):
|
def send_sms(self, to, content):
|
||||||
|
start_time = monotonic()
|
||||||
try:
|
try:
|
||||||
response = self.client.messages.create(
|
response = self.client.messages.create(
|
||||||
body=content,
|
body=content,
|
||||||
@@ -36,8 +35,11 @@ class TwilioClient(SmsClient):
|
|||||||
)
|
)
|
||||||
return response.sid
|
return response.sid
|
||||||
except TwilioRestException as e:
|
except TwilioRestException as e:
|
||||||
logger.exception(e)
|
current_app.logger.exception(e)
|
||||||
raise TwilioClientException(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):
|
def status(self, message_id):
|
||||||
try:
|
try:
|
||||||
@@ -46,5 +48,5 @@ class TwilioClient(SmsClient):
|
|||||||
return response.status
|
return response.status
|
||||||
return None
|
return None
|
||||||
except TwilioRestException as e:
|
except TwilioRestException as e:
|
||||||
logger.exception(e)
|
current_app.logger.exception(e)
|
||||||
raise TwilioClientException(e)
|
raise TwilioClientException(e)
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ boto3==1.2.3
|
|||||||
boto==2.39.0
|
boto==2.39.0
|
||||||
celery==3.1.20
|
celery==3.1.20
|
||||||
twilio==4.6.0
|
twilio==4.6.0
|
||||||
|
monotonic==0.3
|
||||||
|
|
||||||
|
|
||||||
git+https://github.com/alphagov/notifications-python-client.git@0.2.6#egg=notifications-python-client==0.2.6
|
git+https://github.com/alphagov/notifications-python-client.git@0.2.6#egg=notifications-python-client==0.2.6
|
||||||
|
|||||||
Reference in New Issue
Block a user