Class: LogStruct::Integrations::RequestContext::Middleware

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/log_struct/integrations/request_context/middleware.rb

Overview

Middleware that captures request_id and stores it in SemanticLogger's named_tags so all logs during the request include the request_id.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ void

Parameters:

  • app (T.untyped)


13
14
15
# File 'lib/log_struct/integrations/request_context/middleware.rb', line 13

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ T.untyped

Parameters:

  • env (T.untyped)

Returns:

  • (T.untyped)


18
19
20
21
22
23
24
25
26
27
# File 'lib/log_struct/integrations/request_context/middleware.rb', line 18

def call(env)
  request = ::ActionDispatch::Request.new(env)
  request_id = request.request_id
  Thread.current[:logstruct_request_id] = request_id
  ::SemanticLogger.push_named_tags(request_id: request_id)
  @app.call(env)
ensure
  ::SemanticLogger.pop_named_tags
  Thread.current[:logstruct_request_id] = nil
end