import inspect

from threading import RLock

from metrology.instruments import Counter, Meter, Timer, UtilizationTimer, HistogramUniform


class Registry(object):
    def __init__(self):
        self.lock = RLock()
        self.metrics = {}

    def clear(self):
        with self.lock:
            for metric in self.metrics.values():
                if hasattr(metric, 'stop'):
                    metric.stop()
            self.metrics.clear()

    def counter(self, name):
        return self.add_or_get(name, Counter)

    def meter(self, name):
        return self.add_or_get(name, Meter)

    def gauge(self, name, klass):
        return self.add_or_get(name, klass)

    def timer(self, name):
        return self.add_or_get(name, Timer)

    def utilization_timer(self, name):
        return self.add_or_get(name, UtilizationTimer)

    def health_check(self, name, klass):
        return self.add_or_get(name, klass)

    def histogram(self, name, klass=None):
        if not klass:
            klass = HistogramUniform
        return self.add_or_get(name, klass)

    def get(self, name):
        with self.lock:
            return self.metrics[name]

    def add(self, name, metric):
        with self.lock:
            if name in self.metrics:
                raise RuntimeError("%s already present in the registry." % name)
            else:
                self.metrics[name] = metric

    def add_or_get(self, name, klass):
        with self.lock:
            metric = self.metrics.get(name)
            if metric is not None:
                if not isinstance(metric, klass):
                    raise RuntimeError("%s is not of type %s." % (name, klass))
            else:
                if inspect.isclass(klass):
                    metric = klass()
                else:
                    metric = klass
                self.metrics[name] = metric
            return metric

    def stop(self):
        self.clear()

    def __iter__(self):
        with self.lock:
            for name, metric in self.metrics.items():
                yield name, metric

registry = Registry()

@@ -1286,18 +1286,19 @@
 eError(%22
-%25s
+%7B0%7D
  already
@@ -1327,16 +1327,22 @@
 ry.%22
- %25 
+.format(
 name)
+)
 %0A   
@@ -1633,18 +1633,19 @@
 eError(%22
-%25s
+%7B0%7D
  is not 
@@ -1656,15 +1656,20 @@
 ype 
-%25s.%22 %25 
+%7B1%7D.%22.format
 (nam

use new format