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):
|
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):
|
|
|
|
self.app = app
|
2015-01-16 18:44:29 +00:00
|
|
|
self.sender = None
|
2014-05-21 23:50:37 +00:00
|
|
|
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':
|
2014-05-23 18:16:26 +00:00
|
|
|
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')
|
|
|
|
|
2015-02-14 21:30:10 +00:00
|
|
|
request_queue = get_queue(app)
|
|
|
|
reporter = CloudWatchReporter(request_queue, namespace, req_capacity_name,
|
|
|
|
build_percent_name)
|
2014-05-21 23:50:37 +00:00
|
|
|
else:
|
|
|
|
reporter = NullReporter()
|
|
|
|
|
|
|
|
return reporter
|
|
|
|
|
|
|
|
def __getattr__(self, name):
|
|
|
|
return getattr(self.state, name, None)
|