Class: Rage::RequestId

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

Overview

The middleware establishes a connection between the X-Request-Id header (typically generated by a firewall, load balancer, or web server) and Rage’s internal logging system. It ensures that:

  1. All logs produced during the request are tagged with the value submitted in the X-Request-Id header.

  2. The request ID is added back to the response in the X-Request-Id header. If no X-Request-Id header was provided in the request, the middleware adds an internally generated ID to the response.

Additionally, the X-Request-Id header value is sanitized to a maximum of 255 characters, allowing only alphanumeric characters and dashes.

Examples:

Rage.configure do
  config.middleware.use Rage::RequestId
end

Constant Summary collapse

BLACKLISTED_CHARACTERS =
/[^\w\-@]/

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RequestId

Returns a new instance of RequestId.



20
21
22
# File 'lib/rage/middleware/request_id.rb', line 20

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/rage/middleware/request_id.rb', line 24

def call(env)
  env["rage.request_id"] = validate_external_request_id(env["HTTP_X_REQUEST_ID"])
  response = @app.call(env)
  response[1]["X-Request-Id"] = env["rage.request_id"]

  response
end