summary refs log tree commit diff
path: root/synapse/metrics
diff options
context:
space:
mode:
authorPaul "LeoNerd" Evans <paul@matrix.org>2015-03-04 19:28:17 +0000
committerPaul "LeoNerd" Evans <paul@matrix.org>2015-03-12 16:24:50 +0000
commita99d6edc05c60cdb473adbe5fc783532e0ff9ea3 (patch)
treef8916b79b50164fdbe415e01a694c10f613f3049 /synapse/metrics
parentInitial hack at a TimerMetric; for storing counts + duration accumulators (diff)
downloadsynapse-a99d6edc05c60cdb473adbe5fc783532e0ff9ea3.tar.xz
Neater implementation of metric render methods by pulling out 'render' as a base method that calls self.render_item
Diffstat (limited to 'synapse/metrics')
-rw-r--r--synapse/metrics/metric.py33
1 files changed, 15 insertions, 18 deletions
diff --git a/synapse/metrics/metric.py b/synapse/metrics/metric.py
index 7175881941..4a6ab9cd74 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(k):
-            keystr = self._render_key(k)
+    def render_item(self, 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):