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.



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

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

Instance Attribute Details

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

#helpObject

Returns the value of attribute help.



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

def help
  @help
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.default_aggregationObject



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

def self.default_aggregation
  @default_aggregation ||= Summary
end

.default_aggregation=(aggregation) ⇒ Object



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

def self.default_aggregation=(aggregation)
  @default_aggregation = aggregation
end

.default_labelsObject



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

def self.default_labels
  @default_labels || {}
end

.default_labels=(labels) ⇒ Object



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

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

.default_prefixObject



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

def self.default_prefix
  @default_prefix.to_s
end

.default_prefix=(name) ⇒ Object

prefix applied to all metrics



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

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

Instance Method Details

#from_json(json) ⇒ Object



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

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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/prometheus_exporter/metric/base.rb', line 76

def labels_text(labels)
  labels = Base.default_labels.merge(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



45
46
47
# File 'lib/prometheus_exporter/metric/base.rb', line 45

def metric_text
  raise "Not implemented"
end

#prefix(name) ⇒ Object



72
73
74
# File 'lib/prometheus_exporter/metric/base.rb', line 72

def prefix(name)
  Base.default_prefix + name
end

#reset!Object



49
50
51
# File 'lib/prometheus_exporter/metric/base.rb', line 49

def reset!
  raise "Not implemented"
end

#to_hObject



53
54
55
# File 'lib/prometheus_exporter/metric/base.rb', line 53

def to_h
  raise "Not implemented"
end

#to_prometheus_textObject



91
92
93
94
95
96
97
# File 'lib/prometheus_exporter/metric/base.rb', line 91

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

#typeObject



41
42
43
# File 'lib/prometheus_exporter/metric/base.rb', line 41

def type
  raise "Not implemented"
end