Code review
This commit is contained in:
parent
f043bc1379
commit
7c3b555ee9
3 changed files with 14 additions and 14 deletions
6
app.py
6
app.py
|
@ -125,11 +125,7 @@ userevents = UserEventsBuilderModule(app)
|
||||||
superusers = SuperUserManager(app)
|
superusers = SuperUserManager(app)
|
||||||
signer = Signer(app, OVERRIDE_CONFIG_DIRECTORY)
|
signer = Signer(app, OVERRIDE_CONFIG_DIRECTORY)
|
||||||
metric_queue = MetricQueue()
|
metric_queue = MetricQueue()
|
||||||
|
start_cloudwatch_sender(metric_queue, app)
|
||||||
try:
|
|
||||||
start_cloudwatch_sender(metric_queue, app)
|
|
||||||
except KeyError:
|
|
||||||
logger.debug('Cloudwatch not configured')
|
|
||||||
|
|
||||||
tf = app.config['DB_TRANSACTION_FACTORY']
|
tf = app.config['DB_TRANSACTION_FACTORY']
|
||||||
|
|
||||||
|
|
|
@ -11,9 +11,13 @@ def start_cloudwatch_sender(metrics, app):
|
||||||
"""
|
"""
|
||||||
Starts sending from metrics to a new CloudWatchSender.
|
Starts sending from metrics to a new CloudWatchSender.
|
||||||
"""
|
"""
|
||||||
|
try:
|
||||||
access_key = app.config['CLOUDWATCH_AWS_ACCESS_KEY']
|
access_key = app.config['CLOUDWATCH_AWS_ACCESS_KEY']
|
||||||
secret_key = app.config['CLOUDWATCH_AWS_SECRET_KEY']
|
secret_key = app.config['CLOUDWATCH_AWS_SECRET_KEY']
|
||||||
namespace = app.config['CLOUDWATCH_NAMESPACE']
|
namespace = app.config['CLOUDWATCH_NAMESPACE']
|
||||||
|
except KeyError:
|
||||||
|
logger.debug('Cloudwatch not configured')
|
||||||
|
return
|
||||||
|
|
||||||
sender = CloudWatchSender(metrics, access_key, secret_key, namespace)
|
sender = CloudWatchSender(metrics, access_key, secret_key, namespace)
|
||||||
sender.start()
|
sender.start()
|
||||||
|
|
|
@ -18,31 +18,31 @@ class MetricQueue(object):
|
||||||
|
|
||||||
def put(self, *args, **kwargs):
|
def put(self, *args, **kwargs):
|
||||||
if self._queue is None:
|
if self._queue is None:
|
||||||
logging.debug('No metric queue: %s %s' %args, kwargs)
|
logging.debug('No metric queue: %s %s', args, kwargs)
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self._queue.put_nowait((args, kwargs))
|
self._queue.put_nowait((args, kwargs))
|
||||||
except Full:
|
except Full:
|
||||||
logger.error('Metric queue full')
|
logger.error('Metric queue full')
|
||||||
|
|
||||||
def get(self):
|
def get(self):
|
||||||
v = self._queue.get()
|
return self._queue.get()
|
||||||
return v
|
|
||||||
|
|
||||||
def time_blueprint(bp, metric_queue):
|
def time_blueprint(bp, metric_queue):
|
||||||
bp.before_request(time_before_request)
|
bp.before_request(time_before_request)
|
||||||
bp.after_request(time_after_request(bp.name, metric_queue))
|
bp.after_request(time_after_request(bp.name, metric_queue))
|
||||||
|
|
||||||
def time_before_request():
|
def time_before_request():
|
||||||
g._start = time.time()
|
g._request_start_time = time.time()
|
||||||
|
|
||||||
def time_after_request(name, metric_queue):
|
def time_after_request(name, metric_queue):
|
||||||
def f(r):
|
def f(r):
|
||||||
start = getattr(g, '_start', None)
|
start = getattr(g, '_request_start_time', None)
|
||||||
if start is None:
|
if start is None:
|
||||||
return r
|
return r
|
||||||
dur = time.time() - start
|
dur = time.time() - start
|
||||||
dims = dimensions={'endpoint': request.endpoint}
|
dims = {'endpoint': request.endpoint}
|
||||||
metric_queue.put('ResponseTime', dur, dimensions=dims, unit='Seconds')
|
metric_queue.put('ResponseTime', dur, dimensions=dims, unit='Seconds')
|
||||||
metric_queue.put('ResponseCode', r.status_code, dimensions=dims)
|
metric_queue.put('ResponseCode', r.status_code, dimensions=dims)
|
||||||
if r.status_code < 200 or r.status_code >= 300:
|
if r.status_code < 200 or r.status_code >= 300:
|
||||||
|
|
Reference in a new issue