Module: Algolia::Transport::RequestId

Defined in:
lib/algolia/transport/request_id.rb

Overview

Mints the Request-ID tracing header sent by the clients that support it.

Constant Summary collapse

HEADER =
"request-id".freeze
CORRELATION_HEADER =
"Correlation-ID".freeze
QUERY_PARAM =
"x-algolia-request-id".freeze

Class Method Summary collapse

Class Method Details

.generate ⇒ String

Returns a fresh 11-character base62 identifier suitable for the Request-ID header.

Returns:

  • (String)


15
16
17
# File 'lib/algolia/transport/request_id.rb', line 15

def self.generate
  SecureRandom.alphanumeric(11)
end

.request_id?(headers) ⇒ true, false

Whether the given headers already carry a Request-ID entry, whatever its casing and key type. Header hashes keep the caller's literal casing and may use symbol keys, so the lookup must not assume a canonical form.

Parameters:

  • headers (Hash, nil)

Returns:

  • (true, false)


27
28
29
30
31
# File 'lib/algolia/transport/request_id.rb', line 27

def self.request_id?(headers)
  return false unless headers.respond_to?(:each_pair)

  headers.any? { |k, _| k.to_s.casecmp?(HEADER) }
end

.request_id_query_param?(query_params) ⇒ true, false

Whether the given query parameters already carry an x-algolia-request-id entry, whatever its casing and key type. The server consults the query parameter only when the header is absent, so minting a header would override a caller-supplied value sent on that channel.

Parameters:

  • query_params (Hash, nil)

Returns:

  • (true, false)


42
43
44
45
46
# File 'lib/algolia/transport/request_id.rb', line 42

def self.request_id_query_param?(query_params)
  return false unless query_params.respond_to?(:each_pair)

  query_params.any? { |k, _| k.to_s.casecmp?(QUERY_PARAM) }
end