Set format strings ass attributes, update tests

This commit is contained in:
Dan g 2018-07-27 13:51:02 -04:00
parent 6fd2cf4035
commit 5599e9f73b
2 changed files with 12 additions and 20 deletions

View file

@ -123,9 +123,11 @@ class StatsDCollector(object):
if protocol == 'tcp': if protocol == 'tcp':
self._tcp = True self._tcp = True
self._msg_format = '{path}:{value}|{metric_type}\n'
self._sock = self._tcp_socket() self._sock = self._tcp_socket()
elif protocol == 'udp': elif protocol == 'udp':
self._tcp = False self._tcp = False
self._msg_format = '{path}:{value}|{metric_type}'
self._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0) self._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
else: else:
raise ValueError('Invalid protocol: {}'.format(protocol)) raise ValueError('Invalid protocol: {}'.format(protocol))
@ -160,9 +162,10 @@ class StatsDCollector(object):
:param str metric_type: The metric type :param str metric_type: The metric type
""" """
msg = '{0}:{1}|{2}'.format( msg = self._msg_format.format(
self._build_path(path, metric_type), value, metric_type) path=self._build_path(path, metric_type),
msg = self._build_udp_or_tcp_message(msg) value=value,
metric_type=metric_type)
LOGGER.debug('Sending %s to %s:%s', msg.encode('ascii'), LOGGER.debug('Sending %s to %s:%s', msg.encode('ascii'),
self._host, self._port) self._host, self._port)
@ -175,12 +178,6 @@ class StatsDCollector(object):
except (OSError, socket.error) as error: # pragma: nocover except (OSError, socket.error) as error: # pragma: nocover
LOGGER.exception('Error sending statsd metric: %s', error) LOGGER.exception('Error sending statsd metric: %s', error)
def _build_udp_or_tcp_message(self, msg):
if self._tcp is False:
return msg
return msg + "\n"
def _build_path(self, path, metric_type): def _build_path(self, path, metric_type):
"""Return a normalized path. """Return a normalized path.

View file

@ -63,12 +63,9 @@ class TCPStatsdMetricCollectionTests(testing.AsyncHTTPTestCase):
'protocol': 'tcp', 'protocol': 'tcp',
'prepend_metric_type': True}) 'prepend_metric_type': True})
def test_that_tcp_message_appends_a_newline(self): def test_tcp_message_format(self):
orig = 'testing.timers.SimpleHandler.GET.204' expected = '{path}:{value}|{metric_type}\n'
expected = 'testing.timers.SimpleHandler.GET.204\n' self.assertEqual(self.application.statsd._msg_format, expected)
msg = self.application.statsd._build_udp_or_tcp_message(orig)
self.assertEqual(msg, expected)
def test_that_http_method_call_is_recorded(self): def test_that_http_method_call_is_recorded(self):
response = self.fetch('/') response = self.fetch('/')
@ -178,11 +175,9 @@ class UDPStatsdMetricCollectionTests(testing.AsyncHTTPTestCase):
self.statsd.close() self.statsd.close()
super(UDPStatsdMetricCollectionTests, self).tearDown() super(UDPStatsdMetricCollectionTests, self).tearDown()
def test_that_udp_message_is_unchanged(self): def test_udp_message_format(self):
expected = 'testing.timers.SimpleHandler.GET.204' expected = '{path}:{value}|{metric_type}'
self.assertEqual(self.application.statsd._msg_format, expected)
msg = self.application.statsd._build_udp_or_tcp_message(expected)
self.assertEqual(msg, expected)
def test_that_http_method_call_is_recorded(self): def test_that_http_method_call_is_recorded(self):
response = self.fetch('/') response = self.fetch('/')