summary refs log tree commit diff
path: root/synapse
diff options
context:
space:
mode:
authorPaul "LeoNerd" Evans <paul@matrix.org>2015-03-04 17:53:51 +0000
committerPaul "LeoNerd" Evans <paul@matrix.org>2015-03-12 16:24:50 +0000
commit849300bc736b82a6bcefdae110f5bf9111141afb (patch)
treedf553b1ab5a7b5091fceffaa4f32cdd21da94b39 /synapse
parentRename CacheCounterMetric to just CacheMetric; add a CallbackMetric component... (diff)
downloadsynapse-849300bc736b82a6bcefdae110f5bf9111141afb.tar.xz
Neater introspection methods on BaseMetric so that subclasses don't need to touch self.keys directly
Diffstat (limited to '')
-rw-r--r--synapse/metrics/metric.py15
1 files changed, 11 insertions, 4 deletions
diff --git a/synapse/metrics/metric.py b/synapse/metrics/metric.py
index 7e47f86155..e8c15a60e8 100644
--- a/synapse/metrics/metric.py
+++ b/synapse/metrics/metric.py
@@ -20,6 +20,12 @@ class BaseMetric(object):
         self.name = name
         self.keys = keys # OK not to clone as we never write it
 
+    def dimension(self):
+        return len(self.keys)
+
+    def is_scalar(self):
+        return not len(self.keys)
+
     def _render_key(self, values):
         # TODO: some kind of value escape
         return ",".join(["%s=%s" % kv for kv in zip(self.keys, values)])
@@ -35,13 +41,13 @@ class CounterMetric(BaseMetric):
         self.counts = {}
 
         # Scalar metrics are never empty
-        if not len(self.keys):
+        if self.is_scalar():
             self.counts[()] = 0
 
     def inc(self, *values):
-        if len(values) != len(self.keys):
+        if len(values) != self.dimension():
             raise ValueError("Expected as many values to inc() as keys (%d)" %
-                (len(self.keys))
+                (self.dimension())
             )
 
         # TODO: should assert that the tag values are all strings
@@ -55,7 +61,7 @@ class CounterMetric(BaseMetric):
         return dict(self.counts)
 
     def render(self):
-        if not len(self.keys):
+        if self.is_scalar():
             return ["%s %d" % (self.name, self.counts[()])]
 
         return ["%s{%s} %d" % (self.name, self._render_key(k), self.counts[k])
@@ -76,6 +82,7 @@ class CallbackMetric(BaseMetric):
         # TODO(paul): work out something we can do with keys and vectors
         return ["%s %d" % (self.name, self.callback())]
 
+
 class CacheMetric(object):
     """A combination of two CounterMetrics, one to count cache hits and one to
     count misses, and a callback metric to yield the current size.