2015-02-18 19:13:36 +00:00
|
|
|
from buildman.enums import BuildJobResult
|
2015-02-17 15:56:09 +00:00
|
|
|
from util.cloudwatch import get_queue
|
|
|
|
|
|
|
|
|
|
|
|
class BuildReporter(object):
|
|
|
|
"""
|
|
|
|
Base class for reporting build statuses to a metrics service.
|
|
|
|
"""
|
|
|
|
def report_completion_status(self, status):
|
|
|
|
"""
|
|
|
|
Method to invoke the recording of build's completion status to a metric service.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
|
class NullReporter(BuildReporter):
|
|
|
|
"""
|
|
|
|
The /dev/null of BuildReporters.
|
|
|
|
"""
|
|
|
|
def report_completion_status(self, *args):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class CloudWatchBuildReporter(BuildReporter):
|
|
|
|
"""
|
|
|
|
Implements a BuildReporter for Amazon's CloudWatch.
|
|
|
|
"""
|
|
|
|
def __init__(self, queue, namespace_name, completed_name, failed_name, incompleted_name):
|
|
|
|
self._queue = queue
|
|
|
|
self._namespace_name = namespace_name
|
|
|
|
self._completed_name = completed_name
|
|
|
|
self._failed_name = failed_name
|
|
|
|
self._incompleted_name = incompleted_name
|
|
|
|
|
2015-02-18 22:11:45 +00:00
|
|
|
def _send_to_queue(self, *args, **kwargs):
|
|
|
|
self._queue.put((args, kwargs))
|
|
|
|
|
2015-02-17 15:56:09 +00:00
|
|
|
def report_completion_status(self, status):
|
2015-02-18 19:13:36 +00:00
|
|
|
if status == BuildJobResult.COMPLETE:
|
2015-02-17 15:56:09 +00:00
|
|
|
status_name = self._completed_name
|
2015-02-18 19:13:36 +00:00
|
|
|
elif status == BuildJobResult.ERROR:
|
2015-02-17 15:56:09 +00:00
|
|
|
status_name = self._failed_name
|
2015-02-18 19:13:36 +00:00
|
|
|
elif status == BuildJobResult.INCOMPLETE:
|
2015-02-17 15:56:09 +00:00
|
|
|
status_name = self._incompleted_name
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
2015-02-18 22:11:45 +00:00
|
|
|
self._send_to_queue(self._namespace_name, status_name, 1, unit='Count')
|
2015-02-17 15:56:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BuildMetrics(object):
|
|
|
|
"""
|
|
|
|
BuildMetrics initializes a reporter for recording the status of build completions.
|
|
|
|
"""
|
|
|
|
def __init__(self, app=None):
|
|
|
|
self._app = app
|
2015-02-18 19:13:36 +00:00
|
|
|
self._reporter = NullReporter()
|
|
|
|
if app is not None:
|
2015-02-17 15:56:09 +00:00
|
|
|
reporter_type = app.config.get('BUILD_METRICS_TYPE', 'Null')
|
|
|
|
if reporter_type == 'CloudWatch':
|
2015-02-18 21:05:36 +00:00
|
|
|
namespace = app.config['BUILD_METRICS_NAMESPACE']
|
|
|
|
completed_name = app.config['BUILD_METRICS_COMPLETED_NAME']
|
|
|
|
failed_name = app.config['BUILD_METRICS_FAILED_NAME']
|
|
|
|
incompleted_name = app.config['BUILD_METRICS_INCOMPLETED_NAME']
|
2015-02-17 15:56:09 +00:00
|
|
|
request_queue = get_queue(app)
|
|
|
|
self._reporter = CloudWatchBuildReporter(request_queue, namespace, completed_name,
|
|
|
|
failed_name, incompleted_name)
|
2015-02-17 17:21:22 +00:00
|
|
|
|
|
|
|
def __getattr__(self, name):
|
|
|
|
return getattr(self._reporter, name, None)
|