# frozen_string_literal: true require "rack/request" require "rack/utils" require "securerandom" require "yog" class Yog class Rack def initialize(app, **ctx) @app = app @ctx = ctx.merge(svc: "rack") end def call(env) now = Time.now req = ::Rack::Request.new(env) rid = (env["HTTP_X_REQUEST_ID"] ||= SecureRandom.uuid) Yog.context **@ctx, rid: rid do begin res = @app.call(env) ensure duration = (Time.now - now) * 1000 status, headers, * = res headers["X-Request-ID"] ||= rid Yog ::Rack::Utils::HTTP_STATUS_CODES[status], at: "finish", method: req.request_method, path: req.fullpath, status: status, duration: duration end end end end end