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_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 = {}) ⇒ Summary

Returns a new instance of Summary.



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

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.



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

def count
  @count
end

#estimatorsObject (readonly)

Returns the value of attribute estimators.



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

def estimators
  @estimators
end

#totalObject (readonly)

Returns the value of attribute total.



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

def total
  @total
end

Instance Method Details

#calculate_all_quantilesObject



57
58
59
60
61
62
63
64
# File 'lib/prometheus_exporter/metric/summary.rb', line 57

def calculate_all_quantiles
  buffer = @buffers[@current_buffer]

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

  result
end

#calculate_quantiles(raw_data) ⇒ Object



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

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

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

  result
end

#ensure_summary(labels) ⇒ Object

makes sure we have storage



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

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

#metric_textObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/prometheus_exporter/metric/summary.rb', line 66

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



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/prometheus_exporter/metric/summary.rb', line 100

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

#remove(labels) ⇒ Object



34
35
36
37
38
39
# File 'lib/prometheus_exporter/metric/summary.rb', line 34

def remove(labels)
  @counts.delete(labels)
  @sums.delete(labels)
  @buffers[0].delete(labels)
  @buffers[1].delete(labels)
end

#reset!Object



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

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

#rotate_if_neededObject



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

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

#to_hObject



24
25
26
27
28
29
30
31
32
# File 'lib/prometheus_exporter/metric/summary.rb', line 24

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

#typeObject



41
42
43
# File 'lib/prometheus_exporter/metric/summary.rb', line 41

def type
  "summary"
end