Class: Prometheus::Client::Metric

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

Overview

Metric

Direct Known Subclasses

Counter, Gauge, Histogram, Summary

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, docstring:, labels: [], preset_labels: {}, store_settings: {}) ⇒ Metric

Returns a new instance of Metric.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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

Instance Attribute Details

#docstringObject (readonly)

Returns the value of attribute docstring.



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

def docstring
  @docstring
end

#labelsObject (readonly)

Returns the value of attribute labels.



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

def labels
  @labels
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#preset_labelsObject (readonly)

Returns the value of attribute preset_labels.



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

def preset_labels
  @preset_labels
end

Instance Method Details

#get(labels: {}) ⇒ Object

Returns the value for the given label set



55
56
57
58
# 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) ⇒ Object



74
75
76
# File 'lib/prometheus/client/metric.rb', line 74

def init_label_set(labels)
  @store.set(labels: label_set_for(labels), val: 0)
end

#valuesObject

Returns all label sets with their values



79
80
81
# File 'lib/prometheus/client/metric.rb', line 79

def values
  @store.all_values
end

#with_labels(labels) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# 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