Class: Philiprehberger::Metric::Summary
- Inherits:
-
Object
- Object
- Philiprehberger::Metric::Summary
- 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
-
#help ⇒ String
readonly
The help description.
-
#name ⇒ String
readonly
The metric name.
-
#quantiles ⇒ Array<Float>
readonly
The quantiles to compute.
Instance Method Summary collapse
-
#get(labels: {}) ⇒ Hash
Get a snapshot for a specific label set.
-
#initialize(name, help: '', quantiles: DEFAULT_QUANTILES) ⇒ Summary
constructor
A new instance of Summary.
-
#observe(value, labels: {}) ⇒ void
Observe a value.
-
#reset ⇒ void
Reset all observations.
-
#snapshot ⇒ Hash
Return a snapshot of all observations.
-
#type ⇒ String
The metric type name.
Constructor Details
#initialize(name, help: '', quantiles: DEFAULT_QUANTILES) ⇒ Summary
Returns a new instance of Summary.
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
#help ⇒ String (readonly)
Returns the help description.
14 15 16 |
# File 'lib/philiprehberger/metric/summary.rb', line 14 def help @help end |
#name ⇒ String (readonly)
Returns the metric name.
11 12 13 |
# File 'lib/philiprehberger/metric/summary.rb', line 11 def name @name end |
#quantiles ⇒ Array<Float> (readonly)
Returns 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.
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.
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 |
#reset ⇒ void
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 |
#snapshot ⇒ Hash
Return a snapshot of all observations.
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 |
#type ⇒ String
Returns the metric type name.
89 90 91 |
# File 'lib/philiprehberger/metric/summary.rb', line 89 def type 'summary' end |