Created
December 2, 2012 22:36
-
-
Save pgaertig/4191364 to your computer and use it in GitHub Desktop.
Dogslow like functionality for Rack and Rails
This file contains hidden or 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
#Add this to your development.rb | |
config.middleware.use DogslowRack |
This file contains hidden or 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
#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