class Prometheus::Client::Metric

Metric

Attributes

docstring[R]
labels[R]
name[R]
preset_labels[R]

Public Class Methods

new(name, docstring:, labels: [], preset_labels: {}, store_settings: {}) click to toggle source
# File lib/prometheus/client/metric.rb, line 12
def initialize(name,
               docstring:,
               labels: [],
               preset_labels: {},
               store_settings: {})

  validate_name(name)
  validate_docstring(docstring)
  @validator = LabelSetValidator.new(expected_labels: labels,
                                     reserved_labels: reserved_labels)
  @validator.validate_symbols!(labels)
  @validator.validate_symbols!(preset_labels)

  @labels = labels
  @store_settings = store_settings

  @name = name
  @docstring = docstring
  @preset_labels = stringify_values(preset_labels)

  @all_labels_preset = false
  if preset_labels.keys.length == labels.length
    @validator.validate_labelset!(preset_labels)
    @all_labels_preset = true
  end

  @store = Prometheus::Client.config.data_store.for_metric(
    name,
    metric_type: type,
    metric_settings: store_settings
  )

  # WARNING: Our internal store can be replaced later by `with_labels`
  # Everything we do after this point needs to still work if @store gets replaced
  init_label_set({}) if labels.empty?
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 55
def get(labels: {})
  label_set = label_set_for(labels)
  @store.get(labels: label_set)
end
init_label_set(labels) click to toggle source
# File lib/prometheus/client/metric.rb, line 74
def init_label_set(labels)
  @store.set(labels: label_set_for(labels), val: 0)
end
values() click to toggle source

Returns all label sets with their values

# File lib/prometheus/client/metric.rb, line 79
def values
  @store.all_values
end
with_labels(labels) click to toggle source
# File lib/prometheus/client/metric.rb, line 60
def with_labels(labels)
  new_metric = self.class.new(name,
                               docstring: docstring,
                               labels: @labels,
                               preset_labels: preset_labels.merge(labels),
                               store_settings: @store_settings)

  # The new metric needs to use the same store as the "main" declared one, otherwise
  # any observations on that copy with the pre-set labels won't actually be exported.
  new_metric.replace_internal_store(@store)

  new_metric
end

Protected Instance Methods

replace_internal_store(new_store) click to toggle source
# File lib/prometheus/client/metric.rb, line 49
          def replace_internal_store(new_store)
  @store = new_store
end

Private Instance Methods

label_set_for(labels) click to toggle source
# File lib/prometheus/client/metric.rb, line 105
def label_set_for(labels)
  # We've already validated, and there's nothing to merge. Save some cycles
  return preset_labels if @all_labels_preset && labels.empty?
  labels = stringify_values(labels)
  @validator.validate_labelset!(preset_labels.merge(labels))
end
reserved_labels() click to toggle source
# File lib/prometheus/client/metric.rb, line 85
def reserved_labels
  []
end
stringify_values(labels) click to toggle source
# File lib/prometheus/client/metric.rb, line 112
def stringify_values(labels)
  stringified = {}
  labels.each { |k,v| stringified[k] = v.to_s }

  stringified
end
validate_docstring(docstring) click to toggle source
# File lib/prometheus/client/metric.rb, line 99
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 89
def validate_name(name)
  unless name.is_a?(Symbol)
    raise ArgumentError, 'metric name must be a symbol'
  end
  unless name.to_s =~ /\A[a-zA-Z_:][a-zA-Z0-9_:]*\Z/
    msg = 'metric name must match /[a-zA-Z_:][a-zA-Z0-9_:]*/'
    raise ArgumentError, msg
  end
end