Skip to content

Instantly share code, notes, and snippets.

@thisismydesign
Created April 25, 2018 08:56
Show Gist options
  • Save thisismydesign/3cb3f48a7cd67f26d17b35f1420b4517 to your computer and use it in GitHub Desktop.
Save thisismydesign/3cb3f48a7cd67f26d17b35f1420b4517 to your computer and use it in GitHub Desktop.
Rails: slow query logging instrumentation
ActiveSupport::Notifications.subscribe("sql.active_record") do |_name, start, finish, _id, payload|
duration = finish.to_f - start.to_f
if duration >= 1.0 # Feel free to change
Rails.logger.warn "slow query detected: #{payload[:sql]}, duration: #{duration}"
caller.each do |line|
next unless line.include? Dir.pwd
Rails.logger.warn line
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment