Class: Flare::MetricCounter
- Inherits:
-
Object
- Object
- Flare::MetricCounter
- Defined in:
- lib/flare/metric_counter.rb
Overview
Thread-safe counter for metric aggregation. Uses atomic operations for lock-free increments.
Note: Durations are stored as integer milliseconds. Sub-millisecond durations are truncated to 0. For very fast operations (e.g., cache hits), the sum_ms may undercount actual time spent.
Instance Method Summary collapse
- #add(count:, sum_ms:, error_count: 0) ⇒ Object
- #count ⇒ Object
- #error_count ⇒ Object
- #increment(duration_ms:, error: false) ⇒ Object
-
#initialize ⇒ MetricCounter
constructor
A new instance of MetricCounter.
- #sum_ms ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize ⇒ MetricCounter
Returns a new instance of MetricCounter.
13 14 15 16 17 |
# File 'lib/flare/metric_counter.rb', line 13 def initialize @count = Concurrent::AtomicFixnum.new(0) @sum_ms = Concurrent::AtomicFixnum.new(0) @error_count = Concurrent::AtomicFixnum.new(0) end |
Instance Method Details
#add(count:, sum_ms:, error_count: 0) ⇒ Object
25 26 27 28 29 |
# File 'lib/flare/metric_counter.rb', line 25 def add(count:, sum_ms:, error_count: 0) @count.increment(count.to_i) @sum_ms.increment(sum_ms.to_i) @error_count.increment(error_count.to_i) end |
#count ⇒ Object
31 32 33 |
# File 'lib/flare/metric_counter.rb', line 31 def count @count.value end |
#error_count ⇒ Object
39 40 41 |
# File 'lib/flare/metric_counter.rb', line 39 def error_count @error_count.value end |
#increment(duration_ms:, error: false) ⇒ Object
19 20 21 22 23 |
# File 'lib/flare/metric_counter.rb', line 19 def increment(duration_ms:, error: false) @count.increment @sum_ms.increment(duration_ms.to_i) @error_count.increment if error end |
#sum_ms ⇒ Object
35 36 37 |
# File 'lib/flare/metric_counter.rb', line 35 def sum_ms @sum_ms.value end |
#to_h ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/flare/metric_counter.rb', line 43 def to_h { count: @count.value, sum_ms: @sum_ms.value, error_count: @error_count.value } end |