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
Attributes inherited from Base
#data, #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, opts = {}) ⇒ Histogram
Returns a new instance of Histogram.
8
9
10
11
12
|
# File 'lib/prometheus_exporter/metric/histogram.rb', line 8
def initialize(name, help, opts = {})
super(name, help)
@buckets = (opts[:buckets] || DEFAULT_BUCKETS).sort.reverse
reset!
end
|
Instance Method Details
#ensure_histogram(labels) ⇒ Object
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/prometheus_exporter/metric/histogram.rb', line 70
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
81
82
83
84
85
86
|
# File 'lib/prometheus_exporter/metric/histogram.rb', line 81
def fill_buckets(value, buckets)
@buckets.each do |b|
break if value > b
buckets[b] += 1
end
end
|
#metric_text ⇒ Object
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/prometheus_exporter/metric/histogram.rb', line 40
def metric_text
text = +""
first = true
@observations.each do |labels, buckets|
text << "\n" unless first
first = false
count = @counts[labels]
sum = @sums[labels]
text << "#{prefix(@name)}_bucket#{labels_text(with_bucket(labels, "+Inf"))} #{count}\n"
@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)}_count#{labels_text(labels)} #{count}\n"
text << "#{prefix(@name)}_sum#{labels_text(labels)} #{sum}"
end
text
end
|
#observe(value, labels = nil) ⇒ Object
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/prometheus_exporter/metric/histogram.rb', line 59
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
30
31
32
33
34
|
# File 'lib/prometheus_exporter/metric/histogram.rb', line 30
def remove(labels)
@observations.delete(labels)
@counts.delete(labels)
@sums.delete(labels)
end
|
#reset! ⇒ Object
14
15
16
17
18
|
# File 'lib/prometheus_exporter/metric/histogram.rb', line 14
def reset!
@sums = {}
@counts = {}
@observations = {}
end
|
#to_h ⇒ Object
20
21
22
23
24
25
26
27
28
|
# File 'lib/prometheus_exporter/metric/histogram.rb', line 20
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
36
37
38
|
# File 'lib/prometheus_exporter/metric/histogram.rb', line 36
def type
"histogram"
end
|
#with_bucket(labels, bucket) ⇒ Object
88
89
90
|
# File 'lib/prometheus_exporter/metric/histogram.rb', line 88
def with_bucket(labels, bucket)
labels.merge("le" => bucket)
end
|