From 7c3b555ee9956d02283ddf0d118de149c7f3ed46 Mon Sep 17 00:00:00 2001 From: Matt Jibson Date: Wed, 12 Aug 2015 16:31:01 -0400 Subject: [PATCH] Code review --- app.py | 6 +----- util/saas/cloudwatch.py | 10 +++++++--- util/saas/metricqueue.py | 12 ++++++------ 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/app.py b/app.py index 4b2110004..9ddc3d587 100644 --- a/app.py +++ b/app.py @@ -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') +start_cloudwatch_sender(metric_queue, app) tf = app.config['DB_TRANSACTION_FACTORY'] diff --git a/util/saas/cloudwatch.py b/util/saas/cloudwatch.py index ba8f01da0..442c9091e 100644 --- a/util/saas/cloudwatch.py +++ b/util/saas/cloudwatch.py @@ -11,9 +11,13 @@ def start_cloudwatch_sender(metrics, app): """ Starts sending from metrics to a new CloudWatchSender. """ - access_key = app.config['CLOUDWATCH_AWS_ACCESS_KEY'] - secret_key = app.config['CLOUDWATCH_AWS_SECRET_KEY'] - namespace = app.config['CLOUDWATCH_NAMESPACE'] + 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() diff --git a/util/saas/metricqueue.py b/util/saas/metricqueue.py index d6fc4338e..42d2bf543 100644 --- a/util/saas/metricqueue.py +++ b/util/saas/metricqueue.py @@ -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: