Neater register_* methods on overall Metrics container

This commit is contained in:
Paul "LeoNerd" Evans 2015-03-04 19:43:46 +00:00
parent a99d6edc05
commit f52acf3b12
1 changed files with 12 additions and 22 deletions

View File

@ -15,7 +15,7 @@
import logging import logging
from .metric import CounterMetric, CallbackMetric, CacheMetric from .metric import CounterMetric, CallbackMetric, TimerMetric, CacheMetric
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -36,35 +36,25 @@ class Metrics(object):
def __init__(self, name): def __init__(self, name):
self.name_prefix = name self.name_prefix = name
def _register(self, metric): def _register(self, metric_class, name, *args, **kwargs):
all_metrics[metric.name] = metric
def register_counter(self, name, *args, **kwargs):
full_name = "%s.%s" % (self.name_prefix, name) full_name = "%s.%s" % (self.name_prefix, name)
metric = CounterMetric(full_name, *args, **kwargs) metric = metric_class(full_name, *args, **kwargs)
self._register(metric)
all_metrics[full_name] = metric
return metric return metric
def register_callback(self, name, callback, *args, **kwargs): def register_counter(self, *args, **kwargs):
full_name = "%s.%s" % (self.name_prefix, name) 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, *args, **kwargs):
return self._register(CacheMetric, *args, **kwargs)
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 counted(self, func): def counted(self, func):
""" A method decorator that registers a counter, to count invocations """ A method decorator that registers a counter, to count invocations