Class: CloseYourIt::Stats

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

Overview

Contatori diagnostici thread-safe del client: quanti eventi sono stati accodati, scartati (coda piena), spediti con successo o falliti (rete o status non-2xx). Servono a rendere visibili i fallimenti silenziosi del trasporto fire-and-forget.

CloseYourIt.stats.to_h # => { enqueued: 12, dropped: 0, sent: 11, failed: 1 }

Constant Summary collapse

COUNTERS =
%i[enqueued dropped sent failed].freeze

Instance Method Summary collapse

Constructor Details

#initializeStats

Returns a new instance of Stats.



14
15
16
# File 'lib/closeyourit/stats.rb', line 14

def initialize
  @counters = COUNTERS.to_h { |name| [ name, Concurrent::AtomicFixnum.new(0) ] }
end

Instance Method Details

#[](name) ⇒ Object



24
25
26
# File 'lib/closeyourit/stats.rb', line 24

def [](name)
  @counters.fetch(name).value
end

#increment(name) ⇒ Object



18
19
20
21
22
# File 'lib/closeyourit/stats.rb', line 18

def increment(name)
  counter = @counters.fetch(name)
  counter.increment
  counter.value
end

#reset!Object



32
33
34
35
# File 'lib/closeyourit/stats.rb', line 32

def reset!
  @counters.each_value { |counter| counter.value = 0 }
  self
end

#to_hObject



28
29
30
# File 'lib/closeyourit/stats.rb', line 28

def to_h
  @counters.transform_values(&:value)
end