Class: PrometheusExporter::Metric::Summary

Inherits:
Base
  • Object
show all
Defined in:
lib/prometheus_exporter/metric/summary.rb

Constant Summary collapse

DEFAULT_QUANTILES =
[0.99, 0.9, 0.5, 0.1, 0.01]
ROTATE_AGE =
120

Instance Attribute Summary collapse

Attributes inherited from Base

#data, #help, #name

Instance Method Summary collapse

Methods inherited from Base

default_labels, default_labels=, default_prefix, default_prefix=, #from_json, #labels_text, #prefix, #to_prometheus_text

Constructor Details

#initialize(name, help, opts = {}) ⇒ Summary

Returns a new instance of Summary.



11
12
13
14
15
# File 'lib/prometheus_exporter/metric/summary.rb', line 11

def initialize(name, help, opts = {})
  super(name, help)
  reset!
  @quantiles = opts[:quantiles] || DEFAULT_QUANTILES
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



9
10
11
# File 'lib/prometheus_exporter/metric/summary.rb', line 9

def count
  @count
end

#estimatorsObject (readonly)

Returns the value of attribute estimators.



9
10
11
# File 'lib/prometheus_exporter/metric/summary.rb', line 9

def estimators
  @estimators
end

#totalObject (readonly)

Returns the value of attribute total.



9
10
11
# File 'lib/prometheus_exporter/metric/summary.rb', line 9

def total
  @total
end

Instance Method Details

#calculate_all_quantilesObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/prometheus_exporter/metric/summary.rb', line 43

def calculate_all_quantiles
  buffer = @buffers[@current_buffer]

  result = {}
  buffer.each do |labels, raw_data|
    result[labels] = calculate_quantiles(raw_data)
  end

  result

end

#calculate_quantiles(raw_data) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/prometheus_exporter/metric/summary.rb', line 29

def calculate_quantiles(raw_data)
  sorted = raw_data.sort
  length = sorted.length
  result = {}

  if length > 0
    @quantiles.each do |quantile|
      result[quantile] = sorted[(length * quantile).ceil - 1]
    end
  end

  result
end

#ensure_summary(labels) ⇒ Object

makes sure we have storage



72
73
74
75
76
77
78
# File 'lib/prometheus_exporter/metric/summary.rb', line 72

def ensure_summary(labels)
  @buffers[0][labels] ||=  []
  @buffers[1][labels] ||=  []
  @sums[labels] ||= 0.0
  @counts[labels] ||= 0
  nil
end

#metric_textObject



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

def metric_text
  text = +""
  first = true
  calculate_all_quantiles.each do |labels, quantiles|
    text << "\n" unless first
    first = false
    quantiles.each do |quantile, value|
      with_quantile = labels.merge(quantile: quantile)
      text << "#{prefix(@name)}#{labels_text(with_quantile)} #{value.to_f}\n"
    end
    text << "#{prefix(@name)}_sum#{labels_text(labels)} #{@sums[labels]}\n"
    text << "#{prefix(@name)}_count#{labels_text(labels)} #{@counts[labels]}"
  end
  text
end

#observe(value, labels = nil) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/prometheus_exporter/metric/summary.rb', line 91

def observe(value, labels = nil)
  labels ||= {}
  ensure_summary(labels)
  rotate_if_needed

  value = value.to_f
  @buffers[0][labels] << value
  @buffers[1][labels] << value
  @sums[labels] += value
  @counts[labels] += 1
end

#reset!Object



17
18
19
20
21
22
23
# File 'lib/prometheus_exporter/metric/summary.rb', line 17

def reset!
  @buffers = [{}, {}]
  @last_rotated = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  @current_buffer = 0
  @counts = {}
  @sums = {}
end

#rotate_if_neededObject



80
81
82
83
84
85
86
87
88
89
# File 'lib/prometheus_exporter/metric/summary.rb', line 80

def rotate_if_needed
  if (now = Process.clock_gettime(Process::CLOCK_MONOTONIC)) > (@last_rotated + ROTATE_AGE)
    @last_rotated = now
    @buffers[@current_buffer].each do |labels, raw|
      raw.clear
    end
    @current_buffer = @current_buffer == 0 ? 1 : 0
  end
  nil
end

#typeObject



25
26
27
# File 'lib/prometheus_exporter/metric/summary.rb', line 25

def type
  "summary"
end