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.



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

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

Instance Attribute Details

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

#helpObject

Returns the value of attribute help.



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

def help
  @help
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.default_aggregationObject



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

def self.default_aggregation
  @default_aggregation ||= Summary
end

.default_aggregation=(aggregation) ⇒ Object



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

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

.default_labelsObject



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

def self.default_labels
  @default_labels || {}
end

.default_labels=(labels) ⇒ Object



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

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

.default_prefixObject



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

def self.default_prefix
  @default_prefix.to_s
end

.default_prefix=(name) ⇒ Object

prefix applied to all metrics



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

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

Instance Method Details

#from_json(json) ⇒ Object



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

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



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

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



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

def metric_text
  raise "Not implemented"
end

#prefix(name) ⇒ Object



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

def prefix(name)
  Base.default_prefix + name
end

#reset!Object



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

def reset!
  raise "Not implemented"
end

#to_hObject



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

def to_h
  raise "Not implemented"
end

#to_prometheus_textObject



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

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

#typeObject



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

def type
  raise "Not implemented"
end