ASCII Thoughts

Using Pry in production (part 3)

This last trick is not specific to Pry, but is very useful nonetheless.

Stuck request

I had a problem recently where one particular discussion in my application wouldn't load. It looked like the error was a timeout, and as result Nginx was returning a 500. Since this was a timeout and not an exception, the error did not show in New Relic, which made it difficult to debug.

To the console!

My first check was to verify whether the same request would eventually run in the console, and if it was just really slow and needed optimization:

> app.get "/discussions/problems/1234"
................ 10min later, still nothing ..............

So the request was stuck somewhere, but where? set_trace_func to the rescue!

set_trace_func proc { |event, file, line, id, binding, klass|
    if event == "line" && file[RAILS_ROOT]
      puts "#{file}:#{line}"

Run the request again:

> app.get "/foo/bar/baz

And there I had it. The exact line where the request was stuck. It turned out to be a bad string/regex combination, but I'm not sure how else I could have figured it out. set_trace_func definitely was handy.

Logging SQL requests

Also very useful when debugging requests in the console:

# Rails 2
ActiveRecord::Base.connection.instance_variable_set :@logger,

# Rails 3 & 4
ActiveRecord::Base.logger =


So that concludes our short serie on Pry. I hope you enjoyed it :)

That's it for today, cheers!