Created
March 30, 2018 16:56
-
-
Save EdwardDiehl/329952e329d4670c65bf46da168723a2 to your computer and use it in GitHub Desktop.
Ruby on Rails – your own slow query log, no sql configuration required
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
# http://pdabrowski.com/blog/ruby-on-rails/slow-query-log/ | |
# config/initializer/slow_query_logger.rb | |
class SlowQueryLogger | |
MAX_DURATION = 3.0 | |
def self.initialize! | |
ActiveSupport::Notifications.subscribe('sql.active_record') do |name, start, finish, id, payload| | |
duration = finish.to_f - start.to_f | |
if duration >= MAX_DURATION | |
SomeLogger.log("slow query detected: #{payload[:sql]}, duration: #{duration}") | |
end | |
end | |
end | |
end | |
SlowQueryLogger.initialize! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment