Store request.uuid in the current thread #10930

Closed
wants to merge 1 commit into from

3 participants

@samn

This change to ActionDispatch::RequestId stores the
request's id in a slot on the current thread.

This lets included gems (or code that doesn't have
explicit access to the Rack env or ActionDispatch::Request)
forward the request's id to downstream services.

That allows for correlation of log entries across
services in a distributed system, which can be very
helpful in diagnosing issues.

@samn samn Store request.uuid in the current thread
This change to ActionDispatch::RequestId stores the
request's id in a slot on the current thread.

This lets included gems (or code that doesn't have
explicit access to the Rack env or ActionDispatch::Request)
forward the request's id to downstream services.

That allows for correlation of log entries across
services in a distributed system, which can be very
helpful in diagnosing issues.
ac3db66
@senny senny commented on the diff Jun 13, 2013
actionpack/test/dispatch/request_id_test.rb
@@ -17,6 +17,15 @@ class RequestIdTest < ActiveSupport::TestCase
assert_match(/\w+-\w+-\w+-\w+-\w+/, stub_request.uuid)
end
+ test "thread local slot is set" do
+ middleware = ActionDispatch::RequestId.new(lambda do |environment|
+ req = ActionDispatch::Request.new(environment)
+ assert_equal(Thread.current['action_dispatch.request_id'], req.uuid)
+ [ 200, environment, [] ]
@senny
Ruby on Rails member
senny added a note Jun 13, 2013

you could make sure that the lambda is actually being called, otherwise the test would always pass as there is only an assertion inside the lambda.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@egilburg

You could consider implementing a Registry based on https://github.com/rails/rails/blob/master/activesupport/lib/active_support/per_thread_registry.rb for this, so API would be like ActionDispatch::RequestRegistry.request_id or whatever makes sense.

But it would be nice seeing what are some of the use cases you are thinking of. One could argue that encapsulating request info in the controller to the exclusion of other parts of the code is by MVC design.

@pixeltrix
Ruby on Rails member

This has been rejected before, sorry - see #5176 for the discussion.

@pixeltrix pixeltrix closed this Jun 13, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment