Class: HookDeck::Middleware::RequestId

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/hookdeck/middlewares/request_id.rb

Overview

Middleware that automatically generates and manages request IDs for HTTP requests. Adds both server and client request IDs to facilitate request tracking and debugging.

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ RequestId

Initialize the request ID middleware

Parameters:

  • The Faraday app

  • (defaults to: {})

    Configuration options

Options Hash (options):

  • :prefix (String) — default: 'req'

    Prefix for generated request IDs



13
14
15
16
# File 'lib/hookdeck/middlewares/request_id.rb', line 13

def initialize(app, options = {})
  super(app)
  @prefix = options.fetch(:prefix, 'req')
end

Instance Method Details

#call(env) ⇒ Object

Executes the middleware, adding and tracking request IDs



19
20
21
22
23
24
25
26
27
28
# File 'lib/hookdeck/middlewares/request_id.rb', line 19

def call(env)
  env.request_headers['X-Request-Id'] ||= generate_request_id
  env.request_headers['X-Client-Request-Id'] ||= generate_request_id

  @app.call(env).on_complete do |response_env|
    # Store request IDs for potential error handling
    response_env[:hookdeck_request_id] = response_env.response_headers['x-request-id']
    response_env[:hookdeck_client_request_id] = env.request_headers['X-Client-Request-Id']
  end
end