Skip to content

Instantly share code, notes, and snippets.

@pgaertig
Created December 2, 2012 22:36
Show Gist options
  • Save pgaertig/4191364 to your computer and use it in GitHub Desktop.
Save pgaertig/4191364 to your computer and use it in GitHub Desktop.
Dogslow like functionality for Rack and Rails
#Add this to your development.rb
config.middleware.use DogslowRack
#create it in app/middleware
class DogslowRack
FILE_PATH = File.expand_path(__FILE__)
def initialize(app, options={})
@wdt = Thread.new do
Thread.current.abort_on_exception = true
loop do
sleep 2 #sampling is 2sec
Thread.list.each do |thread|
if thread[:request_start_time] && thread[:request_start_time] < Time.now - 10.seconds
thread[:request_start_time] = nil
puts "Slow thread TID-#{thread.object_id.to_s(36)}"
app_trace = thread.backtrace.take_while{|frame| !frame.starts_with?(FILE_PATH)}
puts app_trace.join("\n")
end
end
end
end
puts "Started DogslowRack as #{@wdt}"
end
def call(env)
Thread.current[:request_start_time] = Time.now
@app.call(env)
ensure
Thread.current[:request_start_time] = nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment