diff --git a/instrumentation/rack/lib/opentelemetry/instrumentation/rack/middlewares/event_handler.rb b/instrumentation/rack/lib/opentelemetry/instrumentation/rack/middlewares/event_handler.rb index 353b3dd6a..8bb502a4a 100644 --- a/instrumentation/rack/lib/opentelemetry/instrumentation/rack/middlewares/event_handler.rb +++ b/instrumentation/rack/lib/opentelemetry/instrumentation/rack/middlewares/event_handler.rb @@ -42,7 +42,7 @@ module Middlewares class EventHandler include ::Rack::Events::Abstract - RACK_OTEL_CONTEXT_KEY = 'otel.rack.context.token' + OTEL_TOKEN_AND_SPAN = 'otel.rack.token_and_span' GOOD_HTTP_STATUSES = (100..499) # Creates a server span for this current request using the incoming parent context @@ -58,7 +58,7 @@ def on_start(request, _) span = create_span(parent_context, request) span_ctx = OpenTelemetry::Trace.context_with_span(span, parent_context: parent_context) rack_ctx = OpenTelemetry::Instrumentation::Rack.context_with_span(span, parent_context: span_ctx) - request.env[RACK_OTEL_CONTEXT_KEY] = [OpenTelemetry::Context.attach(rack_ctx), rack_ctx] + request.env[OTEL_TOKEN_AND_SPAN] = [OpenTelemetry::Context.attach(rack_ctx), span] rescue StandardError => e OpenTelemetry.handle_error(exception: e) end @@ -194,10 +194,10 @@ def request_span_attributes(env) end def detach_context(request) - return nil unless request.env[RACK_OTEL_CONTEXT_KEY] + return nil unless request.env[OTEL_TOKEN_AND_SPAN] - token, rack_ctx = request.env[RACK_OTEL_CONTEXT_KEY] - OpenTelemetry::Trace.current_span(rack_ctx).finish + token, span = request.env[OTEL_TOKEN_AND_SPAN] + span.finish OpenTelemetry::Context.detach(token) rescue StandardError => e OpenTelemetry.handle_error(exception: e)