Created
June 24, 2016 13:48
-
-
Save gjastrab/53410eee6827cf1b44426bd95cf12019 to your computer and use it in GitHub Desktop.
Log Connection Pool check ins and outs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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