class Prometheus::Client::Metric

Attributes

base_labels[R]
docstring[R]
name[R]

Public Class Methods

new(name, docstring, base_labels = {}) click to toggle source
# File lib/prometheus/client/metric.rb, line 11
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

Public Instance Methods

get(labels = {}) click to toggle source

Returns the value for the given label set

# File lib/prometheus/client/metric.rb, line 33
def get(labels = {})
  label_set = label_set_for(labels)
  @validator.valid?(label_set)

  @values[label_set].get
end
values() click to toggle source

Returns all label sets with their values

# File lib/prometheus/client/metric.rb, line 41
def values
  synchronize do
    @values.each_with_object({}) do |(labels, value), memo|
      memo[labels] = value
    end
  end
end

Private Instance Methods

default(labels) click to toggle source
# File lib/prometheus/client/metric.rb, line 55
def default(labels)
  value_object(type, @name, @name, labels)
end
label_set_for(labels) click to toggle source
# File lib/prometheus/client/metric.rb, line 71
def label_set_for(labels)
  @validator.validate(@base_labels.merge(labels))
end
synchronize(&block) click to toggle source
# File lib/prometheus/client/metric.rb, line 75
def synchronize(&block)
  @mutex.synchronize(&block)
end
touch_default_value() click to toggle source
# File lib/prometheus/client/metric.rb, line 51
def touch_default_value
  @values[label_set_for({})]
end
validate_docstring(docstring) click to toggle source
# File lib/prometheus/client/metric.rb, line 65
def validate_docstring(docstring)
  return true if docstring.respond_to?(:empty?) && !docstring.empty?

  raise ArgumentError, 'docstring must be given'
end
validate_name(name) click to toggle source
# File lib/prometheus/client/metric.rb, line 59
def validate_name(name)
  return true if name.is_a?(Symbol)

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