Class: Spokes::Middleware::RequestID

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

Constant Summary collapse

PATTERN =
/^[\w\\-_\\.\d]+$/

Instance Method Summary collapse

Constructor Details

#initialize(app, service_name:) ⇒ RequestID

Returns a new instance of RequestID.



6
7
8
9
10
11
# File 'lib/spokes/middleware/request_id.rb', line 6

def initialize(app, service_name:)
  raise "invalid name: #{service_name}" unless service_name =~ PATTERN

  @app          = app
  @service_name = service_name
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/spokes/middleware/request_id.rb', line 13

def call(env)
  id = env['action_dispatch.request_id'] || SecureRandom.uuid
  request_ids = extract_request_ids(env).insert(0, @service_name + ':' + id)

  # make ID of the request accessible to consumers down the stack
  env['REQUEST_ID'] = request_ids[0]

  # Extract request IDs from incoming headers as well. Can be used for
  # identifying a request across a number of components in SOA.
  env['REQUEST_IDS'] = request_ids

  Thread.current[:request_chain] = env['REQUEST_IDS']

  @app.call(env)
end