0
0
Fork 1
mirror of https://mau.dev/maunium/synapse.git synced 2024-11-16 15:01:23 +01:00

Implement vector CallbackMetrics

This commit is contained in:
Paul "LeoNerd" Evans 2015-03-04 17:58:10 +00:00
parent 849300bc73
commit 23ab0c68c2
2 changed files with 23 additions and 3 deletions

View file

@ -79,9 +79,13 @@ class CallbackMetric(BaseMetric):
self.callback = callback self.callback = callback
def render(self): def render(self):
# TODO(paul): work out something we can do with keys and vectors value = self.callback()
return ["%s %d" % (self.name, self.callback())]
if self.is_scalar():
return ["%s %d" % (self.name, value)]
return ["%s{%s} %d" % (self.name, self._render_key(k), value[k])
for k in sorted(value.keys())]
class CacheMetric(object): class CacheMetric(object):
"""A combination of two CounterMetrics, one to count cache hits and one to """A combination of two CounterMetrics, one to count cache hits and one to

View file

@ -65,7 +65,7 @@ class CounterMetricTestCase(unittest.TestCase):
class CallbackMetricTestCase(unittest.TestCase): class CallbackMetricTestCase(unittest.TestCase):
def test_callback(self): def test_scalar(self):
d = dict() d = dict()
metric = CallbackMetric("size", lambda: len(d)) metric = CallbackMetric("size", lambda: len(d))
@ -80,6 +80,22 @@ class CallbackMetricTestCase(unittest.TestCase):
"size 1", "size 1",
]) ])
def test_vector(self):
vals = dict()
metric = CallbackMetric("values", lambda: vals, keys=["type"])
self.assertEquals(metric.render(), [])
# Keys have to be tuples, even if they're 1-element
vals[("foo",)] = 1
vals[("bar",)] = 2
self.assertEquals(metric.render(), [
"values{type=bar} 2",
"values{type=foo} 1",
])
class CacheMetricTestCase(unittest.TestCase): class CacheMetricTestCase(unittest.TestCase):