Class: Rubino::Metrics::Histogram

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

Overview

Bucketed distribution keyed by label set. Buckets are CUMULATIVE per Prometheus convention: each observation increments every bucket whose ‘le >= value`, plus the implicit `+Inf` bucket. Thread-safe.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, help, buckets: DEFAULT_BUCKETS) ⇒ Histogram

Returns a new instance of Histogram.



49
50
51
52
53
54
55
# File 'lib/rubino/metrics.rb', line 49

def initialize(name, help, buckets: DEFAULT_BUCKETS)
  @name = name
  @help = help
  @buckets = buckets
  @observations = Hash.new { |h, k| h[k] = { counts: Hash.new(0), sum: 0.0, count: 0 } }
  @mutex = Mutex.new
end

Instance Attribute Details

#bucketsObject (readonly)

Returns the value of attribute buckets.



47
48
49
# File 'lib/rubino/metrics.rb', line 47

def buckets
  @buckets
end

#helpObject (readonly)

Returns the value of attribute help.



47
48
49
# File 'lib/rubino/metrics.rb', line 47

def help
  @help
end

#nameObject (readonly)

Returns the value of attribute name.



47
48
49
# File 'lib/rubino/metrics.rb', line 47

def name
  @name
end

Instance Method Details

#eachObject



69
70
71
# File 'lib/rubino/metrics.rb', line 69

def each(&)
  @mutex.synchronize { @observations.dup }.each(&)
end

#observe(value, **labels) ⇒ Object

Record one observation. Increments every bucket with ‘le >= value`, the `+Inf` bucket, the `_sum`, and the `_count`.



59
60
61
62
63
64
65
66
67
# File 'lib/rubino/metrics.rb', line 59

def observe(value, **labels)
  @mutex.synchronize do
    obs = @observations[labels]
    @buckets.each { |b| obs[:counts][b] += 1 if value <= b }
    obs[:counts]["+Inf"] += 1
    obs[:sum] += value
    obs[:count] += 1
  end
end

#typeObject



73
# File 'lib/rubino/metrics.rb', line 73

def type = :histogram