Rack middleware which adds a random 16-digit hexidecimal value into an
X-Request-Id
response header and also into
Thread.current[:request_id]
. If you are using a multi-threaded
webserver like puma, see instructions below on how to
safely store the value globally.
Add the following to your Gemfile
.
gem 'rack-request-id'
class MyApp < Sinatra::Base
use Rack::RequestId
end
module MyApp
class Application < Rails::Application
# ...
config.middleware.use "Rack::RequestId"
end
end
Optionally you can override the ID generation proc
. By default it's SecureRandom.hex(16)
.
class MyApp < Sinatra::Base
use Rack::RequestId, id_generator: proc { SecureRandom.random_number(100) }
end
Optionally you can also change storage. Storage objects must have #[]
and #[]=
method.
require 'request_store'
class MyApp < Sinatra::Base
use Rack::RequestId, storage: RequestStore
end
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request