Sha256: 83a51d34959494413c004826c882c14f8488a75ff5e774ce5148891345dd8ba1

Contents?: true

Size: 1.47 KB

Versions: 6

Compression:

Stored size: 1.47 KB

Contents

# 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.
    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) }
      end
      alias add observe
      deprecate :add, :observe, 2016, 10

      # Returns the value for the given label set
      def get(labels = {})
        @validator.valid?(labels)

        synchronize do
          Value.new(@values[labels])
        end
      end

      # Returns all label sets with their values
      def values
        synchronize do
          @values.each_with_object({}) do |(labels, value), memo|
            memo[labels] = Value.new(value)
          end
        end
      end

      private

      def default
        Quantile::Estimator.new
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
prometheus-client-0.9.0 lib/prometheus/client/summary.rb
prometheus-client-0.8.0 lib/prometheus/client/summary.rb
prometheus-client-0.7.1 lib/prometheus/client/summary.rb
prometheus-client-0.7.0 lib/prometheus/client/summary.rb
prometheus-client-0.7.0.pre.rc.1 lib/prometheus/client/summary.rb
prometheus-client-0.6.0 lib/prometheus/client/summary.rb