Skip to content

Instantly share code, notes, and snippets.

@shime
Created December 21, 2012 19:04
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 shime/4355022 to your computer and use it in GitHub Desktop.
Save shime/4355022 to your computer and use it in GitHub Desktop.
Tracking request durations in Rails. Courtesy of @stathat
if Rails.env.production?
slow_logfile = File.open(Rails.root.join("log", "slow.log"), 'a')
slow_log = Logger.new(slow_logfile)
slow_log.level = Logger::INFO
ActiveSupport::Notifications.subscribe "process_action.action_controller" do |name, start, finish, id, payload|
duration = (finish - start) * 1000
StatHat::API.ez_post_value("rails request duration", "info@stathat.com", duration)
view_time = 0
db_time = 0
unless payload[:view_runtime].nil?
view_time = payload[:view_runtime]
StatHat::API.ez_post_value("rails request view duration", "info@stathat.com", view_time)
end
unless payload[:db_runtime].nil?
db_time = payload[:db_runtime]
StatHat::API.ez_post_value("rails request db duration", "info@stathat.com", db_time)
end
if view_time > 100
slow_log.info("#{Time.now.to_formatted_s(:db)} slow request (slow view) (#{duration.to_i}ms total, view: #{view_time.to_i}ms): #{payload[:path]} controller: #{payload[:controller]}, action: #{payload[:action]}")
slow_logfile.flush
end
if db_time > 100
slow_log.info("#{Time.now.to_formatted_s(:db)} slow request (slow db) (#{duration.to_i}ms total, db: #{db_time.to_i}ms): #{payload[:path]} controller: #{payload[:controller]}, action: #{payload[:action]}")
slow_logfile.flush
end
if duration > 500
StatHat::API.ez_post_count("rails slow request", "info@stathat.com", 1)
slow_log.info("#{Time.now.to_formatted_s(:db)} slow request (#{duration.to_i}ms, view: #{view_time.to_i}ms, db: #{db_time.to_i}ms): #{payload[:path]} controller: #{payload[:controller]}, action: #{payload[:action]}")
slow_logfile.flush
end
end
end
@sebastianvirlan
Copy link

Hi, is there any solution in order to print :view_runtime and :db_runtime direct to the webpage?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment