Class: Securial::Middleware::RequestTagLogger

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

Overview

Rack middleware that adds request context tags to log messages.

This middleware intercepts requests and wraps the application call with tagged logging, ensuring all log messages generated during request processing include relevant request metadata for better traceability and debugging.

Instance Method Summary collapse

Constructor Details

#initialize(app, logger = Securial.logger) ⇒ RequestTagLogger

Initializes the middleware with the Rack application and logger.

Examples:

middleware = RequestTagLogger.new(app, Rails.logger)


39
40
41
42
# File 'lib/securial/middleware/request_tag_logger.rb', line 39

def initialize(app, logger = Securial.logger)
  @app = app
  @logger = logger
end

Instance Method Details

#call(env) ⇒ Array

Processes the request with tagged logging context.

Extracts request metadata from the Rack environment and applies them as tags to all log messages generated during the request processing. Tags are automatically removed after the request completes.

Examples:

Request processing with tags

# All log messages during this request will include:
# - Request ID (if available)
# - IP address (if available)
# - User agent (if available)
response = middleware.call(env)


60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/securial/middleware/request_tag_logger.rb', line 60

def call(env)
  request_id = request_id_from_env(env)
  ip_address = ip_from_env(env)
  user_agent = user_agent_from_env(env)

  tags = []
  tags << request_id if request_id
  tags << "IP:#{ip_address}" if ip_address
  tags << "UA:#{user_agent}" if user_agent

  @logger.tagged(*tags) do
    @app.call(env)
  end
end

#ip_from_env(env) ⇒ String? (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Extracts the client IP address from the Rack environment.

Prioritizes ActionDispatch’s processed remote IP (which handles proxy headers) over the raw REMOTE_ADDR to ensure accurate client identification behind load balancers and proxies.



101
102
103
# File 'lib/securial/middleware/request_tag_logger.rb', line 101

def ip_from_env(env)
  env["action_dispatch.remote_ip"]&.to_s || env["REMOTE_ADDR"]
end

#request_id_from_env(env) ⇒ String? (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Extracts the request ID from the Rack environment.

Looks for request ID in ActionDispatch’s request_id or the X-Request-ID header, providing request traceability across multiple services and log aggregation systems.



87
88
89
# File 'lib/securial/middleware/request_tag_logger.rb', line 87

def request_id_from_env(env)
  env["action_dispatch.request_id"] || env["HTTP_X_REQUEST_ID"]
end

#user_agent_from_env(env) ⇒ String? (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Extracts the user agent string from the Rack environment.

Retrieves the HTTP User-Agent header to provide context about the client application or browser making the request.



114
115
116
# File 'lib/securial/middleware/request_tag_logger.rb', line 114

def user_agent_from_env(env)
  env["HTTP_USER_AGENT"]
end