class Prometheus::Client::Metric
Attributes
Public Class Methods
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
Source
# File lib/prometheus/client/metric.rb, line 55 def get(labels: {}) label_set = label_set_for(labels) @store.get(labels: label_set) end
Returns the value for the given label set
Source
# File lib/prometheus/client/metric.rb, line 74 def init_label_set(labels) @store.set(labels: label_set_for(labels), val: 0) end
Source
# File lib/prometheus/client/metric.rb, line 79 def values @store.all_values end
Returns all label sets with their values
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
Source
# File lib/prometheus/client/metric.rb, line 49 def replace_internal_store(new_store) @store = new_store end
Private Instance Methods
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
Source
# File lib/prometheus/client/metric.rb, line 85 def reserved_labels [] end
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
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
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