diff --git a/sprockets/mixins/metrics/statsd.py b/sprockets/mixins/metrics/statsd.py index 3889e2c..41d3607 100644 --- a/sprockets/mixins/metrics/statsd.py +++ b/sprockets/mixins/metrics/statsd.py @@ -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. diff --git a/tests.py b/tests.py index 4b0f2f2..cb2b024 100644 --- a/tests.py +++ b/tests.py @@ -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)