Add $STATSD_PREFIX support.

This commit is contained in:
Dave Shawley 2021-03-23 22:01:02 -04:00
parent 05650286da
commit f6017d1a06
No known key found for this signature in database
GPG key ID: 44A9C9992CCFAB82
3 changed files with 21 additions and 9 deletions

View file

@ -17,6 +17,11 @@ the following environment variables.
The TCP port number that the StatsD server is listening on. This defaults to 8125 if it is not configured.
.. envvar:: STATSD_PREFIX
Optional prefix to use for metric paths. See the documentation for :class:`~sprockets_statsd.mixins.Application`
for addition notes on setting the path prefix.
.. envvar:: STATSD_PROTOCOL
The IP protocol to use when connecting to the StatsD server. You can specify either "tcp" or "udp". The

View file

@ -73,15 +73,18 @@ class Application(web.Application):
statsd_settings.setdefault('protocol',
os.environ.get('STATSD_PROTOCOL', 'tcp'))
try:
prefix = '.'.join([
'applications',
settings['service'],
settings['environment'],
])
except KeyError:
prefix = None
statsd_settings.setdefault('prefix', prefix)
if os.environ.get('STATSD_PREFIX'):
statsd_settings.setdefault('prefix', os.environ['STATSD_PREFIX'])
else:
try:
prefix = '.'.join([
'applications',
settings['service'],
settings['environment'],
])
except KeyError:
prefix = None
statsd_settings.setdefault('prefix', prefix)
super().__init__(*args, **settings)

View file

@ -51,6 +51,7 @@ class ApplicationTests(AsyncTestCaseWithTimeout):
def test_statsd_setting_defaults(self):
self.unsetenv('STATSD_HOST')
self.unsetenv('STATSD_PORT')
self.unsetenv('STATSD_PREFIX')
self.unsetenv('STATSD_PROTOCOL')
app = sprockets_statsd.mixins.Application()
@ -64,12 +65,14 @@ class ApplicationTests(AsyncTestCaseWithTimeout):
def test_that_statsd_settings_read_from_environment(self):
self.setenv('STATSD_HOST', 'statsd')
self.setenv('STATSD_PORT', '5218')
self.setenv('STATSD_PREFIX', 'my-service')
self.setenv('STATSD_PROTOCOL', 'udp')
app = sprockets_statsd.mixins.Application()
self.assertIn('statsd', app.settings)
self.assertEqual('statsd', app.settings['statsd']['host'])
self.assertEqual(5218, app.settings['statsd']['port'])
self.assertEqual('my-service', app.settings['statsd']['prefix'])
self.assertEqual('udp', app.settings['statsd']['protocol'])
def test_prefix_when_only_service_is_set(self):
@ -92,6 +95,7 @@ class ApplicationTests(AsyncTestCaseWithTimeout):
def test_overridden_settings(self):
self.setenv('STATSD_HOST', 'statsd')
self.setenv('STATSD_PORT', '9999')
self.setenv('STATSD_PREFIX', 'service')
self.setenv('STATSD_PROTOCOL', 'tcp')
app = sprockets_statsd.mixins.Application(
statsd={