Sha256: bf6bf28fa94406e3eeca85eafe54c7f00000cd08fcb24a201116109232112460

Contents?: true

Size: 1.46 KB

Versions: 190

Compression:

Stored size: 1.46 KB

Contents

# encoding: UTF-8

require 'thread'

require 'prometheus/client/counter'
require 'prometheus/client/summary'
require 'prometheus/client/gauge'
require 'prometheus/client/histogram'

module Prometheus
  module Client
    # Registry
    class Registry
      class AlreadyRegisteredError < StandardError; end

      def initialize
        @metrics = {}
        @mutex = Mutex.new
      end

      def register(metric)
        name = metric.name

        @mutex.synchronize do
          if exist?(name.to_sym)
            raise AlreadyRegisteredError, "#{name} has already been registered"
          else
            @metrics[name.to_sym] = metric
          end
        end

        metric
      end

      def counter(name, docstring, base_labels = {})
        register(Counter.new(name, docstring, base_labels))
      end

      def summary(name, docstring, base_labels = {})
        register(Summary.new(name, docstring, base_labels))
      end

      def gauge(name, docstring, base_labels = {}, multiprocess_mode = :all)
        register(Gauge.new(name, docstring, base_labels, multiprocess_mode))
      end

      def histogram(name, docstring, base_labels = {},
                    buckets = Histogram::DEFAULT_BUCKETS)
        register(Histogram.new(name, docstring, base_labels, buckets))
      end

      def exist?(name)
        @metrics.key?(name)
      end

      def get(name)
        @metrics[name.to_sym]
      end

      def metrics
        @metrics.values
      end
    end
  end
end

Version data entries

190 entries across 190 versions & 2 rubygems

Version Path
prometheus-client-mmap-1.0.3-x86_64-linux lib/prometheus/client/registry.rb
prometheus-client-mmap-1.0.3-x86_64-darwin lib/prometheus/client/registry.rb
prometheus-client-mmap-1.0.3-arm64-darwin lib/prometheus/client/registry.rb
prometheus-client-mmap-1.0.3-aarch64-linux lib/prometheus/client/registry.rb
prometheus-client-mmap-1.0.2 lib/prometheus/client/registry.rb
prometheus-client-mmap-1.0.2-x86_64-linux lib/prometheus/client/registry.rb
prometheus-client-mmap-1.0.2-x86_64-darwin lib/prometheus/client/registry.rb
prometheus-client-mmap-1.0.2-arm64-darwin lib/prometheus/client/registry.rb
prometheus-client-mmap-1.0.2-aarch64-linux lib/prometheus/client/registry.rb
prometheus-client-mmap-1.0.1 lib/prometheus/client/registry.rb
prometheus-client-mmap-1.0.1-x86_64-linux lib/prometheus/client/registry.rb
prometheus-client-mmap-1.0.1-x86_64-darwin lib/prometheus/client/registry.rb
prometheus-client-mmap-1.0.1-arm64-darwin lib/prometheus/client/registry.rb
prometheus-client-mmap-1.0.1-aarch64-linux lib/prometheus/client/registry.rb
prometheus-client-mmap-1.0.0 lib/prometheus/client/registry.rb
prometheus-client-mmap-1.0.0-x86_64-linux lib/prometheus/client/registry.rb
prometheus-client-mmap-1.0.0-x86_64-darwin lib/prometheus/client/registry.rb
prometheus-client-mmap-1.0.0-arm64-darwin lib/prometheus/client/registry.rb
prometheus-client-mmap-1.0.0-aarch64-linux lib/prometheus/client/registry.rb
prometheus-client-mmap-0.28.1 lib/prometheus/client/registry.rb