From f52acf3b129864f400876f9316e1324e62e75eb0 Mon Sep 17 00:00:00 2001 From: "Paul \"LeoNerd\" Evans" Date: Wed, 4 Mar 2015 19:43:46 +0000 Subject: [PATCH] Neater register_* methods on overall Metrics container --- synapse/metrics/__init__.py | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/synapse/metrics/__init__.py b/synapse/metrics/__init__.py index 442fd70cdf..e1818ce395 100644 --- a/synapse/metrics/__init__.py +++ b/synapse/metrics/__init__.py @@ -15,7 +15,7 @@ import logging -from .metric import CounterMetric, CallbackMetric, CacheMetric +from .metric import CounterMetric, CallbackMetric, TimerMetric, CacheMetric logger = logging.getLogger(__name__) @@ -36,35 +36,25 @@ class Metrics(object): def __init__(self, name): self.name_prefix = name - def _register(self, metric): - all_metrics[metric.name] = metric - - def register_counter(self, name, *args, **kwargs): + def _register(self, metric_class, name, *args, **kwargs): full_name = "%s.%s" % (self.name_prefix, name) - metric = CounterMetric(full_name, *args, **kwargs) - - self._register(metric) + metric = metric_class(full_name, *args, **kwargs) + all_metrics[full_name] = metric return metric - def register_callback(self, name, callback, *args, **kwargs): - full_name = "%s.%s" % (self.name_prefix, name) + def register_counter(self, *args, **kwargs): + return self._register(CounterMetric, *args, **kwargs) - metric = CallbackMetric(full_name, *args, callback=callback, **kwargs) + def register_callback(self, *args, **kwargs): + return self._register(CallbackMetric, *args, **kwargs) - self._register(metric) + def register_timer(self, *args, **kwargs): + return self._register(TimerMetric, *args, **kwargs) - return metric - - def register_cache(self, name, *args, **kwargs): - full_name = "%s.%s" % (self.name_prefix, name) - - metric = CacheMetric(full_name, *args, **kwargs) - - self._register(metric) - - return metric + def register_cache(self, *args, **kwargs): + return self._register(CacheMetric, *args, **kwargs) def counted(self, func): """ A method decorator that registers a counter, to count invocations