From a99d6edc05c60cdb473adbe5fc783532e0ff9ea3 Mon Sep 17 00:00:00 2001 From: "Paul \"LeoNerd\" Evans" Date: Wed, 4 Mar 2015 19:28:17 +0000 Subject: [PATCH] Neater implementation of metric render methods by pulling out 'render' as a base method that calls self.render_item --- synapse/metrics/metric.py | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/synapse/metrics/metric.py b/synapse/metrics/metric.py index 717588194..4a6ab9cd7 100644 --- a/synapse/metrics/metric.py +++ b/synapse/metrics/metric.py @@ -36,8 +36,15 @@ class BaseMetric(object): return not len(self.keys) def _render_key(self, values): + if self.is_scalar(): + return "" # TODO: some kind of value escape - return ",".join(["%s=%s" % kv for kv in zip(self.keys, values)]) + return "{%s}" % ( + ",".join(["%s=%s" % kv for kv in zip(self.keys, values)]) + ) + + def render(self): + return map_concat(self.render_item, sorted(self.counts.keys())) class CounterMetric(BaseMetric): @@ -69,12 +76,8 @@ class CounterMetric(BaseMetric): def fetch(self): return dict(self.counts) - def render(self): - if self.is_scalar(): - return ["%s %d" % (self.name, self.counts[()])] - - return ["%s{%s} %d" % (self.name, self._render_key(k), self.counts[k]) - for k in sorted(self.counts.keys())] + def render_item(self, k): + return ["%s%s %d" % (self.name, self._render_key(k), self.counts[k])] class CallbackMetric(BaseMetric): @@ -93,7 +96,7 @@ class CallbackMetric(BaseMetric): if self.is_scalar(): return ["%s %d" % (self.name, value)] - return ["%s{%s} %d" % (self.name, self._render_key(k), value[k]) + return ["%s%s %d" % (self.name, self._render_key(k), value[k]) for k in sorted(value.keys())] @@ -121,18 +124,12 @@ class TimerMetric(CounterMetric): else: self.times[values] += msec - def render(self): - if self.is_scalar(): - return ["%s:count %d" % (self.name, self.counts[()]), - "%s:msec %d" % (self.name, self.times[()])] + def render_item(self, k): + keystr = self._render_key(k) - def render_item(k): - keystr = self._render_key(k) + return ["%s%s:count %d" % (self.name, keystr, self.counts[k]), + "%s%s:msec %d" % (self.name, keystr, self.times[k])] - return ["%s{%s}:count %d" % (self.name, keystr, self.counts[k]), - "%s{%s}:msec %d" % (self.name, keystr, self.times[k])] - - return map_concat(render_item, sorted(self.counts.keys())) class CacheMetric(object):