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
  dropped_filtered
  retries
  rate_limited
  auth_failures
  payload_splits
  batches_sent
  bytes_sent
  sampled_out
  sql_filtered
].freeze

Instance Method Summary collapse

Constructor Details

#initializeStats

Returns a new instance of Stats.



23
24
25
26
27
# File 'lib/opentrace/stats.rb', line 23

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

Instance Method Details

#get(counter) ⇒ Object



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

def get(counter)
  @counters[counter]
end

#increment(counter, amount = 1) ⇒ Object

Hot path: no mutex. Under CRuby’s GIL, Hash#[]= with integer increment is effectively atomic for single operations.



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

def increment(counter, amount = 1)
  @counters[counter] += amount
end

#reset!Object



46
47
48
49
50
51
# File 'lib/opentrace/stats.rb', line 46

def reset!
  @mutex.synchronize do
    @counters = Hash.new(0)
    @started_at = Time.now
  end
end

#to_hObject

Cold path: mutex for consistent snapshot



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

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