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}"
    end
}

Run the request again:

> app.get "/foo/bar/baz
/data/APP/current/config/initializers/action_controller.rb:8
/data/APP/current/config/initializers/action_controller.rb:11
/data/APP/current/config/initializers/action_controller.rb:11
[...]
/data/APP/current/config/app/models/comment/concerns/admin.rb:34

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, Logger.new(STDOUT)

# Rails 3 & 4
ActiveRecord::Base.logger = Logger.new(STDOUT)

Conclusion

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

That's it for today, cheers!