Class: Rack::RequestId

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/request_id.rb

Overview

Public: Rack middleware that stores the X-Request-Id header in a thread local variable.

Heroku has a labs feature called request_id, which can be used to tracking a request through the system.

app - The Rack app.

Examples

use Rack::RequestId, key: :request_id, value: -> (env) { env['HTTP_X_REQUEST_ID'] }, response_header: 'X-Request-Id'

logger.info "request_id=#{RequestId.request_id} Hello world"
# => request_id=a08a6712229fb991c0e5026c246862c7 Hello world

Constant Summary collapse

REQUEST_HEADER =
'HTTP_X_REQUEST_ID'.freeze
RESPONSE_HEADER =
'X-Request-Id'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, options = nil) ⇒ RequestId

Returns a new instance of RequestId.



23
24
25
26
# File 'lib/rack/request_id.rb', line 23

def initialize(app, options = nil)
  @app = app
  @options = options || default_options
end

Instance Method Details

#call(env) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rack/request_id.rb', line 28

def call(env)
  ::RequestId.with(@options[:key], @options[:value].call(env) || generate) do
    status, headers, body = @app.call(env)

    if @options[:response_header]
      headers[@options[:response_header]] ||= ::RequestId.get(@options[:key]).to_s
    end

    [status, headers, body]
  end
end