Class: Hyperion::Metrics::HistogramAccumulator

Inherits:
Object
  • Object
show all
Defined in:
lib/hyperion/metrics.rb

Overview

Per-(name, labels) histogram accumulator. Fixed-size Integer Array of bucket counters + scalar sum/count. Cumulative bucket semantics match Prometheus client convention: bucket counts observations whose value <= bucket_edges, and the implicit ‘+Inf` bucket is `count` itself. The exporter writes the Inf bucket as the total count plus a `le=“Inf”` line per Prometheus text format.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buckets) ⇒ HistogramAccumulator

Returns a new instance of HistogramAccumulator.



320
321
322
323
324
325
# File 'lib/hyperion/metrics.rb', line 320

def initialize(buckets)
  @buckets = buckets.freeze
  @counts  = Array.new(buckets.size, 0)
  @sum     = 0.0
  @count   = 0
end

Instance Attribute Details

#bucketsObject (readonly)

Returns the value of attribute buckets.



318
319
320
# File 'lib/hyperion/metrics.rb', line 318

def buckets
  @buckets
end

#countObject (readonly)

Returns the value of attribute count.



318
319
320
# File 'lib/hyperion/metrics.rb', line 318

def count
  @count
end

#countsObject (readonly)

Returns the value of attribute counts.



318
319
320
# File 'lib/hyperion/metrics.rb', line 318

def counts
  @counts
end

#sumObject (readonly)

Returns the value of attribute sum.



318
319
320
# File 'lib/hyperion/metrics.rb', line 318

def sum
  @sum
end

Instance Method Details

#observe(value) ⇒ Object

Walk the buckets linearly. For 7 buckets (the default request- duration set) this is faster than binary search; for any reasonable bucket count (< 30) the constant factor wins. Mutex- guarded by the caller (Metrics#observe_histogram).



331
332
333
334
335
336
337
338
339
340
341
# File 'lib/hyperion/metrics.rb', line 331

def observe(value)
  v = value.to_f
  @sum   += v
  @count += 1
  i = 0
  len = @buckets.length
  while i < len
    @counts[i] += 1 if v <= @buckets[i]
    i += 1
  end
end

#snapshotObject



343
344
345
346
# File 'lib/hyperion/metrics.rb', line 343

def snapshot
  # Return a new struct so callers don't see a live, mutating ref.
  { buckets: @buckets, counts: @counts.dup, sum: @sum, count: @count }
end