Class: ForestAdminAgent::Http::CorrelationIdMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/forest_admin_agent/http/correlation_id_middleware.rb

Overview

Rack middleware echoing the agent-generated correlation id back to the client, mirroring the Node agent's correlationIdMiddleware (router.use(...)). Hosts mount it in their middleware stack; CORS exposure of the header is handled by the host's CORS config (see the Rails engine).

The id itself is generated lazily by the agent during the request (see CorrelationId, called from CallerParser). This middleware only resets the thread-local around the request — so a pooled thread never reuses a previous id — and sets the response header when one was generated.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ CorrelationIdMiddleware

Returns a new instance of CorrelationIdMiddleware.



11
12
13
# File 'lib/forest_admin_agent/http/correlation_id_middleware.rb', line 11

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/forest_admin_agent/http/correlation_id_middleware.rb', line 15

def call(env)
  CorrelationId.reset!

  status, headers, body = @app.call(env)

  id = CorrelationId.current?
  headers[CorrelationId::HEADER] = id if id

  [status, headers, body]
ensure
  CorrelationId.reset!
end