Class: PrometheusExporter::Metric::Gauge

Inherits:
Base
  • Object
show all
Defined in:
lib/prometheus_exporter/metric/gauge.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#help, #name

Instance Method Summary collapse

Methods inherited from Base

default_aggregation, default_aggregation=, default_labels, default_labels=, default_prefix, default_prefix=, #from_json, #labels_text, #prefix, #to_prometheus_text

Constructor Details

#initialize(name, help) ⇒ Gauge

Returns a new instance of Gauge.



7
8
9
10
11
12
13
14
# File 'lib/prometheus_exporter/metric/gauge.rb', line 7

def initialize(name, help)
  if name.end_with?("_total")
    raise ArgumentError, "The metric name of gauge must not have _total suffix. Given: #{name}"
  end

  super
  reset!
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/prometheus_exporter/metric/gauge.rb', line 5

def data
  @data
end

Instance Method Details

#decrement(labels = {}, value = 1) ⇒ Object



56
57
58
59
# File 'lib/prometheus_exporter/metric/gauge.rb', line 56

def decrement(labels = {}, value = 1)
  @data[labels] ||= 0
  @data[labels] -= value
end

#increment(labels = {}, value = 1) ⇒ Object



51
52
53
54
# File 'lib/prometheus_exporter/metric/gauge.rb', line 51

def increment(labels = {}, value = 1)
  @data[labels] ||= 0
  @data[labels] += value
end

#metric_textObject



20
21
22
23
24
# File 'lib/prometheus_exporter/metric/gauge.rb', line 20

def metric_text
  @data.map do |labels, value|
    "#{prefix(@name)}#{labels_text(labels)} #{value}"
  end.join("\n")
end

#observe(value, labels = {}) ⇒ Object Also known as: set



38
39
40
41
42
43
44
45
46
47
# File 'lib/prometheus_exporter/metric/gauge.rb', line 38

def observe(value, labels = {})
  if value.nil?
    data.delete(labels)
  else
    if !(Numeric === value)
      raise ArgumentError, 'value must be a number'
    end
    @data[labels] = value
  end
end

#remove(labels) ⇒ Object



34
35
36
# File 'lib/prometheus_exporter/metric/gauge.rb', line 34

def remove(labels)
  @data.delete(labels)
end

#reset!Object



26
27
28
# File 'lib/prometheus_exporter/metric/gauge.rb', line 26

def reset!
  @data = {}
end

#to_hObject



30
31
32
# File 'lib/prometheus_exporter/metric/gauge.rb', line 30

def to_h
  @data.dup
end

#typeObject



16
17
18
# File 'lib/prometheus_exporter/metric/gauge.rb', line 16

def type
  "gauge"
end