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 23, 2018
1 parent 08e3027 commit c4442ed
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
32 changes: 16 additions & 16 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 Down Expand Up @@ -47,7 +47,7 @@ def format_config_metric_entry_for_hostedgraphite_base_metric(config_metric_entr
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 config_metric_entry in config['Metrics']:
hostedgraphite_base_metric = format_config_metric_entry_for_hostedgraphite_base_metric(
Expand All @@ -58,7 +58,7 @@ def create_hostedgraphite_base_metrics(config):
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."""
hostedgraphite_metric_name = (
cloudwatch_metric_datapoint['Options']['Formatter'] % {'statistic': cloudwatch_metric_datapoint['Statistics']}
Expand All @@ -70,19 +70,19 @@ def format_cloudwatch_metric_datapoint_for_hostedgraphite(metric, result):
)


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 @@ -96,11 +96,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 @@ -112,10 +112,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 c4442ed

Please sign in to comment.