Sha256: 4d88f6fa13f66aeab053e02f1427dfa52c2397733159969439305c96ba90673f

Contents?: true

Size: 1.99 KB

Versions: 183

Compression:

Stored size: 1.99 KB

Contents

require 'thread'
require 'prometheus/client/label_set_validator'
require 'prometheus/client/uses_value_type'

module Prometheus
  module Client
    class Metric
      include UsesValueType
      attr_reader :name, :docstring, :base_labels

      def initialize(name, docstring, base_labels = {})
        @mutex = Mutex.new
        @validator = case type
                       when :summary
                         LabelSetValidator.new(['quantile'])
                       when :histogram
                         LabelSetValidator.new(['le'])
                       else
                         LabelSetValidator.new
                     end
        @values = Hash.new { |hash, key| hash[key] = default(key) }

        validate_name(name)
        validate_docstring(docstring)
        @validator.valid?(base_labels)

        @name = name
        @docstring = docstring
        @base_labels = base_labels
      end

      # Returns the value for the given label set
      def get(labels = {})
        label_set = label_set_for(labels)
        @validator.valid?(label_set)

        @values[label_set].get
      end

      # Returns all label sets with their values
      def values
        synchronize do
          @values.each_with_object({}) do |(labels, value), memo|
            memo[labels] = value
          end
        end
      end

      private

      def touch_default_value
        @values[label_set_for({})]
      end

      def default(labels)
        value_object(type, @name, @name, labels)
      end

      def validate_name(name)
        return true if name.is_a?(Symbol)

        raise ArgumentError, 'given name must be a symbol'
      end

      def validate_docstring(docstring)
        return true if docstring.respond_to?(:empty?) && !docstring.empty?

        raise ArgumentError, 'docstring must be given'
      end

      def label_set_for(labels)
        @validator.validate(@base_labels.merge(labels))
      end

      def synchronize(&block)
        @mutex.synchronize(&block)
      end
    end
  end
end

Version data entries

183 entries across 183 versions & 2 rubygems

Version Path
prometheus-client-mmap-0.21.0 lib/prometheus/client/metric.rb
prometheus-client-mmap-0.21.0-x86_64-linux lib/prometheus/client/metric.rb
prometheus-client-mmap-0.21.0-x86_64-linux-musl lib/prometheus/client/metric.rb
prometheus-client-mmap-0.21.0-x86_64-darwin lib/prometheus/client/metric.rb
prometheus-client-mmap-0.21.0-arm64-darwin lib/prometheus/client/metric.rb
prometheus-client-mmap-0.21.0-aarch64-linux lib/prometheus/client/metric.rb
prometheus-client-mmap-0.20.3-x86_64-linux lib/prometheus/client/metric.rb
prometheus-client-mmap-0.20.3-x86_64-linux-musl lib/prometheus/client/metric.rb
prometheus-client-mmap-0.20.3-x86_64-darwin lib/prometheus/client/metric.rb
prometheus-client-mmap-0.20.3-arm64-darwin lib/prometheus/client/metric.rb
prometheus-client-mmap-0.20.3-aarch64-linux lib/prometheus/client/metric.rb
prometheus-client-mmap-0.20.3 lib/prometheus/client/metric.rb
prometheus-client-mmap-0.20.2 lib/prometheus/client/metric.rb
prometheus-client-mmap-0.20.1 lib/prometheus/client/metric.rb
prometheus-client-mmap-0.20.0 lib/prometheus/client/metric.rb
prometheus-client-mmap-0.19.1 lib/prometheus/client/metric.rb
prometheus-client-mmap-0.19.0 lib/prometheus/client/metric.rb
prometheus-client-mmap-0.18.0 lib/prometheus/client/metric.rb
prometheus-client-mmap-0.17.0 lib/prometheus/client/metric.rb
prometheus-client-mmap-0.16.2 lib/prometheus/client/metric.rb