Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for action dispatch request id generation #115

Merged
merged 1 commit into from
Jan 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Unreleased
=====
- Adds support for ActionDispatch::RequestId generated request ids
- Changes uuid format to proper uuid

0.4.2
=====
- Ruby 2.0 compatible
Expand Down
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