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.



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



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/prometheus_exporter/metric/summary.rb', line 60

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



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

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



89
90
91
92
93
94
95
# File 'lib/prometheus_exporter/metric/summary.rb', line 89

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

#metric_textObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/prometheus_exporter/metric/summary.rb', line 72

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



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/prometheus_exporter/metric/summary.rb', line 108

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



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

def remove(labels)
  @counts.delete(labels)
  @sums.delete(labels)
  @buffers[0].delete(labels)
  @buffers[1].delete(labels)
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



97
98
99
100
101
102
103
104
105
106
# File 'lib/prometheus_exporter/metric/summary.rb', line 97

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

#to_hObject



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

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



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

def type
  "summary"
end