Class: OpenTrace::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/opentrace/stats.rb

Constant Summary collapse

COUNTERS =
%i[
  enqueued
  delivered
  dropped_queue_full
  dropped_circuit_open
  dropped_auth_suspended
  dropped_error
  retries
  rate_limited
  auth_failures
  payload_splits
  batches_sent
  bytes_sent
].freeze

Instance Method Summary collapse

Constructor Details

#initializeStats

Returns a new instance of Stats.



20
21
22
23
24
# File 'lib/opentrace/stats.rb', line 20

def initialize
  @counters = COUNTERS.each_with_object({}) { |k, h| h[k] = 0 }
  @mutex = Mutex.new
  @started_at = Time.now
end

Instance Method Details

#get(counter) ⇒ Object



30
31
32
# File 'lib/opentrace/stats.rb', line 30

def get(counter)
  @mutex.synchronize { @counters[counter] }
end

#increment(counter, amount = 1) ⇒ Object



26
27
28
# File 'lib/opentrace/stats.rb', line 26

def increment(counter, amount = 1)
  @mutex.synchronize { @counters[counter] += amount }
end

#reset!Object



40
41
42
43
44
45
# File 'lib/opentrace/stats.rb', line 40

def reset!
  @mutex.synchronize do
    @counters.transform_values! { 0 }
    @started_at = Time.now
  end
end

#to_hObject



34
35
36
37
38
# File 'lib/opentrace/stats.rb', line 34

def to_h
  @mutex.synchronize do
    @counters.merge(uptime_seconds: (Time.now - @started_at).to_i).dup
  end
end