« Just in case you missed it... | Main | Forking Ruby—my RubyConf Keynote is now up »

November 30, 2008

See Rails request paths in 'top'

During our sale, we had one particular request that came in and wedged the application: every time it hit, the mongrel process size zoomed steadily up to 500Mb, so we had to kill it. But finding out which request was doing this was tricky. The log files didn't help—with the amount of traffic we were getting, it was a small needle and a large haystack.

Eventually, we found the culprit. But it would have been a lot easier if I'd thought of this hack on Friday, and not after the sale ended.

If you put this into your application controller:

before_filter :set_process_name_from_request
def set_process_name_from_request
  $0 = request.path[0,16] 
end   

after_filter :unset_process_name_from_request
def unset_process_name_from_request
  $0 = request.path[0,15] + "*"
end  

then Ruby will set the cmd field in your process control block to the first 16 characters of the request path. You can then use top to see what request is being handled by each mongrel.

Pragmatic@ey00-s00220_~ — ssh — 155×40


Once the request has been handled, an asterisk sign is appended, so you can see the last URL when a mongrel becomes idle. 

Pragmatic@ey00-s00220_~ — ssh — 155×40-1

If your version of top doesn't show the short command by default, use the c keyboard command to see it.

This is probably common knowledge, but I thought it was cool.

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d83451c41c69e2010536253aa9970b

Listed below are links to weblogs that reference See Rails request paths in 'top':

Comments

A plugin that we had used for similar purpose:

http://purefiction.net/mongrel_proctitle/

Though it is more useful to track slow requests. Your idea of tracking what a instance was doing last is interesting.

Thx a lot!

BTW: is there any possible PHP do it, too?

Not common knowledge, and definitely cool. Thanks!

Thanks a lot. That was very useful. Implemented on all my ruby on rails websites :-)

Hadn't thunk it. Gracias!

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

Now in Beta

  • Programming Ruby, 3rd Edition
    Third Edition, Covering Ruby 1.9, now available
My Photo

Pragmatic Stuff

Photos

  • www.flickr.com
    This is a Flickr badge showing public photos from pragdave tagged with pragdave_badge. Make your own badge here.

Site Search

  • Google Search

    The web
    PragDave