Skip to content

Instantly share code, notes, and snippets.

@gjastrab
Created June 24, 2016 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gjastrab/53410eee6827cf1b44426bd95cf12019 to your computer and use it in GitHub Desktop.
Save gjastrab/53410eee6827cf1b44426bd95cf12019 to your computer and use it in GitHub Desktop.
Log Connection Pool check ins and outs
ActiveRecord::ConnectionAdapters::AbstractAdapter.class_eval do
set_callback(:checkout, :after, :log_after_checkout)
set_callback(:checkin, :after, :log_after_checkin)
def connection_info_for_logging
pool = ActiveRecord::Base.connection_pool
pool_size = pool.size
count = pool.connections.count
in_use = pool.connections.count(&:in_use?)
queue = pool.instance_variable_get(:@available)
waiting_in_queue = queue.num_waiting
"connection_pool: size: #{pool_size}, connections: #{count}, in use: #{in_use}, waiting_in_queue: #{waiting_in_queue}"
end
def log_after_checkin
logger.debug { " [[POOL INFO]] #{self.class.name.demodulize}##{__method__}, #{connection_info_for_logging}" } if logger && ActiveRecord::Base.connected?
end
def log_after_checkout
logger.debug { " [[POOL INFO]] #{self.class.name.demodulize}##{__method__}, #{connection_info_for_logging}" } if logger && ActiveRecord::Base.connected?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment