Add StatsDCollector.close method.

This commit is contained in:
Dave Shawley 2019-09-02 09:14:02 -04:00
parent d602430138
commit bc02db0f48
3 changed files with 38 additions and 4 deletions

View file

@ -8,6 +8,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 - Add :func:`sprockets.mixins.metrics.statsd.get_client` function
- Add :meth:`sprockets.mixins.metrics.statsd.StatsDCollector.close` method
`4.0.0`_ (06-Feb-2019) `4.0.0`_ (06-Feb-2019)
---------------------- ----------------------

View file

@ -113,6 +113,7 @@ class StatsDCollector:
self._namespace = namespace self._namespace = namespace
self._prepend_metric_type = prepend_metric_type self._prepend_metric_type = prepend_metric_type
self._tcp_reconnect_sleep = 5 self._tcp_reconnect_sleep = 5
self._closing = False
if protocol == 'tcp': if protocol == 'tcp':
self._tcp = True self._tcp = True
@ -137,10 +138,19 @@ class StatsDCollector:
async def _tcp_on_closed(self): async def _tcp_on_closed(self):
"""Invoked when the socket is closed.""" """Invoked when the socket is closed."""
LOGGER.warning('Not connected to statsd, connecting in %s seconds', if self._closing:
self._tcp_reconnect_sleep) LOGGER.info('Statsd socket closed')
await asyncio.sleep(self._tcp_reconnect_sleep) else:
self._sock = self._tcp_socket() LOGGER.warning('Not connected to statsd, connecting in %s seconds',
self._tcp_reconnect_sleep)
await asyncio.sleep(self._tcp_reconnect_sleep)
self._sock = self._tcp_socket()
def close(self):
"""Gracefully close the socket."""
if not self._closing:
self._closing = True
self._sock.close()
def send(self, path, value, metric_type): def send(self, path, value, metric_type):
"""Send a metric to Statsd. """Send a metric to Statsd.

View file

@ -169,6 +169,29 @@ class TCPStatsdMetricCollectionTests(testing.AsyncHTTPTestCase):
response = self.fetch('/', method='POST', body='') response = self.fetch('/', method='POST', body='')
self.assertEqual(response.code, 204) self.assertEqual(response.code, 204)
def test_that_client_closes_socket(self):
response = self.fetch('/status_code')
self.assertEqual(response.code, 200)
self.application.statsd.close()
response = self.fetch('/status_code')
self.assertEqual(response.code, 200)
self.assertTrue(self.application.statsd._sock.closed())
def test_that_client_can_be_closed_multiple_times(self):
response = self.fetch('/status_code')
self.assertEqual(response.code, 200)
self.application.statsd.close()
response = self.fetch('/status_code')
self.assertEqual(response.code, 200)
self.assertTrue(self.application.statsd._sock.closed())
self.application.statsd.close()
response = self.fetch('/status_code')
self.assertEqual(response.code, 200)
self.assertTrue(self.application.statsd._sock.closed())
class TCPStatsdConfigurationTests(testing.AsyncHTTPTestCase): class TCPStatsdConfigurationTests(testing.AsyncHTTPTestCase):