rails/rails — Issue #5176: Make RequestId globally available
Author: mperham
Created: 2012-02-25 21:12:54+00:00
State: open

RequestId is only available via `request.uuid` at the moment. This should be attached to a Thread local so that the uuid can be accessed in ActiveRecord and other libraries that have nothing to do with HTTP. In my case, I want [Sidekiq](/mperham/sidekiq) to include the RequestId with any messages so the Sidekiq log can show the RequestId that created the message but I don't want to tack on another optional parameter to my API and have the user to pass me the request.uuid, like so:

def create
# create user
# send a delayed email
UserEmails.delay(:uuid => request.uuid).send_welcome_email(@user)
# run something async
SomeWorker.context(:uuid => request.uuid).perform_asyc(@user)
end

That's messy. It would be nice if I could just internally call ActionDispatch::Request.uuid to get the current uuid or nil.