###

alias hbin me

Rails HTTP Request IDs

The HTTP Request IDs makes it easy to trace requests from end-to-end in the stack and to identify individual requests in mixed logs like Syslog.

Rails 3.2 introduced the ActionDispatch::RequestId middleware that make a unique X-Request-Id header avariable to the response.

For example: curl -I http://sample.dev/

1
2
3
4
5
6
HTTP/1.1 200 OK
...
X-Request-Id: ddaa28e2-3395-4e66-9ca7-48480882a1df
X-Runtime: 0.045724
Date: Fri, 04 Jul 2014 03:23:43 GMT
Connection: close

To show this request ID in with your application logs, add this line to your config/environments/production.rb

1
config.log_tags = [:uuid]

The logs will then be tagged with the Request ID:

1
2
3
4
5
6
7
I, [2014-07-04T11:23:43.752028 #33305]  INFO -- : [ddaa28e2-3395-4e66-9ca7-48480882a1df] Started HEAD "/" for 127.0.0.1 at 2014-07-04 11:23:43 +0800
I, [2014-07-04T11:23:43.755366 #33305]  INFO -- : [ddaa28e2-3395-4e66-9ca7-48480882a1df] Processing by StaticPagesController#home as */*
I, [2014-07-04T11:23:43.759832 #33305]  INFO -- : [ddaa28e2-3395-4e66-9ca7-48480882a1df]   Rendered static_pages/home.html.erb within layouts/application (0.2ms)
I, [2014-07-04T11:23:43.791258 #33305]  INFO -- : [ddaa28e2-3395-4e66-9ca7-48480882a1df]   Rendered layouts/_shim.html.erb (0.0ms)
I, [2014-07-04T11:23:43.793947 #33305]  INFO -- : [ddaa28e2-3395-4e66-9ca7-48480882a1df]   Rendered layouts/_header.html.erb (0.3ms)
I, [2014-07-04T11:23:43.796489 #33305]  INFO -- : [ddaa28e2-3395-4e66-9ca7-48480882a1df]   Rendered layouts/_footer.html.erb (0.2ms)
I, [2014-07-04T11:23:43.797112 #33305]  INFO -- : [ddaa28e2-3395-4e66-9ca7-48480882a1df] Completed 200 OK in 42ms (Views: 41.2ms | ActiveRecord: 0.0ms)

References:

  1. http://guides.rubyonrails.org/3_2_release_notes.html#action-dispatch
  2. https://devcenter.heroku.com/articles/http-request-id

Comments