From d6024301385ab47102fd5405d98cfc2739ed81c8 Mon Sep 17 00:00:00 2001 From: Dave Shawley Date: Mon, 2 Sep 2019 09:11:57 -0400 Subject: [PATCH] Stop mix-in from failing when not installed. --- docs/api.rst | 9 +++++++++ docs/history.rst | 1 + sprockets/mixins/metrics/statsd.py | 17 +++++++++++++++-- tests.py | 14 ++++++++++++++ 4 files changed, 39 insertions(+), 2 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index 48554ee..e8a2326 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -52,6 +52,15 @@ Statsd Implementation .. autoclass:: sprockets.mixins.metrics.statsd.StatsDCollector :members: +Application Functions +--------------------- +Before you can use the mixin, you have to install the client by calling +the ``install`` function on your application instance. + +.. autofunction:: sprockets.mixins.metrics.statsd.install + +.. autofunction:: sprockets.mixins.metrics.statsd.get_client + Testing Helpers --------------- *So who actually tests that their metrics are emitted as they expect?* diff --git a/docs/history.rst b/docs/history.rst index 0e66ee7..515cc75 100644 --- a/docs/history.rst +++ b/docs/history.rst @@ -7,6 +7,7 @@ Release History --------------- - Add configuration documentation - Exclude Tornado >6 (as-yet-unreleased version) +- Add :func:`sprockets.mixins.metrics.statsd.get_client` function `4.0.0`_ (06-Feb-2019) ---------------------- diff --git a/sprockets/mixins/metrics/statsd.py b/sprockets/mixins/metrics/statsd.py index 8556d41..14952cc 100644 --- a/sprockets/mixins/metrics/statsd.py +++ b/sprockets/mixins/metrics/statsd.py @@ -29,7 +29,9 @@ class StatsdMixin: :param path: elements of the metric path to record """ - self.application.statsd.send(path, duration * 1000.0, 'ms') + client = get_client(self.application) + if client is not None: + client.send(path, duration * 1000.0, 'ms') def increase_counter(self, *path, **kwargs): """Increase a counter. @@ -45,7 +47,9 @@ class StatsdMixin: omitted, the counter is increased by one. """ - self.application.statsd.send(path, kwargs.get('amount', '1'), 'c') + client = get_client(self.application) + if client is not None: + client.send(path, kwargs.get('amount', '1'), 'c') @contextlib.contextmanager def execution_timer(self, *path): @@ -227,3 +231,12 @@ def install(application, **kwargs): setattr(application, 'statsd', StatsDCollector(**kwargs)) return True + + +def get_client(application): + """Fetch the statsd client if it is installed. + + :rtype: .StatsDCollector + + """ + return getattr(application, 'statsd', None) diff --git a/tests.py b/tests.py index 19b89be..c4b86d9 100644 --- a/tests.py +++ b/tests.py @@ -162,6 +162,13 @@ class TCPStatsdMetricCollectionTests(testing.AsyncHTTPTestCase): response = self.fetch('/status_code') self.assertEqual(response.code, 200) + def test_that_mixin_works_without_client(self): + self.application.statsd.close() + delattr(self.application, 'statsd') + + response = self.fetch('/', method='POST', body='') + self.assertEqual(response.code, 204) + class TCPStatsdConfigurationTests(testing.AsyncHTTPTestCase): @@ -308,6 +315,13 @@ class UDPStatsdMetricCollectionTests(testing.AsyncHTTPTestCase): self.assertEqual(expected, list(self.statsd.find_metrics(expected, 'ms'))[0][0]) + def test_that_mixin_works_without_client(self): + self.application.statsd.close() + delattr(self.application, 'statsd') + + response = self.fetch('/', method='POST', body='') + self.assertEqual(response.code, 204) + class UDPStatsdConfigurationTests(testing.AsyncHTTPTestCase):