Skip to content

Commit

Permalink
Prefer request_id over uuid and test the alias
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelfranca committed Feb 20, 2015
1 parent 7f5cf3a commit 027d484
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
8 changes: 4 additions & 4 deletions actionpack/lib/action_dispatch/http/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def initialize(env)
@original_fullpath = nil
@fullpath = nil
@ip = nil
@uuid = nil
@request_id = nil
end

def check_path_parameters!
Expand Down Expand Up @@ -249,11 +249,11 @@ def remote_ip
#
# This unique ID is useful for tracing a request from end-to-end as part of logging or debugging.
# This relies on the rack variable set by the ActionDispatch::RequestId middleware.
def uuid
@uuid ||= env["action_dispatch.request_id"]
def request_id
@request_id ||= env["action_dispatch.request_id"]
end

alias_method :request_id, :uuid
alias_method :uuid, :request_id

# Returns the lowercase name of the HTTP server software.
def server_software
Expand Down
12 changes: 8 additions & 4 deletions actionpack/test/dispatch/request_id_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@

class RequestIdTest < ActiveSupport::TestCase
test "passing on the request id from the outside" do
assert_equal "external-uu-rid", stub_request('HTTP_X_REQUEST_ID' => 'external-uu-rid').uuid
assert_equal "external-uu-rid", stub_request('HTTP_X_REQUEST_ID' => 'external-uu-rid').request_id
end

test "ensure that only alphanumeric uurids are accepted" do
assert_equal "X-Hacked-HeaderStuff", stub_request('HTTP_X_REQUEST_ID' => '; X-Hacked-Header: Stuff').uuid
assert_equal "X-Hacked-HeaderStuff", stub_request('HTTP_X_REQUEST_ID' => '; X-Hacked-Header: Stuff').request_id
end

test "ensure that 255 char limit on the request id is being enforced" do
assert_equal "X" * 255, stub_request('HTTP_X_REQUEST_ID' => 'X' * 500).uuid
assert_equal "X" * 255, stub_request('HTTP_X_REQUEST_ID' => 'X' * 500).request_id
end

test "generating a request id when none is supplied" do
assert_match(/\w+-\w+-\w+-\w+-\w+/, stub_request.uuid)
assert_match(/\w+-\w+-\w+-\w+-\w+/, stub_request.request_id)
end

test "uuid alias" do
assert_equal "external-uu-rid", stub_request('HTTP_X_REQUEST_ID' => 'external-uu-rid').uuid
end

private
Expand Down
2 changes: 1 addition & 1 deletion guides/source/rails_on_rack.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ Much of Action Controller's functionality is implemented as Middlewares. The fol

**`ActionDispatch::RequestId`**

* Makes a unique `X-Request-Id` header available to the response and enables the `ActionDispatch::Request#uuid` method.
* Makes a unique `X-Request-Id` header available to the response and enables the `ActionDispatch::Request#request_id` method.

**`Rails::Rack::Logger`**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Rails.application.configure do
config.log_level = :debug

# Prepend all log lines with the following tags.
# config.log_tags = [ :subdomain, :uuid ]
# config.log_tags = [ :subdomain, :request_id ]

# Use a different logger for distributed setups.
# config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
Expand Down

0 comments on commit 027d484

Please sign in to comment.