2014-05-21 23:50:37 +00:00
|
|
|
import logging
|
|
|
|
|
2015-02-14 21:30:10 +00:00
|
|
|
from util.cloudwatch import get_queue
|
2015-01-16 20:30:58 +00:00
|
|
|
|
2014-05-21 23:50:37 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class NullReporter(object):
|
2014-05-23 18:16:26 +00:00
|
|
|
def report(self, *args):
|
2014-05-21 23:50:37 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2015-02-14 21:30:10 +00:00
|
|
|
class CloudWatchReporter(object):
|
|
|
|
""" CloudWatchReporter reports work queue metrics to CloudWatch """
|
2014-05-23 18:16:26 +00:00
|
|
|
def __init__(self, request_queue, namespace, need_capacity_name, build_percent_name):
|
2015-02-18 19:13:36 +00:00
|
|
|
if None in (request_queue, namespace, need_capacity_name, build_percent_name):
|
|
|
|
raise TypeError
|
|
|
|
|
2014-05-21 23:50:37 +00:00
|
|
|
self._namespace = namespace
|
2014-05-23 18:16:26 +00:00
|
|
|
self._need_capacity_name = need_capacity_name
|
|
|
|
self._build_percent_name = build_percent_name
|
|
|
|
self._put_metrics_queue = request_queue
|
2014-05-21 23:50:37 +00:00
|
|
|
|
2014-05-23 18:16:26 +00:00
|
|
|
def _send_to_queue(self, *args, **kwargs):
|
|
|
|
self._put_metrics_queue.put((args, kwargs))
|
|
|
|
|
|
|
|
def report(self, currently_processing, running_count, total_count):
|
2014-05-22 17:50:06 +00:00
|
|
|
logger.debug('Worker indicated %s running count and %s total count', running_count,
|
|
|
|
total_count)
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-05-21 23:50:37 +00:00
|
|
|
need_capacity_count = total_count - running_count
|
2014-05-23 18:16:26 +00:00
|
|
|
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')
|
|
|
|
|
2014-05-21 23:50:37 +00:00
|
|
|
class QueueMetrics(object):
|
2015-02-14 21:30:10 +00:00
|
|
|
"""
|
|
|
|
QueueMetrics initializes a reporter for recording metrics of work queues.
|
|
|
|
"""
|
2014-05-21 23:50:37 +00:00
|
|
|
def __init__(self, app=None):
|
2015-02-18 19:13:36 +00:00
|
|
|
self._app = app
|
|
|
|
self._reporter = NullReporter()
|
2014-05-21 23:50:37 +00:00
|
|
|
if app is not None:
|
2015-02-18 19:13:36 +00:00
|
|
|
reporter_type = app.config.get('QUEUE_METRICS_TYPE', 'Null')
|
|
|
|
if reporter_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')
|
2014-05-21 23:50:37 +00:00
|
|
|
|
2015-02-18 19:13:36 +00:00
|
|
|
request_queue = get_queue(app)
|
|
|
|
self._reporter = CloudWatchReporter(request_queue, namespace, req_capacity_name,
|
|
|
|
build_percent_name)
|
2014-05-21 23:50:37 +00:00
|
|
|
|
|
|
|
def __getattr__(self, name):
|
2015-02-18 19:13:36 +00:00
|
|
|
return getattr(self._reporter, name, None)
|