lib/skylight/middleware.rb in skylight-5.1.1 vs lib/skylight/middleware.rb in skylight-5.2.0.beta

- old
+ new

@@ -1,10 +1,12 @@ require "securerandom" module Skylight # @api private class Middleware + SKYLIGHT_REQUEST_ID = "skylight.request_id".freeze + class BodyProxy def initialize(body, &block) @body = body @block = block @closed = false @@ -79,11 +81,12 @@ def call(env) set_request_id(env) if Skylight.tracing? - error "Already instrumenting. Make sure the Skylight Rack Middleware hasn't been added more than once." + debug "Already instrumenting. Make sure the Skylight Rack Middleware hasn't been added more than once." + return @app.call(env) end if env["REQUEST_METHOD"] == "HEAD" t { "middleware skipping HEAD" } @app.call(env) @@ -106,11 +109,11 @@ private def log_context # Don't cache this, it will change - { request_id: @current_request_id, inst: Skylight.instrumenter&.uuid } + { request_id: current_request_id, inst: Skylight.instrumenter&.uuid } end # Allow for overwriting def endpoint_name(_env) "Rack" @@ -120,18 +123,28 @@ { source_location: Trace::SYNTHETIC } end # Request ID code based on ActionDispatch::RequestId def set_request_id(env) + return if env[SKYLIGHT_REQUEST_ID] + existing_request_id = env["action_dispatch.request_id"] || env["HTTP_X_REQUEST_ID"] - @current_request_id = env["skylight.request_id"] = make_request_id(existing_request_id) + self.current_request_id = env[SKYLIGHT_REQUEST_ID] = make_request_id(existing_request_id) end def make_request_id(request_id) request_id && !request_id.empty? ? request_id.gsub(/[^\w\-]/, "".freeze)[0...255] : internal_request_id end def internal_request_id SecureRandom.uuid + end + + def current_request_id + Thread.current[SKYLIGHT_REQUEST_ID] + end + + def current_request_id=(request_id) + Thread.current[SKYLIGHT_REQUEST_ID] = request_id end end end