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
# 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
  )
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

#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



46
47
48
49
# File 'lib/prometheus/client/metric.rb', line 46

def get(labels: {})
  label_set = label_set_for(labels)
  @store.get(labels: label_set)
end

#init_label_set(labels) ⇒ Object



59
60
61
# File 'lib/prometheus/client/metric.rb', line 59

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

#valuesObject

Returns all label sets with their values



64
65
66
# File 'lib/prometheus/client/metric.rb', line 64

def values
  @store.all_values
end

#with_labels(labels) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/prometheus/client/metric.rb', line 51

def with_labels(labels)
  self.class.new(name,
                 docstring: docstring,
                 labels: @labels,
                 preset_labels: preset_labels.merge(labels),
                 store_settings: @store_settings)
end