Skip to content

Commit

Permalink
publish_oneshot_metrics -> publish_metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
jerith committed Apr 8, 2014
1 parent 987fcf3 commit 96a0fff
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 30 deletions.
35 changes: 13 additions & 22 deletions vumi/blinkenlights/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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:
Expand All @@ -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.
Expand All @@ -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.
Expand Down
16 changes: 8 additions & 8 deletions vumi/blinkenlights/tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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])

Expand Down

0 comments on commit 96a0fff

Please sign in to comment.