Skip to content
This repository has been archived by the owner on Jan 7, 2019. It is now read-only.

Commit

Permalink
Replace retrying with tenacity
Browse files Browse the repository at this point in the history
As per rholder/retrying#79 retrying is dead. Tenaciy is a community maintained fork.
This also fixes a problem with retry swallowing exceptions.
  • Loading branch information
benvand committed Sep 21, 2018
1 parent 65bad61 commit e03ef5f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
52 changes: 26 additions & 26 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import logging
import os

from datetime import datetime, timedelta
import boto3
import requests
import yaml
from retrying import retry
from datetime import datetime, timedelta
from tenacity import retry, wait_fixed, retry_if_result


logger = logging.getLogger("app")
Expand All @@ -33,45 +33,45 @@ def send_to_hostedgraphite(metrics):
logger.info("Metrics sent to hosted graphite - Status code {}".format(response.status_code))


def format_config_metric_entry_for_hostedgraphite_base_metric(metric_entry, timestamp):
metric_name = (metric_entry['Options']['Formatter'].replace("%(statistic)s", metric_entry['Statistics'].lower()))
base_metric = "{} 0.0 {}".format(metric_name, timestamp)
return base_metric
def format_config_metric_entry_for_hostedgraphite_base_metric(config_metric_entry, timestamp):
hostedgraphite_metric_name = (config_metric_entry['Options']['Formatter'].replace("%(statistic)s", config_metric_entry['Statistics'].lower()))
hostedgraphite_base_metric = "{} 0.0 {}".format(hostedgraphite_metric_name, timestamp)
return hostedgraphite_base_metric


def create_hostedgraphite_base_metrics(config):
"""Take a metric entry from the config format it into a base metric used to initialise the metric on hostedgraphite.
"""
base_metrics = []
hostedgraphite_base_metrics = []
timestamp = int(((datetime.now() - timedelta(days=5)) - datetime(1970, 1, 1)).total_seconds())
for metric_entry in config['Metrics']:
base_metric = format_config_metric_entry_for_hostedgraphite_base_metric(metric_entry, timestamp)
base_metrics.append(base_metric)
return base_metrics
for config_metric_entry in config['Metrics']:
hostedgraphite_base_metric = format_config_metric_entry_for_hostedgraphite_base_metric(config_metric_entry, timestamp)
hostedgraphite_base_metrics.append(hostedgraphite_base_metric)
return hostedgraphite_base_metrics


def format_cloudwatch_metric_datapoint_for_hostedgraphite(metric, result):
def format_cloudwatch_metric_datapoint_for_hostedgraphite(cloudwatch_metric_datapoint, result):
"""Given a cloudwatch metric datapoint convert it into the format hostedgraphite expects."""
return '{0} {1} {2}'.format(
(metric['Options']['Formatter'] % {'statistic': metric['Statistics']}).lower(),
result[metric['Statistics']],
(cloudwatch_metric_datapoint['Options']['Formatter'] % {'statistic': cloudwatch_metric_datapoint['Statistics']}).lower(),
result[cloudwatch_metric_datapoint['Statistics']],
result['Timestamp'].strftime('%s')
)


def get_metric_from_cloudwatch(client, metric):
def get_metric_from_cloudwatch(client, config_metric_entry):
"""Call the client once for th supplied metric."""
end_time = datetime.utcnow()
start_time = end_time - timedelta(seconds=600)
return client.get_metric_statistics(
Period=60,
StartTime=start_time,
EndTime=end_time,
MetricName=metric['MetricName'],
Namespace=metric['Namespace'],
Statistics=[metric['Statistics']],
Dimensions=[{'Name': k, 'Value': v} for k, v in metric['Dimensions'].items()],
Unit=metric.get('Unit', 'None'),
MetricName=config_metric_entry['MetricName'],
Namespace=config_metric_entry['Namespace'],
Statistics=[config_metric_entry['Statistics']],
Dimensions=[{'Name': k, 'Value': v} for k, v in config_metric_entry['Dimensions'].items()],
Unit=config_metric_entry.get('Unit', 'None'),
)


Expand All @@ -85,11 +85,11 @@ def get_metrics_from_cloudwatch_and_format_for_hostedgraphite(config):
hostedgraphite_metrics = []

client = boto3.client('cloudwatch', region_name="eu-west-1")
for metric_entry in config['Metrics']:
cloudwatch_metric = get_metric_from_cloudwatch(client, metric_entry)
for config_metric_entry in config['Metrics']:
cloudwatch_metric = get_metric_from_cloudwatch(client, config_metric_entry)
for cloudwatch_metric_datapoint in cloudwatch_metric['Datapoints']:
hostedgraphite_metric = format_cloudwatch_metric_datapoint_for_hostedgraphite(
metric_entry,
config_metric_entry,
cloudwatch_metric_datapoint
)
hostedgraphite_metrics.append(hostedgraphite_metric)
Expand All @@ -101,10 +101,10 @@ def get_metrics_from_cloudwatch_and_format_for_hostedgraphite(config):
logging.basicConfig(level=logging.INFO, format="%(asctime)s:%(name)s:%(levelname)s:%(message)s")
config = get_config()

base_metrics = create_hostedgraphite_base_metrics(config)
send_to_hostedgraphite("\n".join(base_metrics))
hostedgraphite_base_metrics = create_hostedgraphite_base_metrics(config)
send_to_hostedgraphite("\n".join(hostedgraphite_base_metrics))

@retry(wait_fixed=60000, retry_on_result=lambda res: res is None)
@retry(wait=wait_fixed(60), retry=retry_if_result(lambda res: res is None))
def sleep_and_send_retry():
"""Wrapper to apply retry to get and send methods."""
hostedgraphite_metrics = get_metrics_from_cloudwatch_and_format_for_hostedgraphite(config)
Expand Down
2 changes: 1 addition & 1 deletion requirements-app.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
retrying==1.3.3
tenacity==5.0.2
requests==2.17.3
PyYAML==3.13
boto3==1.9.6
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This file is autogenerated. Do not edit it manually.
retrying==1.3.3
tenacity==5.0.2
requests==2.17.3
PyYAML==3.13
boto3==1.9.6
Expand Down

0 comments on commit e03ef5f

Please sign in to comment.