48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
import logging
|
|
import boto
|
|
|
|
from Queue import Queue
|
|
from threading import Thread
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
def start_cloudwatch_sender(metrics, app):
|
|
"""
|
|
Starts sending from metrics to a new CloudWatchSender.
|
|
"""
|
|
access_key = app.config['CLOUDWATCH_AWS_ACCESS_KEY']
|
|
secret_key = app.config['CLOUDWATCH_AWS_SECRET_KEY']
|
|
namespace = app.config['CLOUDWATCH_NAMESPACE']
|
|
|
|
sender = CloudWatchSender(metrics, access_key, secret_key, namespace)
|
|
sender.start()
|
|
|
|
class CloudWatchSender(Thread):
|
|
"""
|
|
CloudWatchSender loops indefinitely and pulls metrics off of a queue then sends it to CloudWatch.
|
|
"""
|
|
def __init__(self, metrics, aws_access_key, aws_secret_key, namespace):
|
|
Thread.__init__(self)
|
|
self.daemon = True
|
|
|
|
self._aws_access_key = aws_access_key
|
|
self._aws_secret_key = aws_secret_key
|
|
self._metrics = metrics
|
|
self._namespace = namespace
|
|
|
|
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.')
|
|
self._metrics.enable()
|
|
|
|
while True:
|
|
put_metric_args, kwargs = self._metrics.get()
|
|
logger.debug('Got queued put metrics request.')
|
|
try:
|
|
connection.put_metric_data(self._namespace, *put_metric_args, **kwargs)
|
|
except:
|
|
logger.exception('Failed to write to CloudWatch')
|