Class: PrometheusExporter::Metric::Histogram

Inherits:
Base
  • Object
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.



20
21
22
23
24
# File 'lib/prometheus_exporter/metric/histogram.rb', line 20

def initialize(name, help, opts = {})
  super(name, help)
  @buckets = (opts[:buckets] || self.class.default_buckets).sort
  reset!
end

Instance Attribute Details

#bucketsObject (readonly)

Returns the value of attribute buckets.



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

def buckets
  @buckets
end

Class Method Details

.default_bucketsObject



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

def self.default_buckets
  @default_buckets || DEFAULT_BUCKETS
end

.default_buckets=(buckets) ⇒ Object



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

def self.default_buckets=(buckets)
  @default_buckets = buckets
end

Instance Method Details

#ensure_histogram(labels) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/prometheus_exporter/metric/histogram.rb', line 82

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



93
94
95
96
97
98
# File 'lib/prometheus_exporter/metric/histogram.rb', line 93

def fill_buckets(value, buckets)
  @buckets.reverse.each do |b|
    break if value > b
    buckets[b] += 1
  end
end

#metric_textObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/prometheus_exporter/metric/histogram.rb', line 52

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



71
72
73
74
75
76
77
78
79
80
# File 'lib/prometheus_exporter/metric/histogram.rb', line 71

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



42
43
44
45
46
# File 'lib/prometheus_exporter/metric/histogram.rb', line 42

def remove(labels)
  @observations.delete(labels)
  @counts.delete(labels)
  @sums.delete(labels)
end

#reset!Object



26
27
28
29
30
# File 'lib/prometheus_exporter/metric/histogram.rb', line 26

def reset!
  @sums = {}
  @counts = {}
  @observations = {}
end

#to_hObject



32
33
34
35
36
37
38
39
40
# File 'lib/prometheus_exporter/metric/histogram.rb', line 32

def to_h
  data = {}
  @observations.each do |labels, buckets|
    count = @counts[labels]
    sum = @sums[labels]
    data[labels] = { "count" => count, "sum" => sum }
  end
  data
end

#typeObject



48
49
50
# File 'lib/prometheus_exporter/metric/histogram.rb', line 48

def type
  "histogram"
end

#with_bucket(labels, bucket) ⇒ Object



100
101
102
# File 'lib/prometheus_exporter/metric/histogram.rb', line 100

def with_bucket(labels, bucket)
  labels.merge("le" => bucket)
end