Skip to content

Commit

Permalink
add support for action dispatch request id generation
Browse files Browse the repository at this point in the history
  • Loading branch information
eprothro committed Jan 6, 2017
1 parent 940efda commit 15ec188
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
6 changes: 4 additions & 2 deletions lib/rack/timeout/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ def ms(k) # helper method used for formatting values in milliseconds
:timed_out, # This request has run for too long and we're raising a timeout error in it
:completed, # We're done with this request (also set after having timed out a request)
]
ENV_INFO_KEY = "rack-timeout.info" # key under which each request's RequestDetails instance is stored in its env.
ENV_INFO_KEY = "rack-timeout.info".freeze # key under which each request's RequestDetails instance is stored in its env.
HTTP_X_REQUEST_ID = "HTTP_X_REQUEST_ID".freeze # key where request id is stored if generated by upstream client/proxy
ACTION_DISPATCH_REQUEST_ID = "action_dispatch.request_id".freeze # key where request id is stored if generated by action dispatch

# helper methods to read timeout properties. Ensure they're always positive numbers or false. When set to false (or 0), their behaviour is disabled.
def read_timeout_property value, default
Expand Down Expand Up @@ -75,7 +77,7 @@ def initialize(app, service_timeout:nil, wait_timeout:nil, wait_overtime:nil, se
RT = self # shorthand reference
def call(env)
info = (env[ENV_INFO_KEY] ||= RequestDetails.new)
info.id ||= env["HTTP_X_REQUEST_ID"] || SecureRandom.hex
info.id ||= env[HTTP_X_REQUEST_ID] || env[ACTION_DISPATCH_REQUEST_ID] || SecureRandom.uuid

time_started_service = Time.now # The wall time the request started being processed by rack
ts_started_service = fsecs # The monotonic time the request started being processed by rack
Expand Down
9 changes: 7 additions & 2 deletions lib/rack/timeout/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
class Rack::Timeout::Railtie < Rails::Railtie
initializer("rack-timeout.prepend") do |app|
next if Rails.env.test?
app.config.middleware.insert_before Rack::Runtime, Rack::Timeout

if defined?(ActionDispatch::RequestId)
app.config.middleware.insert_after(ActionDispatch::RequestId, Rack::Timeout)
else
app.config.middleware.insert_before(Rack::Runtime, Rack::Timeout)
end
end
end
end

0 comments on commit 15ec188

Please sign in to comment.