Skip to content
This repository has been archived by the owner on Apr 22, 2020. It is now read-only.

Commit

Permalink
#4 allow querying more than one cloudwatch statistic
Browse files Browse the repository at this point in the history
  • Loading branch information
hjacobs committed Apr 12, 2016
1 parent d7ee6f7 commit f4d4f2a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
25 changes: 25 additions & 0 deletions tests/test_cloudwatch.py
Expand Up @@ -69,6 +69,31 @@ def test_cloudwatch_query_one(monkeypatch):
Statistics=['Average'])


def test_cloudwatch_query_one_multiple_statistics(monkeypatch):
client = MagicMock()
client.get_metric_statistics.return_value = {
'Datapoints': [
{'Timestamp': 99, 'Average': 111.25, 'Minimum': 1},
{'Timestamp': 11, 'Average': 100.25}
]}
get = MagicMock()
get.return_value.json.return_value = {'region': 'myregion'}
monkeypatch.setattr('requests.get', get)
monkeypatch.setattr('boto3.client', lambda x, region_name: client)
cloudwatch = CloudwatchWrapper()
start = datetime.datetime.now()
end = start # makes no sense, but works for our test
elb_data = cloudwatch.query_one({'LoadBalancerName': 'pierone-1'}, 'Latency', None, 'AWS/ELB', start=start, end=end)
assert {'Average': 111.25, 'Minimum': 1} == elb_data
assert not client.list_metrics.called
client.get_metric_statistics.assert_called_with(Namespace='AWS/ELB', MetricName='Latency',
Dimensions=[{'Name': 'LoadBalancerName', 'Value': 'pierone-1'}],
StartTime=start,
EndTime=end,
Period=60,
Statistics=['Sum', 'Average', 'Maximum', 'SampleCount', 'Minimum'])


def test_cloudwatch_query_one_no_result(monkeypatch):
client = MagicMock()
client.get_metric_statistics.return_value = {'Datapoints': []}
Expand Down
13 changes: 11 additions & 2 deletions zmon_worker_monitor/builtins/plugins/cloudwatch.py
Expand Up @@ -52,6 +52,12 @@ def query_one(self, dimensions, metric_name, statistics, namespace, period=60, m
if period < 60 or period % 60 != 0:
raise ValueError('Period must be greater than and a multiple of 60')

# special case to gather all types at once
if statistics is None:
statistics = ['Sum', 'Average', 'Maximum', 'SampleCount', 'Minimum']
elif isinstance(statistics, basestring):
statistics = [statistics]

end = end or datetime.datetime.utcnow()
start = start or (end - datetime.timedelta(minutes=minutes))
if isinstance(dimensions, dict):
Expand All @@ -61,11 +67,14 @@ def query_one(self, dimensions, metric_name, statistics, namespace, period=60, m
response = self.client.get_metric_statistics(Namespace=namespace, MetricName=metric_name,
Dimensions=dimensions,
StartTime=start, EndTime=end, Period=period,
Statistics=[statistics])
Statistics=statistics)
data_points = sorted(response['Datapoints'], key=lambda x: x["Timestamp"])
if not data_points:
return None
return data_points[-1][statistics]
if len(statistics) == 1:
return data_points[-1][statistics[0]]
else:
return {s: v for s, v in data_points[-1].items() if s in statistics}

def query(self, dimensions, metric_name, statistics='Sum', namespace=None, period=60, minutes=5):
'''Query one or more metric statistics; allows finding dimensions with wildcards'''
Expand Down

0 comments on commit f4d4f2a

Please sign in to comment.