class PrometheusExporter::Metric::Histogram
Constants
- DEFAULT_BUCKETS
Public Class Methods
new(name, help, opts = {})
click to toggle source
Calls superclass method
PrometheusExporter::Metric::Base::new
# File lib/prometheus_exporter/metric/histogram.rb, line 8 def initialize(name, help, opts = {}) super(name, help) @buckets = (opts[:buckets] || DEFAULT_BUCKETS).sort.reverse reset! end
Public Instance Methods
ensure_histogram(labels)
click to toggle source
# File lib/prometheus_exporter/metric/histogram.rb, line 70 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)
click to toggle source
# File lib/prometheus_exporter/metric/histogram.rb, line 81 def fill_buckets(value, buckets) @buckets.each do |b| break if value > b buckets[b] += 1 end end
metric_text()
click to toggle source
# File lib/prometheus_exporter/metric/histogram.rb, line 40 def metric_text text = +"" first = true @observations.each do |labels, buckets| text << "\n" unless first first = false count = @counts[labels] sum = @sums[labels] text << "#{prefix(@name)}_bucket#{labels_text(with_bucket(labels, "+Inf"))} #{count}\n" @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)}_count#{labels_text(labels)} #{count}\n" text << "#{prefix(@name)}_sum#{labels_text(labels)} #{sum}" end text end
observe(value, labels = nil)
click to toggle source
# File lib/prometheus_exporter/metric/histogram.rb, line 59 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)
click to toggle source
# File lib/prometheus_exporter/metric/histogram.rb, line 30 def remove(labels) @observations.delete(labels) @counts.delete(labels) @sums.delete(labels) end
reset!()
click to toggle source
# File lib/prometheus_exporter/metric/histogram.rb, line 14 def reset! @sums = {} @counts = {} @observations = {} end
to_h()
click to toggle source
# File lib/prometheus_exporter/metric/histogram.rb, line 20 def to_h data = {} @observations.each do |labels, buckets| count = @counts[labels] sum = @sums[labels] data[labels] = { "count" => count, "sum" => sum } end data end
type()
click to toggle source
# File lib/prometheus_exporter/metric/histogram.rb, line 36 def type "histogram" end
with_bucket(labels, bucket)
click to toggle source
# File lib/prometheus_exporter/metric/histogram.rb, line 88 def with_bucket(labels, bucket) labels.merge("le" => bucket) end