lib/prometheus/client/summary.rb in prometheus-client-0.9.0 vs lib/prometheus/client/summary.rb in prometheus-client-0.10.0.pre.alpha.1

- old
+ new

@@ -1,64 +1,55 @@ # encoding: UTF-8 -require 'quantile' require 'prometheus/client/metric' module Prometheus module Client # Summary is an accumulator for samples. It captures Numeric data and - # provides an efficient quantile calculation mechanism. + # provides the total count and sum of observations. class Summary < Metric - extend Gem::Deprecate - - # Value represents the state of a Summary at a given point. - class Value < Hash - attr_accessor :sum, :total - - def initialize(estimator) - @sum = estimator.sum - @total = estimator.observations - - estimator.invariants.each do |invariant| - self[invariant.quantile] = estimator.query(invariant.quantile) - end - end - end - def type :summary end # Records a given value. - def observe(labels, value) - label_set = label_set_for(labels) - synchronize { @values[label_set].observe(value) } + 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 - alias add observe - deprecate :add, :observe, 2016, 10 - # Returns the value for the given label set - def get(labels = {}) - @validator.valid?(labels) + # Returns a hash with "sum" and "count" as keys + def get(labels: {}) + base_label_set = label_set_for(labels) - synchronize do - Value.new(@values[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 - # Returns all label sets with their values + # Returns all label sets with their values expressed as hashes with their sum/count def values - synchronize do - @values.each_with_object({}) do |(labels, value), memo| - memo[labels] = Value.new(value) - end + v = @store.all_values + + v.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 private - def default - Quantile::Estimator.new + def reserved_labels + [:quantile] end end end end