Stop mix-in from failing when not installed.

This commit is contained in:
Dave Shawley 2019-09-02 09:11:57 -04:00
parent 5ced9be0be
commit d602430138
4 changed files with 39 additions and 2 deletions

View file

@ -52,6 +52,15 @@ Statsd Implementation
.. autoclass:: sprockets.mixins.metrics.statsd.StatsDCollector .. autoclass:: sprockets.mixins.metrics.statsd.StatsDCollector
:members: :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 Testing Helpers
--------------- ---------------
*So who actually tests that their metrics are emitted as they expect?* *So who actually tests that their metrics are emitted as they expect?*

View file

@ -7,6 +7,7 @@ Release History
--------------- ---------------
- Add configuration documentation - Add configuration documentation
- Exclude Tornado >6 (as-yet-unreleased version) - Exclude Tornado >6 (as-yet-unreleased version)
- Add :func:`sprockets.mixins.metrics.statsd.get_client` function
`4.0.0`_ (06-Feb-2019) `4.0.0`_ (06-Feb-2019)
---------------------- ----------------------

View file

@ -29,7 +29,9 @@ class StatsdMixin:
:param path: elements of the metric path to record :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): def increase_counter(self, *path, **kwargs):
"""Increase a counter. """Increase a counter.
@ -45,7 +47,9 @@ class StatsdMixin:
omitted, the counter is increased by one. 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 @contextlib.contextmanager
def execution_timer(self, *path): def execution_timer(self, *path):
@ -227,3 +231,12 @@ def install(application, **kwargs):
setattr(application, 'statsd', StatsDCollector(**kwargs)) setattr(application, 'statsd', StatsDCollector(**kwargs))
return True return True
def get_client(application):
"""Fetch the statsd client if it is installed.
:rtype: .StatsDCollector
"""
return getattr(application, 'statsd', None)

View file

@ -162,6 +162,13 @@ class TCPStatsdMetricCollectionTests(testing.AsyncHTTPTestCase):
response = self.fetch('/status_code') response = self.fetch('/status_code')
self.assertEqual(response.code, 200) 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): class TCPStatsdConfigurationTests(testing.AsyncHTTPTestCase):
@ -308,6 +315,13 @@ class UDPStatsdMetricCollectionTests(testing.AsyncHTTPTestCase):
self.assertEqual(expected, self.assertEqual(expected,
list(self.statsd.find_metrics(expected, 'ms'))[0][0]) 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): class UDPStatsdConfigurationTests(testing.AsyncHTTPTestCase):