Remove prepending host name

This commit is contained in:
Ryan Mclean 2016-12-08 15:48:32 -05:00
parent 1fb1f46bc1
commit 6d98e9b806
3 changed files with 11 additions and 22 deletions
README.rst
sprockets/mixins/metrics
tests.py

View file

@ -54,7 +54,6 @@ Settings
:port: The Statsd port :port: The Statsd port
:prepend_metric_type: Optional flag to prepend bucket path with the StatsD :prepend_metric_type: Optional flag to prepend bucket path with the StatsD
metric type metric type
:prepend_hostname: Optional flag to prepend bucket path with the hostname
InfluxDB Mixin InfluxDB Mixin
-------------- --------------

View file

@ -120,20 +120,17 @@ class StatsDCollector(object):
:param str namespace: The StatsD bucket to write metrics into. :param str namespace: The StatsD bucket to write metrics into.
:param bool prepend_metric_type: Optional flag to prepend bucket path :param bool prepend_metric_type: Optional flag to prepend bucket path
with the StatsD metric type with the StatsD metric type
:param bool prepend_hostname: Optional flag to prepend bucket path with
the hostname
""" """
METRIC_TYPES = {'c': 'counters', METRIC_TYPES = {'c': 'counters',
'ms': 'timers'} 'ms': 'timers'}
def __init__(self, host, port, namespace='sprockets', def __init__(self, host, port, namespace='sprockets',
prepend_metric_type=True, prepend_hostname=True): prepend_metric_type=True):
self._host = host self._host = host
self._port = int(port) self._port = int(port)
self._namespace = namespace self._namespace = namespace
self._prepend_metric_type = prepend_metric_type self._prepend_metric_type = prepend_metric_type
self._prepend_hostname = prepend_hostname
self._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0) self._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
def send(self, path, value, metric_type): def send(self, path, value, metric_type):
@ -169,8 +166,7 @@ class StatsDCollector(object):
"""Get prefixes where applicable """Get prefixes where applicable
Add metric prefix counters, timers respectively if Add metric prefix counters, timers respectively if
:attr:`prepend_metric_type` flag is True. Adds hostname prefix :attr:`prepend_metric_type` flag is True.
if :attr:`prepend_hostname` is true.
:param str metric_type: The metric type :param str metric_type: The metric type
:rtype: list :rtype: list
@ -179,8 +175,6 @@ class StatsDCollector(object):
prefixes = [] prefixes = []
if self._prepend_metric_type: if self._prepend_metric_type:
prefixes.append(self.METRIC_TYPES[metric_type]) prefixes.append(self.METRIC_TYPES[metric_type])
if self._prepend_hostname:
prefixes.append(socket.gethostname())
return prefixes return prefixes
@ -191,8 +185,8 @@ def install(application, **kwargs):
install the collector into. install the collector into.
:param kwargs: keyword parameters to pass to the :param kwargs: keyword parameters to pass to the
:class:`StatsDCollector` initializer. :class:`StatsDCollector` initializer.
:returns: :data:`True` if the client was installed by this call :returns: :data:`True` if the client was installed successfully,
and :data:`False` otherwise. or :data:`False` otherwise.
- **host** The StatsD host. If host is not specified, the - **host** The StatsD host. If host is not specified, the
``STATSD_HOST`` environment variable, or default `127.0.0.1`, ``STATSD_HOST`` environment variable, or default `127.0.0.1`,

View file

@ -49,7 +49,8 @@ class StatsdMetricCollectionTests(testing.AsyncHTTPTestCase):
self.statsd = FakeStatsdServer(self.io_loop) self.statsd = FakeStatsdServer(self.io_loop)
statsd.install(self.application, **{'namespace': 'testing', statsd.install(self.application, **{'namespace': 'testing',
'host': self.statsd.sockaddr[0], 'host': self.statsd.sockaddr[0],
'port': self.statsd.sockaddr[1]}) 'port': self.statsd.sockaddr[1],
'prepend_metric_type': True})
def tearDown(self): def tearDown(self):
self.statsd.close() self.statsd.close()
@ -59,8 +60,7 @@ class StatsdMetricCollectionTests(testing.AsyncHTTPTestCase):
response = self.fetch('/') response = self.fetch('/')
self.assertEqual(response.code, 204) self.assertEqual(response.code, 204)
expected = 'testing.timers.{}.SimpleHandler.GET.204'.format( expected = 'testing.timers.SimpleHandler.GET.204'
socket.gethostname().replace('.', '-'))
for path, value, stat_type in self.statsd.find_metrics(expected, 'ms'): for path, value, stat_type in self.statsd.find_metrics(expected, 'ms'):
assert_between(250.0, float(value), 500.0) assert_between(250.0, float(value), 500.0)
@ -68,8 +68,7 @@ class StatsdMetricCollectionTests(testing.AsyncHTTPTestCase):
response = self.fetch('/', method='POST', body='') response = self.fetch('/', method='POST', body='')
self.assertEqual(response.code, 204) self.assertEqual(response.code, 204)
prefix = 'testing.counters.{}.request.path'.format( prefix = 'testing.counters.request.path'
socket.gethostname().replace('.', '-'))
for path, value, stat_type in self.statsd.find_metrics(prefix, 'c'): for path, value, stat_type in self.statsd.find_metrics(prefix, 'c'):
self.assertEqual(int(value), 1) self.assertEqual(int(value), 1)
@ -77,8 +76,7 @@ class StatsdMetricCollectionTests(testing.AsyncHTTPTestCase):
response = self.fetch('/counters/path/5', method='POST', body='') response = self.fetch('/counters/path/5', method='POST', body='')
self.assertEqual(response.code, 204) self.assertEqual(response.code, 204)
prefix = 'testing.counters.{}.path'.format( prefix = 'testing.counters.path'
socket.gethostname().replace('.', '-'))
for path, value, stat_type in self.statsd.find_metrics(prefix, 'c'): for path, value, stat_type in self.statsd.find_metrics(prefix, 'c'):
self.assertEqual(int(value), 5) self.assertEqual(int(value), 5)
@ -86,8 +84,7 @@ class StatsdMetricCollectionTests(testing.AsyncHTTPTestCase):
response = self.fetch('/counters/one.two.three/0.25') response = self.fetch('/counters/one.two.three/0.25')
self.assertEqual(response.code, 204) self.assertEqual(response.code, 204)
prefix = 'testing.timers.{}.one.two.three'.format( prefix = 'testing.timers.one.two.three'
socket.gethostname().replace('.', '-'))
for path, value, stat_type in self.statsd.find_metrics(prefix, 'ms'): for path, value, stat_type in self.statsd.find_metrics(prefix, 'ms'):
assert_between(250.0, float(value), 300.0) assert_between(250.0, float(value), 300.0)
@ -114,8 +111,7 @@ class StatsdConfigurationTests(testing.AsyncHTTPTestCase):
statsd.install(self.application, **{'namespace': 'testing', statsd.install(self.application, **{'namespace': 'testing',
'host': self.statsd.sockaddr[0], 'host': self.statsd.sockaddr[0],
'port': self.statsd.sockaddr[1], 'port': self.statsd.sockaddr[1],
'prepend_metric_type': False, 'prepend_metric_type': False})
'prepend_hostname': False})
def tearDown(self): def tearDown(self):
self.statsd.close() self.statsd.close()