Skip to content

Commit

Permalink
Merge pull request #10 from styleseat/try-catch-timeout
Browse files Browse the repository at this point in the history
Add timeouts, limit retries, and catch exceptions.
  • Loading branch information
Lanny authored Nov 9, 2020
2 parents 9803ea0 + 72c26b3 commit 4b79323
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
26 changes: 20 additions & 6 deletions src/ss_instrumentation/SSInstrumentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from collections import defaultdict

import boto3
from botocore.client import Config


def batch(iterable, n=1):
Expand Down Expand Up @@ -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
Expand All @@ -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 = {
Expand All @@ -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()
Expand Down
7 changes: 6 additions & 1 deletion test/SSInstrumentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 4b79323

Please sign in to comment.