Class: PrometheusExporter::Metric::Base

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

Direct Known Subclasses

Counter, Gauge, Histogram, Summary

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, help) ⇒ Base

Returns a new instance of Base.



24
25
26
27
# File 'lib/prometheus_exporter/metric/base.rb', line 24

def initialize(name, help)
  @name = name
  @help = help
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



22
23
24
# File 'lib/prometheus_exporter/metric/base.rb', line 22

def data
  @data
end

#helpObject

Returns the value of attribute help.



22
23
24
# File 'lib/prometheus_exporter/metric/base.rb', line 22

def help
  @help
end

#nameObject

Returns the value of attribute name.



22
23
24
# File 'lib/prometheus_exporter/metric/base.rb', line 22

def name
  @name
end

Class Method Details

.default_labelsObject



18
19
20
# File 'lib/prometheus_exporter/metric/base.rb', line 18

def self.default_labels
  @default_labels || {}
end

.default_labels=(labels) ⇒ Object



14
15
16
# File 'lib/prometheus_exporter/metric/base.rb', line 14

def self.default_labels=(labels)
  @default_labels = labels
end

.default_prefixObject



10
11
12
# File 'lib/prometheus_exporter/metric/base.rb', line 10

def self.default_prefix
  @default_prefix.to_s
end

.default_prefix=(name) ⇒ Object

prefix applied to all metrics



6
7
8
# File 'lib/prometheus_exporter/metric/base.rb', line 6

def self.default_prefix=(name)
  @default_prefix = name
end

Instance Method Details

#from_json(json) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/prometheus_exporter/metric/base.rb', line 41

def from_json(json)
  json = JSON.parse(json) if String === json
  @name = json["name"]
  @help = json["help"]
  @data = json["data"]
  if Hash === json["data"]
    @data = {}
    json["data"].each do |k, v|
      k = JSON.parse(k)
      k = Hash[k.map { |k1, v1| [k1.to_sym, v1] }]
      @data[k] = v
    end
  end
end

#labels_text(labels) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/prometheus_exporter/metric/base.rb', line 60

def labels_text(labels)
  labels = (labels || {}).merge(Base.default_labels)
  if labels && labels.length > 0
    s = labels.map do |key, value|
      value = value.to_s
      value = escape_value(value) if needs_escape?(value)
      "#{key}=\"#{value}\""
    end.join(",")
    "{#{s}}"
  end
end

#metric_textObject



33
34
35
# File 'lib/prometheus_exporter/metric/base.rb', line 33

def metric_text
  raise "Not implemented"
end

#prefix(name) ⇒ Object



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

def prefix(name)
  Base.default_prefix + name
end

#reset!Object



37
38
39
# File 'lib/prometheus_exporter/metric/base.rb', line 37

def reset!
  raise "Not implemented"
end

#to_prometheus_textObject



72
73
74
75
76
77
78
# File 'lib/prometheus_exporter/metric/base.rb', line 72

def to_prometheus_text
  <<~TEXT
    # HELP #{prefix(name)} #{help}
    # TYPE #{prefix(name)} #{type}
    #{metric_text}
  TEXT
end

#typeObject



29
30
31
# File 'lib/prometheus_exporter/metric/base.rb', line 29

def type
  raise "Not implemented"
end