From 849300bc736b82a6bcefdae110f5bf9111141afb Mon Sep 17 00:00:00 2001 From: "Paul \"LeoNerd\" Evans" Date: Wed, 4 Mar 2015 17:53:51 +0000 Subject: Neater introspection methods on BaseMetric so that subclasses don't need to touch self.keys directly --- synapse/metrics/metric.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'synapse/metrics') 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. -- cgit 1.4.1