Skip to content

Instantly share code, notes, and snippets.

@johndel
Last active December 29, 2015 14:28
Show Gist options
  • Save johndel/7683675 to your computer and use it in GitHub Desktop.
Save johndel/7683675 to your computer and use it in GitHub Desktop.
A simple way to monitor your precious app. Put it inside the initializers and log valuable informations.
ActiveSupport::Notifications.subscribe "sql.active_record" do |name, started, finished, unique_id, data|
Thread.current["active_record_sql_count"] ||= 0
if data[:name].nil? || data[:name] != "SCHEMA"
Thread.current["active_record_sql_count"] = Thread.current["active_record_sql_count"] + 1
end
end
ActiveSupport::Notifications.subscribe "process_action.action_controller" do |name, started, finished, unique_id, data|
time = Time.now
slow_logfile = File.open(Rails.root.join("log", "slow.log"), 'a')
if Thread.current["active_record_sql_count"] && Thread.current["active_record_sql_count"] > 10
output = "===Time: #{time} = Too many queries = Number of queries: #{Thread.current['active_record_sql_count']} - started: #{started} - finished: #{finished} - data: #{data}\n"
Rails.logger.warn(output)
slow_logfile.puts(output)
end
if (data[:db_runtime] && data[:db_runtime] > 5000) || (data[:view_runtime] && data[:view_runtime] > 5000)
output = "===Time: #{time} = Slow Controller Action = started: #{started} - finished: #{finished} - data: #{data}\n"
Rails.logger.warn(output)
slow_logfile << output
end
slow_logfile.close()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment