Skip to content
This repository
Newer
Older
100644 35 lines (31 sloc) 1.385 kb
ddbb2cae » josevalim
2011-10-19 Require securerandom as it is the proper dependency.
1 require 'securerandom'
1b50207e » josevalim
2011-10-19 Require missing string access dependency.
2 require 'active_support/core_ext/string/access'
afde6fdd » dhh
2011-10-19 Added X-Request-Id tracking and TaggedLogging to easily log that and …
3
4 module ActionDispatch
5 # Makes a unique request id available to the action_dispatch.request_id env variable (which is then accessible through
6 # ActionDispatch::Request#uuid) and sends the same id to the client via the X-Request-Id header.
7 #
8 # The unique request id is either based off the X-Request-Id header in the request, which would typically be generated
9 # by a firewall, load balancer, or the web server, or, if this header is not available, a random uuid. If the
10 # header is accepted from the outside world, we sanitize it to a max of 255 chars and alphanumeric and dashes only.
11 #
12 # The unique request id can be used to trace a request end-to-end and would typically end up being part of log files
13 # from multiple pieces of the stack.
14 class RequestId
15 def initialize(app)
16 @app = app
17 end
18
19 def call(env)
20 env["action_dispatch.request_id"] = external_request_id(env) || internal_request_id
ceb66b61 » jeremy
2012-02-29 Tap, tap goes the middleware
21 @app.call(env).tap { |status, headers, body| headers["X-Request-Id"] = env["action_dispatch.request_id"] }
afde6fdd » dhh
2011-10-19 Added X-Request-Id tracking and TaggedLogging to easily log that and …
22 end
f1fecd9b » josevalim
2011-10-19 Make tests run on 1.8.x, add integration setup.
23
afde6fdd » dhh
2011-10-19 Added X-Request-Id tracking and TaggedLogging to easily log that and …
24 private
25 def external_request_id(env)
4ef74536 » josevalim
2011-10-19 Load object/blank and make use of presence.
26 if request_id = env["HTTP_X_REQUEST_ID"].presence
951a325c » marcbowes
2011-10-20 Remove the unneeded `\d` when sanitizing `X-Request-Id`.
27 request_id.gsub(/[^\w\-]/, "").first(255)
afde6fdd » dhh
2011-10-19 Added X-Request-Id tracking and TaggedLogging to easily log that and …
28 end
29 end
f1fecd9b » josevalim
2011-10-19 Make tests run on 1.8.x, add integration setup.
30
afde6fdd » dhh
2011-10-19 Added X-Request-Id tracking and TaggedLogging to easily log that and …
31 def internal_request_id
0f510e97 » arunagw
2011-12-21 Using uuid which is supported in 1.9
32 SecureRandom.uuid
afde6fdd » dhh
2011-10-19 Added X-Request-Id tracking and TaggedLogging to easily log that and …
33 end
34 end
35 end
Something went wrong with that request. Please try again.