Merge lp:~bloodearnest/txstatsd/multi-metric-packets into lp:txstatsd

Proposed by Simon Davy on 2016-04-18
Status: Merged
Approved by: Wes Mason on 2016-04-18
Approved revision: 114
Merged at revision: 113
Proposed branch: lp:~bloodearnest/txstatsd/multi-metric-packets
Merge into: lp:txstatsd
Diff against target: 79 lines (+45/-14)
2 files modified
txstatsd/server/processor.py (+18/-14)
txstatsd/tests/test_processor.py (+27/-0)
To merge this branch: bzr merge lp:~bloodearnest/txstatsd/multi-metric-packets
Reviewer Review Type Date Requested Status
Wes Mason (community) 2016-04-18 Approve on 2016-04-18
Review via email: mp+292131@code.launchpad.net

Commit message

Support newline separated multiple metrics in single packet, like statsd proper.

Description of the change

Support newline separated multiple metrics in single packet, like statsd proper.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'txstatsd/server/processor.py'
2--- txstatsd/server/processor.py 2014-10-03 19:52:29 +0000
3+++ txstatsd/server/processor.py 2016-04-18 10:21:39 +0000
4@@ -51,20 +51,24 @@
5 def process(self, message):
6 """
7 """
8- if not ":" in message:
9- return self.fail(message)
10-
11- key, data = message.strip().split(":", 1)
12- if not "|" in data:
13- return self.fail(message)
14-
15- fields = data.split("|")
16- if len(fields) < 2 or len(fields) > 3:
17- return self.fail(message)
18-
19- key = normalize_key(key)
20- metric_type = fields[1]
21- return self.process_message(message, metric_type, key, fields)
22+ for msg in message.strip().split('\n'):
23+ if ":" not in msg:
24+ self.fail(msg)
25+ continue
26+
27+ key, data = msg.strip().split(":", 1)
28+ if "|" not in data:
29+ self.fail(msg)
30+ continue
31+
32+ fields = data.split("|")
33+ if len(fields) < 2 or len(fields) > 3:
34+ self.fail(msg)
35+ continue
36+
37+ key = normalize_key(key)
38+ metric_type = fields[1]
39+ self.process_message(msg, metric_type, key, fields)
40
41 def rebuild_message(self, metric_type, key, fields):
42 return key + ":" + "|".join(fields)
43
44=== modified file 'txstatsd/tests/test_processor.py'
45--- txstatsd/tests/test_processor.py 2014-10-03 19:52:29 +0000
46+++ txstatsd/tests/test_processor.py 2016-04-18 10:21:39 +0000
47@@ -236,6 +236,33 @@
48 self.assertEqual(0, len(self.processor.counter_metrics))
49 self.assertEqual(["gorets:1|c|@0.1|yay"], self.processor.failures)
50
51+ def test_receive_traling_newline(self):
52+ self.processor.process("gorets:1|c\n")
53+ self.assertEqual(1, len(self.processor.counter_metrics))
54+ self.assertEqual([], self.processor.failures)
55+
56+ def test_multimetric_message(self):
57+ self.processor.process("foo:1|c\nbar:100|ms\nbaz:10|g")
58+ self.assertEqual(1, len(self.processor.counter_metrics))
59+ self.assertEqual(1.0, self.processor.counter_metrics["foo"])
60+ self.assertEqual(1, len(self.processor.timer_metrics))
61+ self.assertEqual([100], self.processor.timer_metrics["bar"])
62+ self.assertEqual(1, len(self.processor.gauge_metrics))
63+ self.assertEqual(
64+ [10.0, 'baz'],
65+ self.processor.gauge_metrics.pop())
66+
67+ def test_multimetric_bad_value(self):
68+ self.processor.process("foo:1|c\nbar:|ms\nbaz:10|g")
69+ self.assertEqual(1, len(self.processor.counter_metrics))
70+ self.assertEqual(1.0, self.processor.counter_metrics["foo"])
71+ self.assertEqual(["bar:|ms"], self.processor.failures)
72+ self.assertEqual(1, len(self.processor.gauge_metrics))
73+ self.assertEqual(
74+ [10.0, 'baz'],
75+ self.processor.gauge_metrics.pop())
76+
77+
78
79 class ProcessorStatsTest(TestCase):
80

Subscribers

People subscribed via source and target branches