6a1dd376c2
This isolates the CloudWatch sending thread to it's own package where it can be shared among any other packages that want to asynchronously send metrics to CloudWatch.
66 lines
2.1 KiB
Python
66 lines
2.1 KiB
Python
import logging
|
|
|
|
from util.cloudwatch import get_queue
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class NullReporter(object):
|
|
def report(self, *args):
|
|
pass
|
|
|
|
|
|
class CloudWatchReporter(object):
|
|
""" CloudWatchReporter reports work queue metrics to CloudWatch """
|
|
def __init__(self, request_queue, namespace, need_capacity_name, build_percent_name):
|
|
self._namespace = namespace
|
|
self._need_capacity_name = need_capacity_name
|
|
self._build_percent_name = build_percent_name
|
|
self._put_metrics_queue = request_queue
|
|
|
|
def _send_to_queue(self, *args, **kwargs):
|
|
self._put_metrics_queue.put((args, kwargs))
|
|
|
|
def report(self, currently_processing, running_count, total_count):
|
|
logger.debug('Worker indicated %s running count and %s total count', running_count,
|
|
total_count)
|
|
|
|
need_capacity_count = total_count - running_count
|
|
self._send_to_queue(self._namespace, self._need_capacity_name, need_capacity_count,
|
|
unit='Count')
|
|
|
|
building_percent = 100 if currently_processing else 0
|
|
self._send_to_queue(self._namespace, self._build_percent_name, building_percent,
|
|
unit='Percent')
|
|
|
|
class QueueMetrics(object):
|
|
"""
|
|
QueueMetrics initializes a reporter for recording metrics of work queues.
|
|
"""
|
|
def __init__(self, app=None):
|
|
self.app = app
|
|
self.sender = None
|
|
if app is not None:
|
|
self.state = self.init_app(app)
|
|
else:
|
|
self.state = None
|
|
|
|
def init_app(self, app):
|
|
analytics_type = app.config.get('QUEUE_METRICS_TYPE', 'Null')
|
|
|
|
if analytics_type == 'CloudWatch':
|
|
namespace = app.config.get('QUEUE_METRICS_NAMESPACE')
|
|
req_capacity_name = app.config.get('QUEUE_METRICS_CAPACITY_SHORTAGE_NAME')
|
|
build_percent_name = app.config.get('QUEUE_METRICS_BUILD_PERCENT_NAME')
|
|
|
|
request_queue = get_queue(app)
|
|
reporter = CloudWatchReporter(request_queue, namespace, req_capacity_name,
|
|
build_percent_name)
|
|
else:
|
|
reporter = NullReporter()
|
|
|
|
return reporter
|
|
|
|
def __getattr__(self, name):
|
|
return getattr(self.state, name, None)
|