Class: PrometheusExporter::Metric::Histogram
- Inherits:
-
Base
- Object
- Base
- PrometheusExporter::Metric::Histogram
show all
- Defined in:
- lib/prometheus_exporter/metric/histogram.rb
Constant Summary
collapse
- DEFAULT_BUCKETS =
[0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5.0, 10.0].freeze
Instance Attribute Summary collapse
Attributes inherited from Base
#data, #help, #name
Class Method Summary
collapse
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, opts = {}) ⇒ Histogram
Returns a new instance of Histogram.
19
20
21
22
23
|
# File 'lib/prometheus_exporter/metric/histogram.rb', line 19
def initialize(name, help, opts = {})
super(name, help)
@buckets = (opts[:buckets] || self.class.default_buckets).sort
reset!
end
|
Instance Attribute Details
#buckets ⇒ Object
Returns the value of attribute buckets.
17
18
19
|
# File 'lib/prometheus_exporter/metric/histogram.rb', line 17
def buckets
@buckets
end
|
Class Method Details
.default_buckets ⇒ Object
9
10
11
|
# File 'lib/prometheus_exporter/metric/histogram.rb', line 9
def self.default_buckets
@default_buckets || DEFAULT_BUCKETS
end
|
.default_buckets=(buckets) ⇒ Object
13
14
15
|
# File 'lib/prometheus_exporter/metric/histogram.rb', line 13
def self.default_buckets=(buckets)
@default_buckets = buckets
end
|
Instance Method Details
#ensure_histogram(labels) ⇒ Object
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/prometheus_exporter/metric/histogram.rb', line 81
def ensure_histogram(labels)
@sums[labels] ||= 0.0
@counts[labels] ||= 0
buckets = @observations[labels]
if buckets.nil?
buckets = @buckets.map { |b| [b, 0] }.to_h
@observations[labels] = buckets
end
buckets
end
|
#fill_buckets(value, buckets) ⇒ Object
92
93
94
95
96
97
|
# File 'lib/prometheus_exporter/metric/histogram.rb', line 92
def fill_buckets(value, buckets)
@buckets.reverse_each do |b|
break if value > b
buckets[b] += 1
end
end
|
#metric_text ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/prometheus_exporter/metric/histogram.rb', line 51
def metric_text
text = +""
first = true
@observations.each do |labels, buckets|
text << "\n" unless first
first = false
count = @counts[labels]
sum = @sums[labels]
@buckets.each do |bucket|
value = @observations[labels][bucket]
text << "#{prefix(@name)}_bucket#{labels_text(with_bucket(labels, bucket.to_s))} #{value}\n"
end
text << "#{prefix(@name)}_bucket#{labels_text(with_bucket(labels, "+Inf"))} #{count}\n"
text << "#{prefix(@name)}_count#{labels_text(labels)} #{count}\n"
text << "#{prefix(@name)}_sum#{labels_text(labels)} #{sum}"
end
text
end
|
#observe(value, labels = nil) ⇒ Object
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/prometheus_exporter/metric/histogram.rb', line 70
def observe(value, labels = nil)
labels ||= {}
buckets = ensure_histogram(labels)
value = value.to_f
@sums[labels] += value
@counts[labels] += 1
fill_buckets(value, buckets)
end
|
#remove(labels) ⇒ Object
41
42
43
44
45
|
# File 'lib/prometheus_exporter/metric/histogram.rb', line 41
def remove(labels)
@observations.delete(labels)
@counts.delete(labels)
@sums.delete(labels)
end
|
#reset! ⇒ Object
25
26
27
28
29
|
# File 'lib/prometheus_exporter/metric/histogram.rb', line 25
def reset!
@sums = {}
@counts = {}
@observations = {}
end
|
#to_h ⇒ Object
31
32
33
34
35
36
37
38
39
|
# File 'lib/prometheus_exporter/metric/histogram.rb', line 31
def to_h
data = {}
@observations.each do |labels, buckets|
count = @counts[labels]
sum = @sums[labels]
data[labels] = { "count" => count, "sum" => sum }
end
data
end
|
#type ⇒ Object
47
48
49
|
# File 'lib/prometheus_exporter/metric/histogram.rb', line 47
def type
"histogram"
end
|
#with_bucket(labels, bucket) ⇒ Object
99
100
101
|
# File 'lib/prometheus_exporter/metric/histogram.rb', line 99
def with_bucket(labels, bucket)
labels.merge("le" => bucket)
end
|