2015-08-17 16:03:49 +00:00
|
|
|
import datetime
|
2015-08-11 20:52:05 +00:00
|
|
|
import logging
|
2015-08-12 15:58:04 +00:00
|
|
|
import time
|
2015-08-11 20:52:05 +00:00
|
|
|
|
2015-08-12 15:58:04 +00:00
|
|
|
from functools import wraps
|
2015-08-11 20:52:05 +00:00
|
|
|
from Queue import Queue, Full
|
|
|
|
|
2015-08-12 15:58:04 +00:00
|
|
|
from flask import g, request
|
2016-09-29 19:44:06 +00:00
|
|
|
from trollius import Return
|
2015-08-12 15:58:04 +00:00
|
|
|
|
2015-08-11 20:52:05 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2015-08-11 20:39:33 +00:00
|
|
|
|
2016-06-28 18:36:17 +00:00
|
|
|
# Buckets for the API response times.
|
|
|
|
API_RESPONSE_TIME_BUCKETS = [.01, .025, .05, .1, .25, .5, 1.0, 2.5, 5.0]
|
2016-02-01 20:07:46 +00:00
|
|
|
|
2016-09-29 19:44:06 +00:00
|
|
|
# Buckets for the builder start times.
|
|
|
|
BUILDER_START_TIME_BUCKETS = [.5, 1.0, 5.0, 10.0, 30.0, 60.0, 120.0, 180.0, 240.0, 300.0, 600.0]
|
|
|
|
|
2015-11-20 20:32:17 +00:00
|
|
|
|
2015-08-11 20:39:33 +00:00
|
|
|
class MetricQueue(object):
|
2016-06-28 18:36:17 +00:00
|
|
|
""" Object to which various metrics are written, for distribution to metrics collection
|
2016-12-01 16:20:31 +00:00
|
|
|
system(s) such as Prometheus.
|
2016-06-28 18:36:17 +00:00
|
|
|
"""
|
2016-02-01 20:07:46 +00:00
|
|
|
def __init__(self, prom):
|
2016-06-28 18:36:17 +00:00
|
|
|
# Define the various exported metrics.
|
2016-02-01 20:07:46 +00:00
|
|
|
self.resp_time = prom.create_histogram('response_time', 'HTTP response time in seconds',
|
2016-06-28 18:36:17 +00:00
|
|
|
labelnames=['endpoint'],
|
|
|
|
buckets=API_RESPONSE_TIME_BUCKETS)
|
2016-02-01 20:07:46 +00:00
|
|
|
self.resp_code = prom.create_counter('response_code', 'HTTP response code',
|
|
|
|
labelnames=['endpoint', 'code'])
|
|
|
|
self.non_200 = prom.create_counter('response_non200', 'Non-200 HTTP response codes',
|
|
|
|
labelnames=['endpoint'])
|
|
|
|
self.multipart_upload_start = prom.create_counter('multipart_upload_start',
|
2016-09-09 19:13:58 +00:00
|
|
|
'Multipart upload started')
|
|
|
|
self.multipart_upload_end = prom.create_counter('multipart_upload_end',
|
2016-02-01 20:07:46 +00:00
|
|
|
'Multipart upload ends.', labelnames=['type'])
|
|
|
|
self.build_capacity_shortage = prom.create_gauge('build_capacity_shortage',
|
|
|
|
'Build capacity shortage.')
|
2016-09-29 19:44:06 +00:00
|
|
|
self.builder_time_to_start = prom.create_histogram('builder_tts',
|
|
|
|
'Time from triggering to starting a builder.',
|
|
|
|
labelnames=['builder_type'],
|
|
|
|
buckets=BUILDER_START_TIME_BUCKETS)
|
2016-11-03 19:28:40 +00:00
|
|
|
self.builder_time_to_build = prom.create_histogram('builder_ttb',
|
2016-09-29 19:44:06 +00:00
|
|
|
'Time from triggering to actually starting a build',
|
|
|
|
labelnames=['builder_type'],
|
|
|
|
buckets=BUILDER_START_TIME_BUCKETS)
|
|
|
|
self.build_time = prom.create_histogram('build_time', 'Time spent buildig', labelnames=['builder_type'])
|
|
|
|
self.builder_fallback = prom.create_counter('builder_fallback', 'Builder fell back to ec2 executor')
|
2016-02-01 20:07:46 +00:00
|
|
|
self.percent_building = prom.create_gauge('build_percent_building', 'Percent building.')
|
2016-06-28 18:36:17 +00:00
|
|
|
self.build_counter = prom.create_counter('builds', 'Number of builds', labelnames=['name'])
|
|
|
|
self.ephemeral_build_workers = prom.create_counter('ephemeral_build_workers',
|
2016-12-13 23:02:03 +00:00
|
|
|
'Number of started ephemeral build workers')
|
2016-06-28 18:36:17 +00:00
|
|
|
self.ephemeral_build_worker_failure = prom.create_counter('ephemeral_build_worker_failure',
|
2016-12-13 23:02:03 +00:00
|
|
|
'Number of failed-to-start ephemeral build workers')
|
2016-06-28 18:36:17 +00:00
|
|
|
|
|
|
|
self.work_queue_running = prom.create_gauge('work_queue_running', 'Running items in a queue',
|
|
|
|
labelnames=['queue_name'])
|
|
|
|
self.work_queue_available = prom.create_gauge('work_queue_available',
|
|
|
|
'Available items in a queue',
|
|
|
|
labelnames=['queue_name'])
|
|
|
|
|
2016-09-09 19:13:58 +00:00
|
|
|
self.repository_pull = prom.create_counter('repository_pull', 'Repository Pull Count',
|
2016-11-03 19:28:40 +00:00
|
|
|
labelnames=['namespace', 'repo_name', 'protocol',
|
|
|
|
'status'])
|
2016-09-09 19:13:58 +00:00
|
|
|
|
|
|
|
self.repository_push = prom.create_counter('repository_push', 'Repository Push Count',
|
2016-11-03 19:28:40 +00:00
|
|
|
labelnames=['namespace', 'repo_name', 'protocol',
|
|
|
|
'status'])
|
2016-09-09 19:13:58 +00:00
|
|
|
|
2016-09-09 19:36:54 +00:00
|
|
|
self.repository_build_completed = prom.create_counter('repository_build_completed',
|
|
|
|
'Repository Build Complete Count',
|
|
|
|
labelnames=['namespace', 'repo_name',
|
2016-09-16 20:26:04 +00:00
|
|
|
'status', 'executor'])
|
2016-11-03 19:28:40 +00:00
|
|
|
|
2016-12-15 18:20:16 +00:00
|
|
|
self.chunk_size = prom.create_histogram('chunk_size',
|
|
|
|
'Registry blob chunk size',
|
|
|
|
labelnames=['storage_region'])
|
|
|
|
|
2016-11-03 19:28:40 +00:00
|
|
|
self.chunk_upload_time = prom.create_histogram('chunk_upload_time',
|
|
|
|
'Registry blob chunk upload time',
|
2016-12-14 17:16:02 +00:00
|
|
|
labelnames=['storage_region'])
|
2016-11-03 19:28:40 +00:00
|
|
|
|
|
|
|
self.authentication_count = prom.create_counter('authenication_count',
|
|
|
|
'Authentication count',
|
|
|
|
labelnames=['kind', 'status'])
|
2016-09-09 19:36:54 +00:00
|
|
|
|
2016-09-12 20:19:19 +00:00
|
|
|
self.repository_count = prom.create_gauge('repository_count', 'Number of repositories')
|
|
|
|
self.user_count = prom.create_gauge('user_count', 'Number of users')
|
|
|
|
self.org_count = prom.create_gauge('org_count', 'Number of Organizations')
|
|
|
|
self.robot_count = prom.create_gauge('robot_count', 'Number of robot accounts')
|
|
|
|
|
2016-06-28 18:36:17 +00:00
|
|
|
# Deprecated: Define an in-memory queue for reporting metrics to CloudWatch or another
|
|
|
|
# provider.
|
|
|
|
self._queue = None
|
2015-08-12 19:14:09 +00:00
|
|
|
|
2016-06-28 18:36:17 +00:00
|
|
|
def enable_deprecated(self, maxsize=10000):
|
2015-08-12 19:14:09 +00:00
|
|
|
self._queue = Queue(maxsize)
|
2015-08-11 20:39:33 +00:00
|
|
|
|
2016-06-28 18:36:17 +00:00
|
|
|
def put_deprecated(self, name, value, **kwargs):
|
2015-08-12 19:14:09 +00:00
|
|
|
if self._queue is None:
|
2015-08-17 20:26:20 +00:00
|
|
|
logger.debug('No metric queue %s %s %s', name, value, kwargs)
|
2015-08-12 19:14:09 +00:00
|
|
|
return
|
2015-08-12 20:31:01 +00:00
|
|
|
|
2015-08-11 20:52:05 +00:00
|
|
|
try:
|
2015-08-17 16:03:49 +00:00
|
|
|
kwargs.setdefault('timestamp', datetime.datetime.now())
|
|
|
|
kwargs.setdefault('dimensions', {})
|
|
|
|
self._queue.put_nowait((name, value, kwargs))
|
2015-08-11 20:52:05 +00:00
|
|
|
except Full:
|
|
|
|
logger.error('Metric queue full')
|
2015-08-11 20:39:33 +00:00
|
|
|
|
2016-06-28 18:36:17 +00:00
|
|
|
def get_deprecated(self):
|
2015-08-12 20:31:01 +00:00
|
|
|
return self._queue.get()
|
2015-08-12 15:58:04 +00:00
|
|
|
|
2016-06-28 18:36:17 +00:00
|
|
|
def get_nowait_deprecated(self):
|
2015-08-17 16:03:49 +00:00
|
|
|
return self._queue.get_nowait()
|
|
|
|
|
2016-02-01 20:07:46 +00:00
|
|
|
|
2016-09-29 19:44:06 +00:00
|
|
|
def duration_collector_async(metric, labelvalues):
|
|
|
|
""" Decorates a method to have its duration time logged to the metric. """
|
|
|
|
def decorator(func):
|
|
|
|
@wraps(func)
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
trigger_time = time.time()
|
|
|
|
try:
|
|
|
|
rv = func(*args, **kwargs)
|
|
|
|
except Return as e:
|
|
|
|
metric.Observe(time.time() - trigger_time, labelvalues=labelvalues)
|
|
|
|
raise e
|
|
|
|
return rv
|
|
|
|
return wrapper
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
2016-06-28 18:36:17 +00:00
|
|
|
def time_decorator(name, metric_queue):
|
|
|
|
""" Decorates an endpoint method to have its request time logged to the metrics queue. """
|
|
|
|
after = _time_after_request(name, metric_queue)
|
|
|
|
def decorator(func):
|
|
|
|
@wraps(func)
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
_time_before_request()
|
|
|
|
rv = func(*args, **kwargs)
|
|
|
|
after(rv)
|
|
|
|
return rv
|
|
|
|
return wrapper
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
2015-08-12 15:58:04 +00:00
|
|
|
def time_blueprint(bp, metric_queue):
|
2016-06-28 18:36:17 +00:00
|
|
|
""" Decorates a blueprint to have its request time logged to the metrics queue. """
|
|
|
|
bp.before_request(_time_before_request)
|
|
|
|
bp.after_request(_time_after_request(bp.name, metric_queue))
|
2015-08-12 15:58:04 +00:00
|
|
|
|
2016-02-01 20:07:46 +00:00
|
|
|
|
2016-06-28 18:36:17 +00:00
|
|
|
def _time_before_request():
|
2015-08-12 20:31:01 +00:00
|
|
|
g._request_start_time = time.time()
|
2015-08-12 15:58:04 +00:00
|
|
|
|
2016-02-01 20:07:46 +00:00
|
|
|
|
2016-06-28 18:36:17 +00:00
|
|
|
def _time_after_request(name, metric_queue):
|
2015-08-12 15:58:04 +00:00
|
|
|
def f(r):
|
2015-08-12 20:31:01 +00:00
|
|
|
start = getattr(g, '_request_start_time', None)
|
2015-08-12 15:58:04 +00:00
|
|
|
if start is None:
|
|
|
|
return r
|
2015-08-17 20:26:20 +00:00
|
|
|
|
2015-08-12 15:58:04 +00:00
|
|
|
dur = time.time() - start
|
2015-08-12 20:31:01 +00:00
|
|
|
dims = {'endpoint': request.endpoint}
|
2015-08-17 20:26:20 +00:00
|
|
|
|
2016-06-28 18:36:17 +00:00
|
|
|
metric_queue.put_deprecated('ResponseTime', dur, dimensions=dims, unit='Seconds')
|
|
|
|
metric_queue.put_deprecated('ResponseCode', r.status_code, dimensions=dims)
|
2015-12-16 18:52:08 +00:00
|
|
|
|
2016-02-01 20:07:46 +00:00
|
|
|
metric_queue.resp_time.Observe(dur, labelvalues=[request.endpoint])
|
|
|
|
metric_queue.resp_code.Inc(labelvalues=[request.endpoint, r.status_code])
|
2015-11-20 20:32:17 +00:00
|
|
|
|
2015-12-16 18:52:08 +00:00
|
|
|
if r.status_code >= 500:
|
2016-06-28 18:36:17 +00:00
|
|
|
metric_queue.put_deprecated('5XXResponse', 1, dimensions={'name': name})
|
2015-12-16 18:52:08 +00:00
|
|
|
elif r.status_code < 200 or r.status_code >= 300:
|
2016-06-28 18:36:17 +00:00
|
|
|
metric_queue.put_deprecated('Non200Response', 1, dimensions={'name': name})
|
2016-02-01 20:07:46 +00:00
|
|
|
metric_queue.non_200.Inc(labelvalues=[request.endpoint])
|
2015-08-17 20:26:20 +00:00
|
|
|
|
2015-08-12 15:58:04 +00:00
|
|
|
return r
|
|
|
|
return f
|