class Prometheus::Client::Metric
Attributes
docstring[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) @store = Prometheus::Client.config.data_store.for_metric( name, metric_type: type, metric_settings: store_settings ) if preset_labels.keys.length == labels.length @validator.validate_labelset!(preset_labels) @all_labels_preset = true end 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 45 def get(labels: {}) label_set = label_set_for(labels) @store.get(labels: label_set) end
values()
click to toggle source
Returns all label sets with their values
# File lib/prometheus/client/metric.rb, line 59 def values @store.all_values end
with_labels(labels)
click to toggle source
# File lib/prometheus/client/metric.rb, line 50 def with_labels(labels) self.class.new(name, docstring: docstring, labels: @labels, preset_labels: preset_labels.merge(labels), store_settings: @store_settings) end
Private Instance Methods
label_set_for(labels)
click to toggle source
# File lib/prometheus/client/metric.rb, line 85 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 65 def reserved_labels [] end
stringify_values(labels)
click to toggle source
# File lib/prometheus/client/metric.rb, line 92 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 79 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 69 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