Class: Flare::MetricStorage
- Inherits:
-
Object
- Object
- Flare::MetricStorage
- Defined in:
- lib/flare/metric_storage.rb
Overview
Thread-safe storage for metric aggregation. Uses Concurrent::Map for lock-free reads and writes.
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#drain ⇒ Object
Atomically retrieves and clears all metrics.
- #empty? ⇒ Boolean
- #increment(key, duration_ms:, error: false) ⇒ Object
-
#initialize ⇒ MetricStorage
constructor
A new instance of MetricStorage.
- #size ⇒ Object
Constructor Details
#initialize ⇒ MetricStorage
Returns a new instance of MetricStorage.
10 11 12 |
# File 'lib/flare/metric_storage.rb', line 10 def initialize @storage = Concurrent::Map.new end |
Instance Method Details
#[](key) ⇒ Object
38 39 40 |
# File 'lib/flare/metric_storage.rb', line 38 def [](key) @storage[key] end |
#drain ⇒ Object
Atomically retrieves and clears all metrics. Returns a frozen hash of MetricKey => counter data.
21 22 23 24 25 26 27 28 |
# File 'lib/flare/metric_storage.rb', line 21 def drain result = {} @storage.keys.each do |key| counter = @storage.delete(key) result[key] = counter.to_h if counter end result.freeze end |
#empty? ⇒ Boolean
34 35 36 |
# File 'lib/flare/metric_storage.rb', line 34 def empty? @storage.empty? end |
#increment(key, duration_ms:, error: false) ⇒ Object
14 15 16 17 |
# File 'lib/flare/metric_storage.rb', line 14 def increment(key, duration_ms:, error: false) counter = @storage.compute_if_absent(key) { MetricCounter.new } counter.increment(duration_ms: duration_ms, error: error) end |
#size ⇒ Object
30 31 32 |
# File 'lib/flare/metric_storage.rb', line 30 def size @storage.size end |