Sha256: 3f4016174e19332a4c462129f4e69f1d02f3f8d6dc5cb41eb83352f05a0a8497

Contents?: true

Size: 1.6 KB

Versions: 2

Compression:

Stored size: 1.6 KB

Contents

module SpanManager
  # The [SpanManager::ManagedSpan] inherits all of the OpenTracing funcionality and layers on
  # in-process propagation capabilities.
  class ManagedSpan < OpenTracing::Span
    extend Forwardable

    def_delegators :@span, :context, :operation_name=, :set_tag, :set_baggage_item, :get_baggage_item, :log, :finish

    # Initializes a new active span. It's ManagedSpanSource responsibility to activate the span.
    #
    # @param span [OpenTracing::Span] the span to wrap
    # @param deactivate [Proc] the current span deactivation supplier. The block must return instance of [SpanManager::ManagedSpan]
    def initialize(span, deactivate)
      @span = span
      @deactivate = deactivate.respond_to?(:call) ? deactivate : nil
      @active = true
    end

    # @return [OpenTracing::Span] the wrapped span
    def wrapped
      @span
    end

    # @return [Boolean] whether the span is active or not
    def active?
      @active
    end

    # Mark the end of active period for the current span in this asynchronus executor and/or thread.
    # It's safe to call the method multiple times.
    def deactivate
      if @active && @deactivate
        deactivated_span = @deactivate.call
        warn "ActiveSpan::SpanSource inconsistency found during deactivation" unless deactivated_span == self
        @active = false
      end
    end

    # Finishes the current span, and if not yet deactivated marks the end of the active period for the span.
    # @param end_time [Time] custom end time, if not now
    def finish(end_time: Time.now)
      deactivate
      @span.finish(end_time: end_time)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
spanmanager-0.3.0 lib/spanmanager/managed_span.rb
spanmanager-0.2.0 lib/spanmanager/managed_span.rb