Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
test middleware for Ruby 2.0 logging the method dispatches going on when a request is being handled.
class TracePoint
class Middleware
def initialize(app)
@app = app
end
def call(env)
stats = {}
trace = TracePoint.new(:call) do |tp|
stats[tp.defined_class] ||= {}
stats[tp.defined_class][tp.method_id] ||= 0
stats[tp.defined_class][tp.method_id] += 1
end
trace.enable
response = @app.call(env)
trace.disable
puts env['PATH_INFO']
puts "#{stats.keys.size} classes used"
puts "#{stats.map{|k,v| v.keys}.flatten.size} methods used"
puts "#{stats.map{|k,v| v.values}.flatten.sum} methods dispatched"
#File.open("tmp/#{env['PATH_INFO'].gsub('/', '_')}_req_stats.json", "w"){|f| f << stats.to_json }
puts ""
response
end
end
end
@mattetti

This comment has been minimized.

Copy link
Owner Author

mattetti commented Mar 6, 2013

to use in Rails, add the following to the application.rb file:

config.middleware.insert_before(ActionDispatch::Static, TracePoint::Middleware)
@chucai

This comment has been minimized.

Copy link

chucai commented Dec 20, 2013

I got the following error from my Rails application when I use this code.

ERROR ArgumentError: wrong number of arguments (1 for 5)
    /Users/xdhe/Documents/FreeWheel/rpm/ui/payment/lib/tracepoint_middleware.rb:12:in `initialize'

Can you give me some help? Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.