Class: Dinie::Internal::Middleware::Logging

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/dinie/runtime/logger.rb

Overview

‘Logging` — the Faraday response middleware that makes logging the natural hook point (architecture §9, RB12), filling the gap OpenAI Ruby leaves (no logger at all). Client mounts it on the ONE shared connection, BEFORE the adapter, so it observes the FINAL request/response of EVERY call — including the TokenManager’s ‘POST /auth/token`, whose `Authorization: Basic` header (the client secret) is redacted here automatically.

── Retry correlation across a Faraday-level middleware ──The retry loop lives ABOVE this middleware (in HttpClient), so each attempt is a separate round-trip through ‘#call`. To still stitch a request and its retries together in an APM, the first attempt (no `X-Dinie-Retry-Count`) mints an origin `request_log_id` and stashes it on the current thread; each retry — run synchronously in that SAME thread by the retry loop —reports `retry_of: <origin>`. This is the Ruby manifestation of the TS logger’s ‘requestLogID`/`retryOf`, adapted to the middleware hook point.

Constant Summary collapse

ORIGIN_LOG_ID_KEY =

Thread-local key holding the origin request’s log id for the current logical request.

:__dinie_origin_request_log_id
RETRY_COUNT_HEADER =

Request header HttpClient sets on retried attempts (‘“1”`, `“2”`, …); absent on the first.

"x-dinie-retry-count"
REQUEST_ID_HEADER =

Response header carrying the server-side request id (architecture §7 — ‘err.request_id`).

"x-request-id"

Instance Method Summary collapse

Constructor Details

#initialize(app, level: nil, logger: nil) ⇒ Logging

Returns a new instance of Logging.

Parameters:

  • app (#call)

    the next middleware/adapter in the stack

  • level (Symbol, String, nil) (defaults to: nil)

    log level (forwarded to RuntimeLogger)

  • logger (Object, nil) (defaults to: nil)

    custom sink (forwarded to RuntimeLogger)



273
274
275
276
# File 'lib/dinie/runtime/logger.rb', line 273

def initialize(app, level: nil, logger: nil)
  super(app)
  @logger = RuntimeLogger.new(level: level, logger: logger)
end

Instance Method Details

#call(request_env) ⇒ Faraday::Response

Parameters:

  • request_env (Faraday::Env)

Returns:

  • (Faraday::Response)


280
281
282
283
284
285
286
287
# File 'lib/dinie/runtime/logger.rb', line 280

def call(request_env)
  correlation = correlate(request_env.request_headers)
  log_request(request_env, correlation)
  started = monotonic_now
  @app.call(request_env).on_complete do |response_env|
    log_response(response_env, correlation, started)
  end
end