Module: DeadBro::DbConnectionSubscriber::CheckoutInstrumentation

Defined in:
lib/dead_bro/db_connection_subscriber.rb

Overview

Prepended onto ConnectionPool so every checkout is timed. Only accumulates when a request is being tracked (thread-local is a Numeric).

Instance Method Summary collapse

Instance Method Details

#checkout(*args) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/dead_bro/db_connection_subscriber.rb', line 11

def checkout(*args)
  return super unless Thread.current[DbConnectionSubscriber::WAIT_KEY].is_a?(Numeric)

  # Initialize conn before calling super so the rescue block can tell whether
  # checkout succeeded before timing code raised (avoids double-checkout).
  conn = nil
  t0   = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  conn = super
  Thread.current[DbConnectionSubscriber::WAIT_KEY]  += (Process.clock_gettime(Process::CLOCK_MONOTONIC) - t0) * 1000.0
  Thread.current[DbConnectionSubscriber::COUNT_KEY] += 1
  conn
rescue
  conn || super
end