Fix parsing of StatsD metrics with Py3

When running with Py3 we compare a byte string to a unicode string
when parsing StatsD metrics. This patch adds some unit tests to
reproduce the bug and decodes the bytestring to make the existing
comparisons valid under Py3. When backporting to Train we can use
Oslo encodeutils. Clearly we could have more unit tests, but
this makes a start.

Change-Id: I6341f96f5c186428d2d829cabf618a6f84f40ce2
Story: 2007684
Task: 39796
This commit is contained in:
Doug Szumski 2020-05-21 21:42:20 +01:00
parent 579dc2c6c5
commit a2400fcf12
2 changed files with 50 additions and 0 deletions

View File

@ -159,6 +159,7 @@ class Server(object):
def submit_packets(self, packets):
for packet in packets.split(b"\n"):
packet = packet.decode("utf-8")
if not packet.strip():
continue

49
tests/test_statsd.py Normal file
View File

@ -0,0 +1,49 @@
from unittest import mock
import unittest
import monasca_agent.common.metrics as metrics_pkg
import monasca_agent.statsd.udp as udp
class TestStatsd(unittest.TestCase):
def testSubmitPacket(self):
mock_aggregator = mock.Mock()
srv = udp.Server(mock_aggregator, 'localhost', 1234)
test_packet = b"monasca.log.out_logs_truncated_bytes:0|g|#" \
b"{'service': 'monitoring', 'component': 'monasca-log-api'}"
srv.submit_packets(test_packet)
mock_aggregator.submit_metric.assert_called_once_with(
'monasca.log.out_logs_truncated_bytes',
0,
metrics_pkg.Gauge,
dimensions={
'service': 'monitoring',
'component': 'monasca-log-api'},
sample_rate=1)
def testSubmitPackets(self):
mock_aggregator = mock.Mock()
srv = udp.Server(mock_aggregator, 'localhost', 1234)
test_packet = b"monasca.log.out_logs_truncated_bytes:0|g|#" \
b"{'service': 'monitoring', 'component': 'monasca-log-api'}\n" \
b"application_metric:10|c|#{'service': 'workload'}"
srv.submit_packets(test_packet)
mock_aggregator.submit_metric.assert_has_calls([
mock.call(
'monasca.log.out_logs_truncated_bytes',
0,
metrics_pkg.Gauge,
dimensions={
'service': 'monitoring',
'component': 'monasca-log-api'},
sample_rate=1
),
mock.call(
'application_metric',
10,
metrics_pkg.Counter,
dimensions={
'service': 'workload'},
sample_rate=1
)
])