back

Debugging Rails 2.3.2 Apps with Rack::Bug

Rack::Bug is a Rack middleware by Bryan Helmkamp that’s useful for figuring out exactly what is going on in your responses and why they’re taking so long.

Here’s a very quick guide/tutorial on using it in a Rails app.

4/23 – Check the bottom of the post for a few updates

Screenshots

Here’s what you’ll end up with when you finish.

Basics

What it is

Rack::Bug can tell you a lot about where your app is spending time and about the request/response environment.

What it isn’t

Rack::Bug won’t help you if your request fails. That is, you still get the normal rails errors without anything extra. Plus, it appears to still have some rough edges.

Setup

First, let’s build a quick sample app. I use RyanB’s nifty-generators below to save some time.

Also, before you start, remember that since it’s middleware, Rack::Bug requires Rails > 2.3.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

rails blog

cd blog

rm public/index.html

sg nifty_layout

sg nifty_scaffold Cat name:string monorail:boolean color:string

rake db:migrate

script/plugin install git://github.com/brynary/rack-bug.git

Now that the plugin is installed, tell rails that we want Rack::Bug added to the middleware stack.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#config/environment.rb

RAILS_GEM_VERSION = '2.3.2' unless defined? RAILS_GEM_VERSION

require File.join(File.dirname(__FILE__), 'boot')

Rails::Initializer.run do |config|


  config.middleware.use "Rack::Bug" 


end

Usage

Now start up the server and open up the bookmarklet. This will let you toggle Rack::Bug on and off. It will ask you for a password, but we haven’t set one so you can click OK.

1
2
3
4

script/server
open http://localhost:3000/__rack_bug__/bookmarklet.html

One you’ve turned it on, just make any request (e.g. localhost:3000/cats/new), and Voila!

Updates

Options

You can pass in several options to Rack::Bug like so (in Rails):

1
2
3

        config.middlware.use("Rack::Bug", :password => 'muzak')

:ip_masks

Allowed ip addresses. Defaults to 127.0.0.1

E.g.:

1
2
3
4
5
6
7
8
9
10


# from http://www.brynary.com/2009/4/22/rack-bug-debugging-toolbar-in-four-minutes

ActionController::Dispatcher.middleware.use Rack::Bug,
  :ip_masks   => [IPAddr.new("127.0.0.1")],
  :secret_key => "epT5uCIchlsHCeR9dloOeAPG66PtHd9K8l0q9avitiaA/KUrY7DE52hD4yWY+8z1",
  :password   => "rack-bug-secret"


:password

If you set a password here, then you will have to enter it when enabling Rack::Bug Defaults to nil.

:secret_key

Set this if you want to be able to use the SQL query debugging toolbar features. Defaults to nil.

:intercept_redirects

1
2
3
4
5


intercept_redirect if @response.redirect? && options["rack-bug.intercept_redirects"]


:panel_classes

Defaults to the normal Rails debugging panels.

Screencast

Bryan has put together a screencast about Rack::Bug here

April 20, 2009

  1. Bryan Helmkamp says:

    Glad you're finding it useful. I'm working on a screencast for a formal release announcement.

    What rough edges did you see? Would like to polish them up.

    Thanks,

    -Bryan

  2. Alderete says:

    I am assuming that, as middleware, it's not usable with Passenger? At least, I tried it, and it didn't seem to work, but did when I accessed the site via Mongrel.

    (I don't know enough about how Passenger is different from e.g. Mongrel in this regard; does Passenger not use Rack?)

  3. Mischa says:

    @alderete, It should work w/ the latest passenger, afaik. What env are you trying to use it in? If you're trying to use it in prod, make sure you set the ip mask etc.

    I've updated the post with some additional documentation on how to do this.

  4. Mischa says:

    @bryan

    Just a few documentation things. I've added them in my fork at:

    http://github.com/mischa/rack-bug/tree/master