Class: Logsy::RackMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/logsy/rack_middleware.rb

Overview

Rack middleware that captures the request id into Logsy at the earliest useful point in the stack — before Rails::Rack::Logger emits “Started GET …” and before ActionController’s start_processing event fires — so every log line of the request carries it, not just the ones emitted after controller callbacks run.

The Railtie inserts this automatically right after ActionDispatch::RequestId (which populates ‘action_dispatch.request_id` from X-Request-Id or generates a UUID). Outside Rails, falls back to the X-Request-Id header, then to a generated UUID, so the tag is always present.

Tag cleanup between requests is handled by the Rails executor (ActionDispatch::Executor wraps this middleware and resets CurrentAttributes); plain Rack apps should call Logsy.reset themselves.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RackMiddleware

Returns a new instance of RackMiddleware.



22
23
24
# File 'lib/logsy/rack_middleware.rb', line 22

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/logsy/rack_middleware.rb', line 26

def call(env)
  Logsy[:request_id] = env['action_dispatch.request_id'] ||
                       env['HTTP_X_REQUEST_ID'] ||
                       SecureRandom.uuid

  @app.call(env)
end