Class: Prometheus::Client::Summary

Inherits:
Metric
  • Object
show all
Defined in:
lib/prometheus/client/summary.rb

Overview

Summary is an accumulator for samples. It captures Numeric data and provides the total count and sum of observations.

Instance Attribute Summary

Attributes inherited from Metric

#docstring, #labels, #name, #preset_labels

Instance Method Summary collapse

Methods inherited from Metric

#initialize, #with_labels

Constructor Details

This class inherits a constructor from Prometheus::Client::Metric

Instance Method Details

#get(labels: {}) ⇒ Object

Returns a hash with “sum” and “count” as keys



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/prometheus/client/summary.rb', line 30

def get(labels: {})
  base_label_set = label_set_for(labels)

  internal_counters = ["count", "sum"]

  @store.synchronize do
    internal_counters.each_with_object({}) do |counter, acc|
      acc[counter] = @store.get(labels: base_label_set.merge(quantile: counter))
    end
  end
end

#init_label_set(labels) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/prometheus/client/summary.rb', line 53

def init_label_set(labels)
  base_label_set = label_set_for(labels)

  @store.synchronize do
    @store.set(labels: base_label_set.merge(quantile: "count"), val: 0)
    @store.set(labels: base_label_set.merge(quantile: "sum"), val: 0)
  end
end

#observe(value, labels: {}) ⇒ Object

Records a given value. The recorded value is usually positive or zero. A negative value is accepted but prevents current versions of Prometheus from properly detecting counter resets in the sum of observations. See prometheus.io/docs/practices/histograms/#count-and-sum-of-observations for details.



20
21
22
23
24
25
26
27
# File 'lib/prometheus/client/summary.rb', line 20

def observe(value, labels: {})
  base_label_set = label_set_for(labels)

  @store.synchronize do
    @store.increment(labels: base_label_set.merge(quantile: "count"), by: 1)
    @store.increment(labels: base_label_set.merge(quantile: "sum"), by: value)
  end
end

#typeObject



10
11
12
# File 'lib/prometheus/client/summary.rb', line 10

def type
  :summary
end

#valuesObject

Returns all label sets with their values expressed as hashes with their sum/count



43
44
45
46
47
48
49
50
51
# File 'lib/prometheus/client/summary.rb', line 43

def values
  values = @store.all_values

  values.each_with_object({}) do |(label_set, v), acc|
    actual_label_set = label_set.reject{|l| l == :quantile }
    acc[actual_label_set] ||= { "count" => 0.0, "sum" => 0.0 }
    acc[actual_label_set][label_set[:quantile]] = v
  end
end