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
|
|
|
|
|
2015-08-11 20:52:05 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2015-08-11 20:39:33 +00:00
|
|
|
|
2016-02-01 20:07:46 +00:00
|
|
|
|
|
|
|
API_BUCKETS = [.01, .025, .05, .1, .25, .5, 1.0, 2.5, 5.0]
|
|
|
|
|
2015-11-20 20:32:17 +00:00
|
|
|
|
2015-08-11 20:39:33 +00:00
|
|
|
class MetricQueue(object):
|
2016-02-01 20:07:46 +00:00
|
|
|
def __init__(self, prom):
|
2015-08-12 19:14:09 +00:00
|
|
|
self._queue = None
|
2016-02-01 20:07:46 +00:00
|
|
|
self.resp_time = prom.create_histogram('response_time', 'HTTP response time in seconds',
|
|
|
|
labelnames=['endpoint'], buckets=API_BUCKETS)
|
|
|
|
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',
|
|
|
|
'Multipart upload startse')
|
|
|
|
self.multipart_upload_end = prom.create_counter('self._metric_queue.multipart_upload_end',
|
|
|
|
'Multipart upload ends.', labelnames=['type'])
|
|
|
|
self.build_capacity_shortage = prom.create_gauge('build_capacity_shortage',
|
|
|
|
'Build capacity shortage.')
|
|
|
|
self.percent_building = prom.create_gauge('build_percent_building', 'Percent building.')
|
2015-08-12 19:14:09 +00:00
|
|
|
|
|
|
|
def enable(self, maxsize=10000):
|
|
|
|
self._queue = Queue(maxsize)
|
2015-08-11 20:39:33 +00:00
|
|
|
|
2015-08-17 16:03:49 +00:00
|
|
|
def put(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
|
|
|
|
|
|
|
def get(self):
|
2015-08-12 20:31:01 +00:00
|
|
|
return self._queue.get()
|
2015-08-12 15:58:04 +00:00
|
|
|
|
2015-08-17 16:03:49 +00:00
|
|
|
def get_nowait(self):
|
|
|
|
return self._queue.get_nowait()
|
|
|
|
|
2016-02-01 20:07:46 +00:00
|
|
|
|
2015-08-12 15:58:04 +00:00
|
|
|
def time_blueprint(bp, metric_queue):
|
|
|
|
bp.before_request(time_before_request)
|
|
|
|
bp.after_request(time_after_request(bp.name, metric_queue))
|
|
|
|
|
2016-02-01 20:07:46 +00:00
|
|
|
|
2015-08-12 15:58:04 +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
|
|
|
|
2015-08-12 15:58:04 +00:00
|
|
|
def time_after_request(name, metric_queue):
|
|
|
|
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
|
|
|
|
2015-08-12 15:58:04 +00:00
|
|
|
metric_queue.put('ResponseTime', dur, dimensions=dims, unit='Seconds')
|
|
|
|
metric_queue.put('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:
|
|
|
|
metric_queue.put('5XXResponse', 1, dimensions={'name': name})
|
|
|
|
elif r.status_code < 200 or r.status_code >= 300:
|
2015-08-12 15:58:04 +00:00
|
|
|
metric_queue.put('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
|
|
|
|
|
2016-02-01 20:07:46 +00:00
|
|
|
|
2015-08-12 15:58:04 +00:00
|
|
|
def time_decorator(name, metric_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
|