2015-02-14 21:30:10 +00:00
|
|
|
import logging
|
|
|
|
import boto
|
2015-08-17 16:03:49 +00:00
|
|
|
import time
|
2017-01-05 16:31:21 +00:00
|
|
|
import random
|
2015-02-14 21:30:10 +00:00
|
|
|
|
2015-08-17 16:03:49 +00:00
|
|
|
from Queue import Empty
|
2015-02-14 21:30:10 +00:00
|
|
|
from threading import Thread
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2015-08-26 20:48:48 +00:00
|
|
|
MAX_BATCH_METRICS = 20
|
2015-08-17 16:03:49 +00:00
|
|
|
|
|
|
|
# Sleep for this much time between failed send requests.
|
|
|
|
# This prevents hammering cloudwatch when it's not available.
|
|
|
|
FAILED_SEND_SLEEP_SECS = 5
|
|
|
|
|
2015-08-12 19:14:09 +00:00
|
|
|
def start_cloudwatch_sender(metrics, app):
|
2015-02-14 21:30:10 +00:00
|
|
|
"""
|
2015-08-11 20:39:33 +00:00
|
|
|
Starts sending from metrics to a new CloudWatchSender.
|
2015-02-14 21:30:10 +00:00
|
|
|
"""
|
2015-08-17 20:26:20 +00:00
|
|
|
access_key = app.config.get('CLOUDWATCH_AWS_ACCESS_KEY')
|
|
|
|
secret_key = app.config.get('CLOUDWATCH_AWS_SECRET_KEY')
|
|
|
|
namespace = app.config.get('CLOUDWATCH_NAMESPACE')
|
|
|
|
|
|
|
|
if not namespace:
|
2015-08-17 16:03:49 +00:00
|
|
|
logger.debug('CloudWatch not configured')
|
2015-08-12 20:31:01 +00:00
|
|
|
return
|
2015-02-18 19:13:36 +00:00
|
|
|
|
2015-08-11 20:39:33 +00:00
|
|
|
sender = CloudWatchSender(metrics, access_key, secret_key, namespace)
|
2015-02-18 19:13:36 +00:00
|
|
|
sender.start()
|
2015-02-14 21:30:10 +00:00
|
|
|
|
|
|
|
class CloudWatchSender(Thread):
|
|
|
|
"""
|
|
|
|
CloudWatchSender loops indefinitely and pulls metrics off of a queue then sends it to CloudWatch.
|
|
|
|
"""
|
2015-08-11 20:39:33 +00:00
|
|
|
def __init__(self, metrics, aws_access_key, aws_secret_key, namespace):
|
2015-02-14 21:30:10 +00:00
|
|
|
Thread.__init__(self)
|
|
|
|
self.daemon = True
|
|
|
|
|
|
|
|
self._aws_access_key = aws_access_key
|
|
|
|
self._aws_secret_key = aws_secret_key
|
2015-08-11 20:39:33 +00:00
|
|
|
self._metrics = metrics
|
|
|
|
self._namespace = namespace
|
2015-02-14 21:30:10 +00:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
try:
|
|
|
|
logger.debug('Starting CloudWatch sender process.')
|
|
|
|
connection = boto.connect_cloudwatch(self._aws_access_key, self._aws_secret_key)
|
|
|
|
except:
|
|
|
|
logger.exception('Failed to connect to CloudWatch.')
|
2016-06-28 18:36:17 +00:00
|
|
|
self._metrics.enable_deprecated()
|
2015-02-14 21:30:10 +00:00
|
|
|
|
|
|
|
while True:
|
2015-08-17 16:03:49 +00:00
|
|
|
metrics = {
|
|
|
|
'name': [],
|
|
|
|
'value': [],
|
|
|
|
'unit': [],
|
|
|
|
'timestamp': [],
|
|
|
|
'dimensions': [],
|
|
|
|
}
|
|
|
|
|
2016-06-28 18:36:17 +00:00
|
|
|
metric = self._metrics.get_deprecated()
|
2015-08-17 16:03:49 +00:00
|
|
|
append_metric(metrics, metric)
|
|
|
|
|
|
|
|
while len(metrics['name']) < MAX_BATCH_METRICS:
|
|
|
|
try:
|
2016-06-28 18:36:17 +00:00
|
|
|
metric = self._metrics.get_nowait_deprecated()
|
2015-08-17 16:03:49 +00:00
|
|
|
append_metric(metrics, metric)
|
|
|
|
except Empty:
|
|
|
|
break
|
|
|
|
|
2015-02-14 21:30:10 +00:00
|
|
|
try:
|
2015-08-17 16:03:49 +00:00
|
|
|
connection.put_metric_data(self._namespace, **metrics)
|
|
|
|
logger.debug('Sent %d CloudWatch metrics', len(metrics['name']))
|
2015-02-14 21:30:10 +00:00
|
|
|
except:
|
2015-08-17 16:03:49 +00:00
|
|
|
for i in range(len(metrics['name'])):
|
2016-06-28 18:36:17 +00:00
|
|
|
self._metrics.put_deprecated(metrics['name'][i], metrics['value'][i],
|
2015-08-17 16:03:49 +00:00
|
|
|
unit=metrics['unit'][i],
|
|
|
|
dimensions=metrics['dimensions'][i],
|
|
|
|
timestamp=metrics['timestamp'][i],
|
|
|
|
)
|
|
|
|
|
|
|
|
logger.exception('Failed to write to CloudWatch: %s', metrics)
|
|
|
|
logger.debug('Attempted to requeue %d metrics.', len(metrics['name']))
|
2017-01-05 16:31:21 +00:00
|
|
|
# random int between 1/2 and 1 1/2 of FAILED_SEND_SLEEP duration
|
|
|
|
sleep_secs = random.randint(FAILED_SEND_SLEEP_SECS/2, 3*FAILED_SEND_SLEEP_SECS/2)
|
|
|
|
time.sleep(sleep_secs)
|
2015-08-17 16:03:49 +00:00
|
|
|
|
|
|
|
def append_metric(metrics, m):
|
|
|
|
name, value, kwargs = m
|
|
|
|
metrics['name'].append(name)
|
|
|
|
metrics['value'].append(value)
|
|
|
|
metrics['unit'].append(kwargs.get('unit'))
|
|
|
|
metrics['dimensions'].append(kwargs.get('dimensions'))
|
|
|
|
metrics['timestamp'].append(kwargs.get('timestamp'))
|