lib/opentelemetry/instrumentation/active_job/handlers/default.rb in opentelemetry-instrumentation-active_job-0.7.1 vs lib/opentelemetry/instrumentation/active_job/handlers/default.rb in opentelemetry-instrumentation-active_job-0.7.2

- old
+ new

@@ -39,13 +39,13 @@ # @param id [String] of the event # @param payload [Hash] containing job run information # @return [Hash] with the span and generated context tokens def start_span(name, _id, payload) span = tracer.start_span(name, attributes: @mapper.call(payload)) - tokens = [OpenTelemetry::Context.attach(OpenTelemetry::Trace.context_with_span(span))] + token = OpenTelemetry::Context.attach(OpenTelemetry::Trace.context_with_span(span)) - { span: span, ctx_tokens: tokens } + { span: span, ctx_token: token } end # Creates a span and registers it with the current context # # @param _name [String] of the Event (unused) @@ -53,35 +53,34 @@ # @param payload [Hash] containing job run information # @return [Hash] with the span and generated context tokens def finish(_name, _id, payload) otel = payload.delete(:__otel) span = otel&.fetch(:span) - tokens = otel&.fetch(:ctx_tokens) + token = otel&.fetch(:ctx_token) on_exception((payload[:error] || payload[:exception_object]), span) rescue StandardError => e OpenTelemetry.handle_error(exception: e) ensure - finish_span(span, tokens) + finish_span(span, token) end # Finishes the provided spans and also detaches the associated contexts # # @param span [OpenTelemetry::Trace::Span] - # @param tokens [Array] to unregister - def finish_span(span, tokens) + # @param token [Numeric] to unregister + def finish_span(span, token) # closes the span after all attributes have been finalized begin if span&.recording? span.status = OpenTelemetry::Trace::Status.ok if span.status.code == OpenTelemetry::Trace::Status::UNSET span.finish end rescue StandardError => e OpenTelemetry.handle_error(exception: e) end - # pops the context stack - tokens&.reverse&.each do |token| + begin OpenTelemetry::Context.detach(token) rescue StandardError => e OpenTelemetry.handle_error(exception: e) end end