Class: Pgbus::RateCounter
- Inherits:
-
Object
- Object
- Pgbus::RateCounter
- Defined in:
- lib/pgbus/rate_counter.rb
Overview
Thread-safe rate counter inspired by LavinMQ’s rate_stats macro. Tracks absolute counts via AtomicFixnum and computes rates as deltas between periodic snapshots.
Usage:
counter = RateCounter.new(:enqueued, :dequeued, :failed)
counter.increment(:enqueued)
counter.snapshot! # call periodically (e.g. every 5s)
counter.rate(:enqueued) # => msgs/s since last snapshot
counter.rates # => { enqueued: 12.4, dequeued: 10.1, failed: 0.2 }
Instance Attribute Summary collapse
-
#names ⇒ Object
readonly
Returns the value of attribute names.
Instance Method Summary collapse
- #count(name) ⇒ Object
- #counts ⇒ Object
- #increment(name, delta = 1) ⇒ Object
-
#initialize(*names) ⇒ RateCounter
constructor
A new instance of RateCounter.
- #rate(name) ⇒ Object
- #rates ⇒ Object
- #snapshot! ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(*names) ⇒ RateCounter
Returns a new instance of RateCounter.
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/pgbus/rate_counter.rb', line 19 def initialize(*names) @names = names.map(&:to_sym) @counters = {} @last_values = {} @rates = {} @last_snapshot_at = monotonic_now @names.each do |name| @counters[name] = Concurrent::AtomicFixnum.new(0) @last_values[name] = 0 @rates[name] = 0.0 end end |
Instance Attribute Details
#names ⇒ Object (readonly)
Returns the value of attribute names.
17 18 19 |
# File 'lib/pgbus/rate_counter.rb', line 17 def names @names end |
Instance Method Details
#count(name) ⇒ Object
37 38 39 |
# File 'lib/pgbus/rate_counter.rb', line 37 def count(name) @counters.fetch(name).value end |
#counts ⇒ Object
49 50 51 |
# File 'lib/pgbus/rate_counter.rb', line 49 def counts @names.to_h { |name| [name, @counters[name].value] } end |
#increment(name, delta = 1) ⇒ Object
33 34 35 |
# File 'lib/pgbus/rate_counter.rb', line 33 def increment(name, delta = 1) @counters.fetch(name).increment(delta) end |
#rate(name) ⇒ Object
41 42 43 |
# File 'lib/pgbus/rate_counter.rb', line 41 def rate(name) @rates.fetch(name) end |
#rates ⇒ Object
45 46 47 |
# File 'lib/pgbus/rate_counter.rb', line 45 def rates @names.to_h { |name| [name, @rates[name]] } end |
#snapshot! ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/pgbus/rate_counter.rb', line 53 def snapshot! now = monotonic_now elapsed = now - @last_snapshot_at return if elapsed <= 0 @names.each do |name| current = @counters[name].value delta = current - @last_values[name] @rates[name] = (delta / elapsed).round(1) @last_values[name] = current end @last_snapshot_at = now end |
#to_h ⇒ Object
68 69 70 71 72 73 |
# File 'lib/pgbus/rate_counter.rb', line 68 def to_h { counts: counts, rates: rates } end |