Code review

This commit is contained in:
Matt Jibson 2015-08-12 16:31:01 -04:00
parent f043bc1379
commit 7c3b555ee9
3 changed files with 14 additions and 14 deletions

4
app.py
View file

@ -125,11 +125,7 @@ userevents = UserEventsBuilderModule(app)
superusers = SuperUserManager(app)
signer = Signer(app, OVERRIDE_CONFIG_DIRECTORY)
metric_queue = MetricQueue()
try:
start_cloudwatch_sender(metric_queue, app)
except KeyError:
logger.debug('Cloudwatch not configured')
tf = app.config['DB_TRANSACTION_FACTORY']

View file

@ -11,9 +11,13 @@ def start_cloudwatch_sender(metrics, app):
"""
Starts sending from metrics to a new CloudWatchSender.
"""
try:
access_key = app.config['CLOUDWATCH_AWS_ACCESS_KEY']
secret_key = app.config['CLOUDWATCH_AWS_SECRET_KEY']
namespace = app.config['CLOUDWATCH_NAMESPACE']
except KeyError:
logger.debug('Cloudwatch not configured')
return
sender = CloudWatchSender(metrics, access_key, secret_key, namespace)
sender.start()

View file

@ -18,31 +18,31 @@ class MetricQueue(object):
def put(self, *args, **kwargs):
if self._queue is None:
logging.debug('No metric queue: %s %s' %args, kwargs)
logging.debug('No metric queue: %s %s', args, kwargs)
return
try:
self._queue.put_nowait((args, kwargs))
except Full:
logger.error('Metric queue full')
def get(self):
v = self._queue.get()
return v
return self._queue.get()
def time_blueprint(bp, metric_queue):
bp.before_request(time_before_request)
bp.after_request(time_after_request(bp.name, metric_queue))
def time_before_request():
g._start = time.time()
g._request_start_time = time.time()
def time_after_request(name, metric_queue):
def f(r):
start = getattr(g, '_start', None)
start = getattr(g, '_request_start_time', None)
if start is None:
return r
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('ResponseCode', r.status_code, dimensions=dims)
if r.status_code < 200 or r.status_code >= 300: