Skip to content

Instantly share code, notes, and snippets.

@justinvdm
Created November 6, 2013 08:44
Show Gist options
  • Select an option

  • Save justinvdm/7332886 to your computer and use it in GitHub Desktop.

Select an option

Save justinvdm/7332886 to your computer and use it in GitHub Desktop.
diff --git a/go/vumitools/metrics.py b/go/vumitools/metrics.py
index fa02967..751df3a 100644
--- a/go/vumitools/metrics.py
+++ b/go/vumitools/metrics.py
@@ -5,6 +5,10 @@ from vumi.blinkenlights.metrics import Metric
class GoMetric(object):
+ """
+ Encapsulates name retrieval, value retrieval and publishing for Go metrics.
+ """
+
AGGREGATORS = None
def __init__(self, name, aggregators=None):
@@ -14,23 +18,72 @@ class GoMetric(object):
def get_full_name(self):
"""
- This is for constructing the full, prefixed metric name in django land
- if a manager is not available.
+ This is for constructing the full, prefixed metric name in
+ *Django land* if a manager is not available.
"""
return settings.GO_METRICS_PREFIX + self.metric.name
def get_value(self):
- raise NotImplementedError(
- "ConversationMetric.value() needs to be overriden")
+ """
+ Should be overriden to return the value used when publishing the
+ metric.
+ """
+ raise NotImplementedError("GoMetric.get_value() needs to be overriden")
+
+ def get_aggregators(self):
+ return self.metric.aggregators
+
+ def oneshot(self, publisher, value=None):
+ """
+ Should do a once-off publish for the metric using a construct capable
+ of publishing metrics.
+ """
+ raise NotImplementedError("GoMetric.oneshot() needs to be overriden")
+
+
+class TxMetric(GoMetric):
+ """
+ Base for a metric publised in *Twisted land*.
+ """
@inlineCallbacks
def oneshot(self, manager, value=None):
+ """
+ Does a once-off publish for the metric using a `MetricManager` (as opposed
+ to relying on the manager to do periodic publishing).
+ """
if value is None:
value = self.get_value()
manager.oneshot(self.metric, (yield value))
-class ConversationMetric(GoMetric):
+class DjangoMetric(GoMetric):
+ """
+ Base for a metric publised in *Django land*.
+ """
+
+ def __init__(self, metric_name, aggregators=None):
+ name = self.make_name(metric_name)
+ super(DjangoMetric, self).__init__(name, aggregators)
+
+ @classmethod
+ def make_name(self, metric_name):
+ return "django.%s" % (metric_name,)
+
+ def oneshot(self, connection, value=None):
+ """
+ Does a once-off publish for the metric using an `AmqpConnection`.
+ """
+ if value is None:
+ value = self.get_value()
+
+ connection.publish_metric(
+ self.get_full_name(),
+ self.get_aggregators(),
+ value)
+
+
+class ConversationMetric(TxMetric):
METRIC_NAME = None
def __init__(self, conv):
@@ -43,7 +96,7 @@ class ConversationMetric(GoMetric):
conv.user_account.key, conv.key, cls.METRIC_NAME)
-class AccountMetric(GoMetric):
+class AccountMetric(TxMetric):
def __init__(self, account, store_name, metric_name, aggregators=None):
name = self.make_name(account, store_name, metric_name)
super(AccountMetric, self).__init__(name, aggregators)
@@ -53,13 +106,3 @@ class AccountMetric(GoMetric):
def make_name(self, account, store_name, metric_name):
return "campaigns.%s.stores.%s.%s" % (
account.key, store_name, metric_name)
-
-
-class DjangoMetric(GoMetric):
- def __init__(self, metric_name, aggregators=None):
- name = self.make_name(metric_name)
- super(DjangoMetric, self).__init__(name, aggregators)
-
- @classmethod
- def make_name(self, metric_name):
- return "django.%s" % (metric_name,)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment