class Kiev::Rack::RequestId

Constants

RAILS_REQUEST_ID

for Rails 4

Public Class Methods

new(app) click to toggle source
# File lib/kiev/rack/request_id.rb, line 11
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# File lib/kiev/rack/request_id.rb, line 15
def call(env)
  request_id_header_out = to_rack(:request_id)
  tracking_id_header_out = to_rack(:tracking_id)

  tracking_id = make_tracking_id(env[to_http(:tracking_id)] || env[RAILS_REQUEST_ID] || env[to_http(:request_id)])
  RequestStore.store[:tracking_id] = tracking_id
  RequestStore.store[:request_id] = tracking_id
  RequestStore.store[:request_depth] = request_depth(env)
  RequestStore.store[:tree_path] = tree_path(env)

  @app.call(env).tap do |_status, headers, _body|
    headers[tracking_id_header_out] = tracking_id
    headers[request_id_header_out] = tracking_id
  end
end

Private Instance Methods

internal_tracking_id() click to toggle source
# File lib/kiev/rack/request_id.rb, line 65
def internal_tracking_id
  SecureRandom.uuid
end
make_tracking_id(tracking_id) click to toggle source
# File lib/kiev/rack/request_id.rb, line 57
def make_tracking_id(tracking_id)
  if tracking_id.nil? || tracking_id.empty?
    internal_tracking_id
  else
    Util.sanitize(tracking_id)
  end
end
request_depth(env) click to toggle source
# File lib/kiev/rack/request_id.rb, line 39
def request_depth(env)
  request_depth_header = to_http(:request_depth)
  tree_root?(env) ? 0 : (env[request_depth_header].to_i + 1)
end
to_http(value) click to toggle source
# File lib/kiev/rack/request_id.rb, line 49
def to_http(value)
  Util.to_http(to_rack(value))
end
to_rack(value) click to toggle source
# File lib/kiev/rack/request_id.rb, line 53
def to_rack(value)
  Config.instance.all_http_propagated_fields[value]
end
tree_path(env) click to toggle source
# File lib/kiev/rack/request_id.rb, line 44
def tree_path(env)
  tree_path_header = to_http(:tree_path)
  tree_root?(env) ? SubrequestHelper.root_path(synchronous: true) : Util.sanitize(env[tree_path_header])
end
tree_root?(env) click to toggle source
# File lib/kiev/rack/request_id.rb, line 33
def tree_root?(env)
  tracking_id_header_in = to_http(:tracking_id)
  request_id_header_in = to_http(:request_id)
  !env[tracking_id_header_in] && !env[request_id_header_in]
end