MOBILE-9220 added newline to tcp messages, added tests

This commit is contained in:
Dan g 2018-07-27 10:14:32 -04:00
parent b5d1cae249
commit 6fd2cf4035
2 changed files with 20 additions and 0 deletions

View file

@ -162,6 +162,7 @@ class StatsDCollector(object):
"""
msg = '{0}:{1}|{2}'.format(
self._build_path(path, metric_type), value, metric_type)
msg = self._build_udp_or_tcp_message(msg)
LOGGER.debug('Sending %s to %s:%s', msg.encode('ascii'),
self._host, self._port)
@ -174,6 +175,12 @@ class StatsDCollector(object):
except (OSError, socket.error) as error: # pragma: nocover
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):
"""Return a normalized path.

View file

@ -63,6 +63,13 @@ class TCPStatsdMetricCollectionTests(testing.AsyncHTTPTestCase):
'protocol': 'tcp',
'prepend_metric_type': True})
def test_that_tcp_message_appends_a_newline(self):
orig = 'testing.timers.SimpleHandler.GET.204'
expected = 'testing.timers.SimpleHandler.GET.204\n'
msg = self.application.statsd._build_udp_or_tcp_message(orig)
self.assertEqual(msg, expected)
def test_that_http_method_call_is_recorded(self):
response = self.fetch('/')
self.assertEqual(response.code, 204)
@ -171,6 +178,12 @@ class UDPStatsdMetricCollectionTests(testing.AsyncHTTPTestCase):
self.statsd.close()
super(UDPStatsdMetricCollectionTests, self).tearDown()
def test_that_udp_message_is_unchanged(self):
expected = 'testing.timers.SimpleHandler.GET.204'
msg = self.application.statsd._build_udp_or_tcp_message(expected)
self.assertEqual(msg, expected)
def test_that_http_method_call_is_recorded(self):
response = self.fetch('/')
self.assertEqual(response.code, 204)