Class: Rubino::Metrics::Histogram
- Inherits:
-
Object
- Object
- Rubino::Metrics::Histogram
- 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
-
#buckets ⇒ Object
readonly
Returns the value of attribute buckets.
-
#help ⇒ Object
readonly
Returns the value of attribute help.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #each ⇒ Object
-
#initialize(name, help, buckets: DEFAULT_BUCKETS) ⇒ Histogram
constructor
A new instance of Histogram.
-
#observe(value, **labels) ⇒ Object
Record one observation.
- #type ⇒ Object
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
#buckets ⇒ Object (readonly)
Returns the value of attribute buckets.
47 48 49 |
# File 'lib/rubino/metrics.rb', line 47 def buckets @buckets end |
#help ⇒ Object (readonly)
Returns the value of attribute help.
47 48 49 |
# File 'lib/rubino/metrics.rb', line 47 def help @help end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
47 48 49 |
# File 'lib/rubino/metrics.rb', line 47 def name @name end |
Instance Method Details
#each ⇒ Object
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 |
#type ⇒ Object
73 |
# File 'lib/rubino/metrics.rb', line 73 def type = :histogram |