mirror of
https://github.com/sprockets/sprockets-statsd.git
synced 2024-11-14 19:29:30 +00:00
Move statsd connector to attribute.
Since @aremm still believes that `Application.settings` should only be used for configuration and not for state ;)
This commit is contained in:
parent
69dfb51bf8
commit
6d310db517
2 changed files with 26 additions and 19 deletions
|
@ -10,6 +10,12 @@ from sprockets_statsd import statsd
|
|||
class Application(web.Application):
|
||||
"""Mix this into your application to add a statsd connection.
|
||||
|
||||
.. attribute:: statsd_connector
|
||||
:type: sprockets_statsd.statsd.Connector
|
||||
|
||||
Connection to the StatsD server that is set between calls
|
||||
to :meth:`.start_statsd` and :meth:`.stop_statsd`.
|
||||
|
||||
This mix-in is configured by the ``statsd`` settings key. The
|
||||
value is a dictionary with the following keys.
|
||||
|
||||
|
@ -71,7 +77,7 @@ class Application(web.Application):
|
|||
super().__init__(*args, **settings)
|
||||
|
||||
self.settings['statsd']['port'] = int(self.settings['statsd']['port'])
|
||||
self.__statsd_connector = None
|
||||
self.statsd_connector = None
|
||||
|
||||
async def start_statsd(self):
|
||||
"""Start the connector during startup.
|
||||
|
@ -82,8 +88,8 @@ class Application(web.Application):
|
|||
until the connector is running.
|
||||
|
||||
"""
|
||||
statsd_settings = self.settings['statsd']
|
||||
if statsd_settings.get('_connector') is None:
|
||||
if self.statsd_connector is None:
|
||||
statsd_settings = self.settings['statsd']
|
||||
kwargs = {
|
||||
'host': statsd_settings['host'],
|
||||
'port': statsd_settings['port'],
|
||||
|
@ -92,9 +98,9 @@ class Application(web.Application):
|
|||
kwargs['reconnect_sleep'] = statsd_settings['reconnect_sleep']
|
||||
if 'wait_timeout' in statsd_settings:
|
||||
kwargs['wait_timeout'] = statsd_settings['wait_timeout']
|
||||
connector = statsd.Connector(**kwargs)
|
||||
await connector.start()
|
||||
self.settings['statsd']['_connector'] = connector
|
||||
|
||||
self.statsd_connector = statsd.Connector(**kwargs)
|
||||
await self.statsd_connector.start()
|
||||
|
||||
async def stop_statsd(self):
|
||||
"""Stop the connector during shutdown.
|
||||
|
@ -104,18 +110,19 @@ class Application(web.Application):
|
|||
connector is stopped.
|
||||
|
||||
"""
|
||||
connector = self.settings['statsd'].pop('_connector', None)
|
||||
if connector is not None:
|
||||
await connector.stop()
|
||||
if self.statsd_connector is not None:
|
||||
await self.statsd_connector.stop()
|
||||
self.statsd_connector = None
|
||||
|
||||
|
||||
class RequestHandler(web.RequestHandler):
|
||||
"""Mix this into your handler to send metrics to a statsd server."""
|
||||
__connector: statsd.Connector
|
||||
statsd_connector: statsd.Connector
|
||||
|
||||
def initialize(self, **kwargs):
|
||||
super().initialize(**kwargs)
|
||||
self.__connector = self.settings.get('statsd', {}).get('_connector')
|
||||
self.application: Application
|
||||
self.statsd_connector = self.application.statsd_connector
|
||||
|
||||
def __build_path(self, *path):
|
||||
full_path = '.'.join(str(c) for c in path)
|
||||
|
@ -130,9 +137,9 @@ class RequestHandler(web.RequestHandler):
|
|||
:param path: path to record the duration under
|
||||
|
||||
"""
|
||||
if self.__connector is not None:
|
||||
self.__connector.inject_metric(self.__build_path('timers', *path),
|
||||
secs * 1000.0, 'ms')
|
||||
if self.statsd_connector is not None:
|
||||
self.statsd_connector.inject_metric(
|
||||
self.__build_path('timers', *path), secs * 1000.0, 'ms')
|
||||
|
||||
def increase_counter(self, *path, amount: int = 1):
|
||||
"""Adjust a counter.
|
||||
|
@ -142,8 +149,8 @@ class RequestHandler(web.RequestHandler):
|
|||
1 and can be negative
|
||||
|
||||
"""
|
||||
if self.__connector is not None:
|
||||
self.__connector.inject_metric(
|
||||
if self.statsd_connector is not None:
|
||||
self.statsd_connector.inject_metric(
|
||||
self.__build_path('counters', *path), amount, 'c')
|
||||
|
||||
@contextlib.contextmanager
|
||||
|
|
|
@ -98,11 +98,11 @@ class ApplicationTests(testing.AsyncTestCase):
|
|||
})
|
||||
try:
|
||||
self.io_loop.run_sync(app.start_statsd)
|
||||
connector = app.settings['statsd']['_connector']
|
||||
connector = app.statsd_connector
|
||||
self.assertIsNotNone(connector, 'statsd.Connector not created')
|
||||
|
||||
self.io_loop.run_sync(app.start_statsd)
|
||||
self.assertIs(app.settings['statsd']['_connector'], connector,
|
||||
self.assertIs(app.statsd_connector, connector,
|
||||
'statsd.Connector should not be recreated')
|
||||
finally:
|
||||
self.io_loop.run_sync(app.stop_statsd)
|
||||
|
@ -124,7 +124,7 @@ class ApplicationTests(testing.AsyncTestCase):
|
|||
})
|
||||
self.io_loop.run_sync(app.start_statsd)
|
||||
|
||||
processor = app.settings['statsd']['_connector'].processor
|
||||
processor = app.statsd_connector.processor
|
||||
self.assertEqual(0.5, processor._reconnect_sleep)
|
||||
self.assertEqual(0.25, processor._wait_timeout)
|
||||
self.io_loop.run_sync(app.stop_statsd)
|
||||
|
|
Loading…
Reference in a new issue