Class: Prometheus::Client::Metric

Inherits:
Object
  • Object
show all
Includes:
UsesValueType
Defined in:
lib/prometheus/client/metric.rb

Direct Known Subclasses

Counter, Gauge, Histogram, Summary

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from UsesValueType

#value_class, #value_object

Constructor Details

#initialize(name, docstring, base_labels = {}) ⇒ Metric

Returns a new instance of Metric.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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

Instance Attribute Details

#base_labelsObject (readonly)

Returns the value of attribute base_labels.



9
10
11
# File 'lib/prometheus/client/metric.rb', line 9

def base_labels
  @base_labels
end

#docstringObject (readonly)

Returns the value of attribute docstring.



9
10
11
# File 'lib/prometheus/client/metric.rb', line 9

def docstring
  @docstring
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/prometheus/client/metric.rb', line 9

def name
  @name
end

Instance Method Details

#get(labels = {}) ⇒ Object

Returns the value for the given label set



33
34
35
36
37
38
# 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

#valuesObject

Returns all label sets with their values



41
42
43
44
45
46
47
# 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