diff --git a/setup.py b/setup.py index 0562182..9715549 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ ) setup(name='ss_instrumentation', - version='1.1.0', + version='1.2.0', description='', url='https://github.com/styleseat/ss-instrumentation', author='Some Dude at StyleSeat', diff --git a/src/ss_instrumentation/SSInstrumentation.py b/src/ss_instrumentation/SSInstrumentation.py index f47df06..87e8b8b 100644 --- a/src/ss_instrumentation/SSInstrumentation.py +++ b/src/ss_instrumentation/SSInstrumentation.py @@ -7,6 +7,7 @@ from collections import defaultdict import boto3 +from botocore.client import Config def batch(iterable, n=1): @@ -111,7 +112,12 @@ class SSInstrumentation(object): def __init__(self, config, storage=None): self.namespace = config['AWS_METRIC_NAMESPACE'] - self.client = boto3.client('cloudwatch', region_name=config['AWS_LOGGING_REGION']) + self.client = boto3.client('cloudwatch', Config( + connect_timeout=3, + read_timeout=3, + retries={'max_attempts': 0}, + region_name=config['AWS_LOGGING_REGION'] + )) if storage: self._storage = storage @@ -135,7 +141,11 @@ def put_metric(self, metric_name, value, **kwargs): return self.put_metrics([data]) def put_metrics(self, metrics): - """Store multiple metrics in CloudWatch""" + """ + Store multiple metrics in CloudWatch. May fail in various ways, returns + True on success but in most contexts you won't want to retry or even + bother to check the return value. + """ metrics_data = [] for metric in metrics: metric_data = { @@ -152,10 +162,14 @@ def put_metrics(self, metrics): metrics_data.append(metric_data) - self.client.put_metric_data( - Namespace=self.namespace, - MetricData=metrics_data - ) + try: + self.client.put_metric_data( + Namespace=self.namespace, + MetricData=metrics_data + ) + return True + except Exception: + return False def flush_meters(self): values = self._storage.pop_values_for_period() diff --git a/test/SSInstrumentation.py b/test/SSInstrumentation.py index dadf774..d0aa12d 100644 --- a/test/SSInstrumentation.py +++ b/test/SSInstrumentation.py @@ -52,7 +52,12 @@ def assert_metric_present(self, actual_metrics, expected_metric): @mock.patch('boto3.client') def test_client_config(self, mock_client_constructor): self.create_instr() - mock_client_constructor.assert_called_with('cloudwatch', region_name='us-west-2') + client_name, config = mock_client_constructor.call_args.args + assert client_name == 'cloudwatch' + assert config.connect_timeout == 3 + assert config.read_timeout == 3 + assert config.retries['max_attempts'] == 0 + assert config.region_name == 'us-west-2' @standard_mock def test_put_metric(self, mock_client):