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 = LabelSetValidator.new @values = Hash.new { |hash, key| hash[key] = default } 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 26 def get(labels = {}) @validator.valid?(labels) @values[labels] end
values()
click to toggle source
Returns all label sets with their values
# File lib/prometheus/client/metric.rb, line 33 def values synchronize do @values.each_with_object({}) do |(labels, value), memo| memo[labels] = value end end end
Private Instance Methods
default()
click to toggle source
# File lib/prometheus/client/metric.rb, line 43 def default nil end
label_set_for(labels)
click to toggle source
# File lib/prometheus/client/metric.rb, line 63 def label_set_for(labels) @validator.validate(labels) end
synchronize() { || ... }
click to toggle source
# File lib/prometheus/client/metric.rb, line 67 def synchronize @mutex.synchronize { yield } end
validate_docstring(docstring)
click to toggle source
# File lib/prometheus/client/metric.rb, line 57 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 47 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