Class: Philiprehberger::Metric::Summary

Inherits:
Object
  • Object
show all
Defined in:
lib/philiprehberger/metric/summary.rb

Overview

A summary metric that tracks value distributions and computes quantile estimates.

Constant Summary collapse

DEFAULT_QUANTILES =

Default quantiles for summary metrics.

[0.5, 0.9, 0.99].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, help: '', quantiles: DEFAULT_QUANTILES) ⇒ Summary

Returns a new instance of Summary.

Parameters:

  • name (String)

    the metric name

  • help (String) (defaults to: '')

    the help description

  • quantiles (Array<Float>) (defaults to: DEFAULT_QUANTILES)

    quantiles to compute (default: [0.5, 0.9, 0.99])



22
23
24
25
26
27
28
# File 'lib/philiprehberger/metric/summary.rb', line 22

def initialize(name, help: '', quantiles: DEFAULT_QUANTILES)
  @name = name
  @help = help
  @quantiles = quantiles.sort.freeze
  @mutex = Mutex.new
  @observations = {}
end

Instance Attribute Details

#helpString (readonly)

Returns the help description.

Returns:

  • (String)

    the help description



14
15
16
# File 'lib/philiprehberger/metric/summary.rb', line 14

def help
  @help
end

#nameString (readonly)

Returns the metric name.

Returns:

  • (String)

    the metric name



11
12
13
# File 'lib/philiprehberger/metric/summary.rb', line 11

def name
  @name
end

#quantilesArray<Float> (readonly)

Returns the quantiles to compute.

Returns:

  • (Array<Float>)

    the quantiles to compute



17
18
19
# File 'lib/philiprehberger/metric/summary.rb', line 17

def quantiles
  @quantiles
end

Instance Method Details

#get(labels: {}) ⇒ Hash

Get a snapshot for a specific label set.

Parameters:

  • labels (Hash) (defaults to: {})

    the label set

Returns:

  • (Hash)

    with :count, :sum, and each quantile value



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/philiprehberger/metric/summary.rb', line 50

def get(labels: {})
  key = labels.sort.to_h
  @mutex.synchronize do
    entry = @observations[key]
    return build_empty_result unless entry

    sorted = entry[:values].sort
    result = { count: entry[:count], sum: entry[:sum] }
    @quantiles.each do |q|
      result[q] = compute_quantile(sorted, q)
    end
    result
  end
end

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

This method returns an undefined value.

Observe a value.

Parameters:

  • value (Numeric)

    the observed value

  • labels (Hash) (defaults to: {})

    optional labels



35
36
37
38
39
40
41
42
43
44
# File 'lib/philiprehberger/metric/summary.rb', line 35

def observe(value, labels: {})
  key = labels.sort.to_h
  @mutex.synchronize do
    @observations[key] ||= { values: [], sum: 0.0, count: 0 }
    entry = @observations[key]
    entry[:values] << value
    entry[:sum] += value
    entry[:count] += 1
  end
end

#resetvoid

This method returns an undefined value.

Reset all observations.



84
85
86
# File 'lib/philiprehberger/metric/summary.rb', line 84

def reset
  @mutex.synchronize { @observations.clear }
end

#snapshotHash

Return a snapshot of all observations.

Returns:

  • (Hash)

    labels => observation data



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/philiprehberger/metric/summary.rb', line 68

def snapshot
  @mutex.synchronize do
    @observations.transform_values do |entry|
      sorted = entry[:values].sort
      result = { count: entry[:count], sum: entry[:sum] }
      @quantiles.each do |q|
        result[q] = compute_quantile(sorted, q)
      end
      result
    end
  end
end

#typeString

Returns the metric type name.

Returns:

  • (String)

    the metric type name



89
90
91
# File 'lib/philiprehberger/metric/summary.rb', line 89

def type
  'summary'
end