Class: Flare::MetricStorage

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeMetricStorage

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



43
44
45
# File 'lib/flare/metric_storage.rb', line 43

def [](key)
  @storage[key]
end

#add(key, count:, sum_ms:, error_count: 0) ⇒ Object



19
20
21
22
# File 'lib/flare/metric_storage.rb', line 19

def add(key, count:, sum_ms:, error_count: 0)
  counter = @storage.compute_if_absent(key) { MetricCounter.new }
  counter.add(count: count, sum_ms: sum_ms, error_count: error_count)
end

#drainObject

Atomically retrieves and clears all metrics. Returns a frozen hash of MetricKey => counter data.



26
27
28
29
30
31
32
33
# File 'lib/flare/metric_storage.rb', line 26

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

Returns:

  • (Boolean)


39
40
41
# File 'lib/flare/metric_storage.rb', line 39

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

#sizeObject



35
36
37
# File 'lib/flare/metric_storage.rb', line 35

def size
  @storage.size
end