Skip to content

Commit

Permalink
Fix parsing of StatsD metrics with Py3
Browse files Browse the repository at this point in the history
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
(cherry picked from commit a2400fc)
  • Loading branch information
dougszumski committed Jun 1, 2020
1 parent 7a7e5e6 commit 63e0c66
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
3 changes: 3 additions & 0 deletions monasca_agent/statsd/udp.py
Expand Up @@ -16,6 +16,8 @@
import select
import socket

from oslo_utils import encodeutils

import monasca_agent.common.metrics as metrics_pkg

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -159,6 +161,7 @@ def _parse_dogstatsd_tags(statsd_msg):
def submit_packets(self, packets):
for packet in packets.split(b"\n"):

packet = encodeutils.safe_decode(packet)
if not packet.strip():
continue

Expand Down
49 changes: 49 additions & 0 deletions tests/test_statsd.py
@@ -0,0 +1,49 @@
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
)
])

0 comments on commit 63e0c66

Please sign in to comment.