lib/prometheus/client/registry.rb in prometheus-client-0.2.0 vs lib/prometheus/client/registry.rb in prometheus-client-0.3.0.pre.rc.1

- old
+ new

@@ -1,61 +1,59 @@ -require 'json' +# encoding: UTF-8 + require 'thread' -require 'prometheus/client/container' require 'prometheus/client/counter' require 'prometheus/client/summary' require 'prometheus/client/gauge' module Prometheus module Client # Registry - # - # class Registry class AlreadyRegisteredError < StandardError; end - def initialize() - @containers = {} + def initialize + @metrics = {} @mutex = Mutex.new end - def register(name, docstring, metric, base_labels = {}) - container = Container.new(name, docstring, metric, base_labels) + def register(metric) + name = metric.name @mutex.synchronize do - if exist?(name) - raise AlreadyRegisteredError, "#{name} has already been registered" + if exist?(name.to_sym) + fail AlreadyRegisteredError, "#{name} has already been registered" else - @containers[name] = container + @metrics[name.to_sym] = metric end end - container + metric end def counter(name, docstring, base_labels = {}) - register(name, docstring, Counter.new, base_labels).metric + register(Counter.new(name, docstring, base_labels)) end def summary(name, docstring, base_labels = {}) - register(name, docstring, Summary.new, base_labels).metric + register(Summary.new(name, docstring, base_labels)) end def gauge(name, docstring, base_labels = {}) - register(name, docstring, Gauge.new, base_labels).metric + register(Gauge.new(name, docstring, base_labels)) end def exist?(name) - @containers.has_key?(name) + @metrics.key?(name) end def get(name) - @containers[name] + @metrics[name.to_sym] end - def to_json(*json) - @containers.values.to_json(*json) + def metrics + @metrics.values end end end end