Class: Stipa::Middleware::RequestId

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

Overview

Propagates an upstream X-Request-Id header or mints a new one. Runs before the router so req.id is always set when handlers execute.

With a load balancer / API gateway that injects X-Request-Id, distributed traces stay correlated across services automatically.

Instance Method Summary collapse

Constructor Details

#initialize(next_app, header: 'X-Request-Id') ⇒ RequestId

Returns a new instance of RequestId.



64
65
66
67
# File 'lib/stipa/middleware.rb', line 64

def initialize(next_app, header: 'X-Request-Id')
  @next_app = next_app
  @header   = header
end

Instance Method Details

#call(req, res) ⇒ Object



69
70
71
72
73
74
# File 'lib/stipa/middleware.rb', line 69

def call(req, res)
  # Header lookup is lowercase in Request; header name for response is as-is
  req.id = req[@header.downcase] || SecureRandom.hex(8)
  res.set_header(@header, req.id)
  @next_app.call(req, res)
end