Permalink
Browse files

use caller supplied request guid if it exists

Change-Id: I18b597ff331fb8ca644f9d450b70220272ef879b
  • Loading branch information...
1 parent b8548e4 commit 646ebb75ca3e89574fbcd293f05cdbd663d5afb7 @pbozeman pbozeman committed Oct 22, 2012
Showing with 61 additions and 1 deletion.
  1. +13 −1 lib/sinatra/vcap.rb
  2. +48 −0 spec/sinatra/vcap_spec.rb
View
@@ -115,7 +115,19 @@ def vcap_configure(opts = {})
end
logger_name = opts[:logger_name] || "vcap.api"
env["rack.logger"] = Steno.logger(logger_name)
- @request_guid = SecureRandom.uuid
+
+ @request_guid = env["X_VCAP_REQUEST_ID"]
+ @request_guid ||= env["X_REQUEST_ID"]
+
+ # we append a new guid to the request because we have no idea if the
+ # caller is really going to be giving us a unique guid, i.e. they might
+ # generate the guid and then turn around and make 3 api calls using it.
+ if @request_guid
+ @request_guid = "#{@request_guid}::#{SecureRandom.uuid}"
+ else
+ @request_guid ||= SecureRandom.uuid
+ end
+
Thread.current[:vcap_request_id] = @request_guid
Steno.config.context.data["request_guid"] = @request_guid
end
@@ -126,4 +126,52 @@ def app
last_response.body.should == last_response.headers["X-VCAP-Request-ID"]
end
end
+
+ describe "caller provided x-vcap-request-id" do
+ before(:all) do
+ get "/request_id", {}, { "X_VCAP_REQUEST_ID" => "abcdef" }
+ end
+
+ it "should set the X-VCAP-Request-ID to the caller specified value" do
+ last_response.status.should == 200
+ last_response.headers["X-VCAP-Request-ID"].should match /abcdef::.*/
+ end
+
+ it "should access the request id via Thread.current[:request_id]" do
+ last_response.status.should == 200
+ last_response.body.should match /abcdef::.*/
+ end
+ end
+
+ describe "caller provided x-request-id" do
+ before(:all) do
+ get "/request_id", {}, { "X_REQUEST_ID" => "abcdef" }
+ end
+
+ it "should set the X-VCAP-Request-ID to the caller specified value" do
+ last_response.status.should == 200
+ last_response.headers["X-VCAP-Request-ID"].should match /abcdef::.*/
+ end
+
+ it "should access the request id via Thread.current[:request_id]" do
+ last_response.status.should == 200
+ last_response.body.should match /abcdef::.*/
+ end
+ end
+
+ describe "caller provided x-request-id and x-vcap-request-id" do
+ before(:all) do
+ get "/request_id", {}, { "X_REQUEST_ID" => "abc", "X_VCAP_REQUEST_ID" => "def" }
+ end
+
+ it "should set the X-VCAP-Request-ID to the caller specified value of X_VCAP_REQUEST_ID" do
+ last_response.status.should == 200
+ last_response.headers["X-VCAP-Request-ID"].should match /def::.*/
+ end
+
+ it "should access the request id via Thread.current[:request_id]" do
+ last_response.status.should == 200
+ last_response.body.should match /def::.*/
+ end
+ end
end

0 comments on commit 646ebb7

Please sign in to comment.