Sha256: 4558677ebb9f8c8b35a866d68e212e19e710b5a3ee9830206cd83d98105d0d46

Contents?: true

Size: 1.35 KB

Versions: 8

Compression:

Stored size: 1.35 KB

Contents

module PromMultiProc
  class Proxy
    attr_reader :multis

    def initialize(base)
      @base = base
      @proxies = {}
      @multis = []

      add_proxy_methods
    end

    def add_multi(collector, method, value, labels)
      @multis << [collector, method, value, labels]
    end

  private

    def add_proxy_methods
      @base.metrics.each do |name|
        @proxies[name] = ProxyCollector.new(self, @base.metric(name))
        define_singleton_method(name) do
          @proxies[name]
        end
      end
    end
  end

  class ProxyCollector
    def initialize(proxy, collector)
      @proxy = proxy
      @collector = collector

      add_proxy_methods
    end

  private

    def add_proxy_methods
      @collector.metric_methods.each do |meth|
        define_singleton_method(meth) do |*args|
          case args.length
          when 0
            value = 1.0
            labels = {}
          when 1
            if args[0].is_a?(Hash)
              value = 1.0
              labels = args[0]
            else
              value = args[0]
              labels = {}
            end
          when 2
            value = args[0]
            labels = args[1]
          else
            raise PromMultiProcError.new("Invalid number of arguments")
          end

          @proxy.add_multi(@collector, meth, value, labels)
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
prom_multi_proc_rb-0.1.7 lib/prom_multi_proc/proxy.rb
prom_multi_proc_rb-0.1.6 lib/prom_multi_proc/proxy.rb
prom_multi_proc_rb-0.1.5 lib/prom_multi_proc/proxy.rb
prom_multi_proc_rb-0.1.4 lib/prom_multi_proc/proxy.rb
prom_multi_proc_rb-0.1.3 lib/prom_multi_proc/proxy.rb
prom_multi_proc_rb-0.1.2 lib/prom_multi_proc/proxy.rb
prom_multi_proc_rb-0.1.1 lib/prom_multi_proc/proxy.rb
prom_multi_proc_rb-0.1.0 lib/prom_multi_proc/proxy.rb