diff --git a/vumi/blinkenlights/metrics.py b/vumi/blinkenlights/metrics.py index cc7b28eed..dc3bf61fc 100644 --- a/vumi/blinkenlights/metrics.py +++ b/vumi/blinkenlights/metrics.py @@ -68,7 +68,7 @@ def start(self, channel=None): elif channel is not None: raise RuntimeError( "channel must not be provided if publisher is present.") - self._task = LoopingCall(self._publish_metrics) + self._task = LoopingCall(self.publish_metrics) done = self._task.start(self._publish_interval, now=False) done.addErrback(lambda failure: log.err(failure, "MetricManager publishing task died")) @@ -79,14 +79,24 @@ def stop(self): self._task.stop() self._task = None - def _publish_metrics(self): + def publish_metrics(self, publisher=None): + """ + Publish all waiting metrics. + + If a publisher is provided, it will be used instead of the manager's + publisher. + """ msg = MetricMessage() self._collect_oneshot_metrics(msg) self._collect_polled_metrics(msg) - self.publish_message(msg) + if publisher is None: + publisher = self._publisher + publisher.publish_message(msg) if self._on_publish is not None: self._on_publish(self) + _publish_metrics = publish_metrics # For old tests that poke this. + def _collect_oneshot_metrics(self, msg): oneshots, self._oneshot_msgs = self._oneshot_msgs, [] for metric, values in oneshots: @@ -96,12 +106,6 @@ def _collect_polled_metrics(self, msg): for metric in self._metrics: msg.append((self.prefix + metric.name, metric.aggs, metric.poll())) - def publish_message(self, msg): - """ - Compat wrapper for tests that assume this is a publisher. - """ - return self._publisher.publish_message(msg) - def oneshot(self, metric, value): """Publish a single value for the given metric. @@ -116,19 +120,6 @@ def oneshot(self, metric, value): self._oneshot_msgs.append( (metric, [(int(time.time()), value)])) - def publish_oneshot_metrics(self, publisher=None): - """ - Publish all waiting oneshot metrics. - - If a publisher is provided, it will be used instead of the manager's - publisher. - """ - msg = MetricMessage() - self._collect_oneshot_metrics(msg) - if publisher is None: - publisher = self._publisher - publisher.publish_message(msg) - def register(self, metric): """Register a new metric object to be managed by this metric set. diff --git a/vumi/blinkenlights/tests/test_metrics.py b/vumi/blinkenlights/tests/test_metrics.py index 816ef31bc..2388a7a6d 100644 --- a/vumi/blinkenlights/tests/test_metrics.py +++ b/vumi/blinkenlights/tests/test_metrics.py @@ -171,7 +171,7 @@ def test_publish_metrics_poll(self): yield self.start_manager(mm) cnt.inc() - mm._publish_metrics() + mm.publish_metrics() self._check_msg(mm, cnt, [1]) @inlineCallbacks @@ -181,7 +181,7 @@ def test_publish_metrics_oneshot(self): yield self.start_manager(mm) mm.oneshot(cnt, 1) - mm._publish_metrics() + mm.publish_metrics() self._check_msg(mm, cnt, [1]) @inlineCallbacks @@ -203,25 +203,25 @@ def test_start(self): self._check_msg(mm, cnt, [1, 1]) @inlineCallbacks - def test_publish_oneshot_metrics(self): + def test_publish_metrics(self): mm = metrics.MetricManager("vumi.test.", 0.1, self.on_publish) cnt = metrics.Count("my.count") yield self.start_manager(mm) mm.oneshot(cnt, 1) self.assertEqual(len(mm._oneshot_msgs), 1) - mm.publish_oneshot_metrics() + mm.publish_metrics() self.assertEqual(mm._oneshot_msgs, []) self._check_msg(mm, cnt, [1]) - def test_publish_oneshot_metrics_not_started_no_publisher(self): + def test_publish_metrics_not_started_no_publisher(self): mm = metrics.MetricManager("vumi.test.") self.assertEqual(mm._publisher, None) mm.oneshot(metrics.Count("my.count"), 1) - self.assertRaises(Exception, mm.publish_oneshot_metrics) + self.assertRaises(Exception, mm.publish_metrics) @inlineCallbacks - def test_publish_oneshot_metrics_not_started_with_publisher(self): + def test_publish_metrics_not_started_with_publisher(self): mm = metrics.MetricManager("vumi.test.", 0.1, self.on_publish) publisher = metrics.MetricPublisher() channel = yield get_stubbed_channel(self.worker_helper.broker) @@ -231,7 +231,7 @@ def test_publish_oneshot_metrics_not_started_with_publisher(self): self.assertEqual(mm._publisher, None) mm.oneshot(cnt, 1) self.assertEqual(len(mm._oneshot_msgs), 1) - mm.publish_oneshot_metrics(publisher) + mm.publish_metrics(publisher) self.assertEqual(mm._oneshot_msgs, []) self._check_msg(mm, cnt, [1])