mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-02 09:26:08 -05:00
Removed statsd from the api and use the statsd in the utils library.
The statsd code was added to the utils library a while ago, uses the statsd from the util library and therefore consolidates the code into once place.
This commit is contained in:
@@ -2,6 +2,7 @@ from datetime import datetime, timedelta
|
||||
import math
|
||||
|
||||
from flask import current_app
|
||||
from notifications_utils.statsd_decorators import statsd
|
||||
from requests import (
|
||||
post as requests_post,
|
||||
RequestException
|
||||
@@ -20,7 +21,6 @@ from app.dao.notifications_dao import (
|
||||
dao_get_notifications_by_references,
|
||||
)
|
||||
from app.models import NOTIFICATION_CREATED
|
||||
from app.statsd_decorators import statsd
|
||||
|
||||
LETTERS_PDF_FILE_LOCATION_STRUCTURE = \
|
||||
'{folder}/NOTIFY.{reference}.{duplex}.{letter_class}.{colour}.{crown}.{date}.pdf'
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
from flask import current_app
|
||||
from notifications_utils.statsd_decorators import statsd
|
||||
|
||||
from app import notify_celery
|
||||
from app.config import QueueNames
|
||||
from app.statsd_decorators import statsd
|
||||
|
||||
from app.notifications.notifications_ses_callback import process_ses_response
|
||||
|
||||
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
from celery.signals import worker_process_shutdown
|
||||
from flask import current_app
|
||||
from notifications_utils.recipients import InvalidEmailError
|
||||
from notifications_utils.statsd_decorators import statsd
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
|
||||
from app import notify_celery
|
||||
from app.config import QueueNames
|
||||
from app.dao import notifications_dao
|
||||
from app.dao.notifications_dao import update_notification_status_by_id
|
||||
from app.statsd_decorators import statsd
|
||||
from app.delivery import send_to_providers
|
||||
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ from datetime import (
|
||||
|
||||
from celery.signals import worker_process_shutdown
|
||||
from flask import current_app
|
||||
from notifications_utils.statsd_decorators import statsd
|
||||
from sqlalchemy import and_
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
from notifications_utils.s3 import s3upload
|
||||
@@ -54,7 +55,6 @@ from app.models import (
|
||||
JOB_STATUS_READY_TO_SEND
|
||||
)
|
||||
from app.notifications.process_notifications import send_notification_to_queue
|
||||
from app.statsd_decorators import statsd
|
||||
from app.celery.tasks import (
|
||||
create_dvla_file_contents_for_notifications,
|
||||
process_job
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import json
|
||||
|
||||
from notifications_utils.statsd_decorators import statsd
|
||||
|
||||
from app import (
|
||||
DATETIME_FORMAT,
|
||||
notify_celery,
|
||||
@@ -7,7 +10,6 @@ from app.dao.notifications_dao import (
|
||||
get_notification_by_id,
|
||||
)
|
||||
|
||||
from app.statsd_decorators import statsd
|
||||
from app.dao.service_callback_api_dao import get_service_callback_api_for_service
|
||||
from requests import (
|
||||
HTTPError,
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
from celery.signals import worker_process_shutdown
|
||||
from notifications_utils.statsd_decorators import statsd
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
from app import notify_celery
|
||||
from flask import current_app
|
||||
|
||||
from app.statsd_decorators import statsd
|
||||
from app.dao.statistics_dao import (
|
||||
create_or_update_job_sending_statistics,
|
||||
update_job_stats_outcome_count
|
||||
|
||||
@@ -8,6 +8,7 @@ from flask import current_app
|
||||
from notifications_utils.recipients import (
|
||||
RecipientCSV
|
||||
)
|
||||
from notifications_utils.statsd_decorators import statsd
|
||||
from notifications_utils.template import (
|
||||
SMSMessageTemplate,
|
||||
WithSubjectTemplate,
|
||||
@@ -71,7 +72,6 @@ from app.models import (
|
||||
)
|
||||
from app.notifications.process_notifications import persist_notification
|
||||
from app.service.utils import service_allowed_to_send_to
|
||||
from app.statsd_decorators import statsd
|
||||
from notifications_utils.s3 import s3upload
|
||||
|
||||
|
||||
|
||||
@@ -3,12 +3,12 @@ from datetime import (
|
||||
datetime
|
||||
)
|
||||
from flask import current_app
|
||||
from notifications_utils.statsd_decorators import statsd
|
||||
from sqlalchemy import desc
|
||||
|
||||
from app import db
|
||||
from app.dao.dao_utils import transactional
|
||||
from app.models import InboundSms
|
||||
from app.statsd_decorators import statsd
|
||||
|
||||
|
||||
@transactional
|
||||
|
||||
@@ -2,6 +2,7 @@ import uuid
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from flask import current_app
|
||||
from notifications_utils.statsd_decorators import statsd
|
||||
from sqlalchemy import func, desc, asc, cast, Date as sql_date
|
||||
|
||||
from app import db
|
||||
@@ -12,7 +13,6 @@ from app.models import (
|
||||
LETTER_TYPE
|
||||
)
|
||||
from app.variables import LETTER_TEST_API_FILENAME
|
||||
from app.statsd_decorators import statsd
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from datetime import datetime
|
||||
|
||||
from notifications_utils.statsd_decorators import statsd
|
||||
|
||||
from app import db
|
||||
from app.dao.dao_utils import transactional
|
||||
@@ -12,7 +13,6 @@ from app.models import (
|
||||
MonthlyBilling,
|
||||
NotificationHistory
|
||||
)
|
||||
from app.statsd_decorators import statsd
|
||||
from app.utils import convert_utc_to_bst
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from notifications_utils.statsd_decorators import statsd
|
||||
from sqlalchemy import Float, Integer, and_
|
||||
from sqlalchemy import func, case, cast
|
||||
from sqlalchemy import literal_column
|
||||
@@ -17,7 +18,6 @@ from app.models import (
|
||||
LetterRate,
|
||||
Service
|
||||
)
|
||||
from app.statsd_decorators import statsd
|
||||
from app.utils import get_london_month_from_utc_column
|
||||
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ from notifications_utils.recipients import (
|
||||
InvalidPhoneError,
|
||||
InvalidEmailError,
|
||||
)
|
||||
from notifications_utils.statsd_decorators import statsd
|
||||
from werkzeug.datastructures import MultiDict
|
||||
from sqlalchemy import (desc, func, or_, asc)
|
||||
from sqlalchemy.orm import joinedload
|
||||
@@ -44,7 +45,6 @@ from app.models import (
|
||||
)
|
||||
|
||||
from app.dao.dao_utils import transactional
|
||||
from app.statsd_decorators import statsd
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import uuid
|
||||
from datetime import date, datetime, timedelta, time
|
||||
|
||||
from notifications_utils.statsd_decorators import statsd
|
||||
from sqlalchemy import asc, func, extract
|
||||
from sqlalchemy.orm import joinedload
|
||||
from flask import current_app
|
||||
@@ -40,7 +41,6 @@ from app.models import (
|
||||
TEMPLATE_TYPES,
|
||||
LETTER_TYPE,
|
||||
)
|
||||
from app.statsd_decorators import statsd
|
||||
from app.utils import get_london_month_from_utc_column, get_london_midnight_in_utc
|
||||
|
||||
DEFAULT_SERVICE_PERMISSIONS = [
|
||||
|
||||
@@ -2,6 +2,7 @@ from datetime import datetime, timedelta
|
||||
from itertools import groupby
|
||||
|
||||
from flask import current_app
|
||||
from notifications_utils.statsd_decorators import statsd
|
||||
from sqlalchemy import func
|
||||
from sqlalchemy.exc import IntegrityError, SQLAlchemyError
|
||||
|
||||
@@ -17,7 +18,6 @@ from app.models import (
|
||||
NOTIFICATION_STATUS_SUCCESS,
|
||||
NOTIFICATION_DELIVERED,
|
||||
NOTIFICATION_SENT)
|
||||
from app.statsd_decorators import statsd
|
||||
|
||||
|
||||
@transactional
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from notifications_utils.statsd_decorators import statsd
|
||||
from sqlalchemy import or_, and_
|
||||
|
||||
from app import db
|
||||
from app.statsd_decorators import statsd
|
||||
from app.dao.dao_utils import transactional
|
||||
from app.models import StatsTemplateUsageByMonth, Template
|
||||
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
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()
|
||||
try:
|
||||
res = func(*args, **kwargs)
|
||||
elapsed_time = monotonic() - start_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
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
current_app.logger.error(
|
||||
"{namespace} call {func} failed".format(
|
||||
namespace=namespace, func=func.__name__
|
||||
)
|
||||
)
|
||||
raise e
|
||||
else:
|
||||
current_app.logger.info(
|
||||
"{namespace} call {func} took {time}".format(
|
||||
namespace=namespace, func=func.__name__, time="{0:.4f}".format(elapsed_time)
|
||||
)
|
||||
)
|
||||
return res
|
||||
wrapper.__wrapped__.__name__ = func.__name__
|
||||
return wrapper
|
||||
|
||||
return time_function
|
||||
@@ -1,24 +0,0 @@
|
||||
from unittest.mock import ANY
|
||||
|
||||
from app.statsd_decorators import statsd
|
||||
import app
|
||||
|
||||
|
||||
class AnyStringWith(str):
|
||||
def __eq__(self, other):
|
||||
return self in other
|
||||
|
||||
|
||||
def test_should_call_statsd(notify_api, mocker):
|
||||
mocker.patch('app.statsd_client.incr')
|
||||
mocker.patch('app.statsd_client.timing')
|
||||
mock_logger = mocker.patch.object(notify_api.logger, 'info')
|
||||
|
||||
@statsd(namespace="test")
|
||||
def test_function():
|
||||
return True
|
||||
|
||||
assert test_function()
|
||||
app.statsd_client.incr.assert_called_once_with("test.test_function")
|
||||
app.statsd_client.timing.assert_called_once_with("test.test_function", ANY)
|
||||
mock_logger.assert_called_once_with(AnyStringWith("test call test_function took "))
|
||||
Reference in New Issue
Block a user