class Instana::Processor

Public Class Methods

new(logger: ::Instana.logger) click to toggle source
# File lib/instana/tracing/processor.rb, line 12
def initialize(logger: ::Instana.logger)
  # The main queue before being reported to the
  # host agent.  Spans in this queue are complete
  # and ready to be sent.
  @queue = Queue.new

  # This is the maximum number of spans we send to the host
  # agent at once.
  @batch_size = 3000
  @logger = logger
  @pid = Process.pid

  @spans_opened = Concurrent::AtomicFixnum.new(0)
  @spans_closed = Concurrent::AtomicFixnum.new(0)
end

Public Instance Methods

add_span(span) click to toggle source

Adds a span to the span queue

@param [Trace] - the trace to be added to the queue

# File lib/instana/tracing/processor.rb, line 31
def add_span(span)
  # :nocov:
  if @pid != Process.pid
    @logger.info("Proces `#{@pid}` has forked into #{Process.pid}. Running post fork hook.")
    ::Instana.config[:post_fork_proc].call
    @pid = Process.pid
  end
  # :nocov:

  @spans_closed.increment
  @queue.push(span)
end
clear!() click to toggle source

Removes all traces from the @queue. Used in the test suite to reset state.

# File lib/instana/tracing/processor.rb, line 109
def clear!
  @spans_opened.value = 0
  @spans_closed.value = 0

  until @queue.empty? do
    # Non-blocking pop; ignore exception
    @queue.pop(true) rescue nil
  end
end
queued_spans() click to toggle source

Retrieves all of the traces in @queue and returns the sum of their raw spans. This is used by Processor::send and in the test suite. Note that traces retrieved with this method are removed entirely from the queue.

@return [Array] An array of [Span] or empty

# File lib/instana/tracing/processor.rb, line 93
def queued_spans
  return [] if @queue.empty?

  spans = []
  until @queue.empty? do
    # Non-blocking pop; ignore exception
    span = @queue.pop(true) rescue nil
    spans << span.raw if span.is_a?(Span) && span.context.level == 1
  end

  spans
end
send(&block) click to toggle source

send

Sends all traces in @queue to the host agent

FIXME: Add limits checking here in regards to:

- Max HTTP Post size
- Out of control/growing queue
- Prevent another run of the timer while this is running
# File lib/instana/tracing/processor.rb, line 75
def send(&block)
  return if @queue.empty? || ENV.key?('INSTANA_TEST')

  # Retrieve all spans for queued traces
  spans = queued_spans

  # Report spans in batches
  spans.each_slice(@batch_size, &block)
end
span_metrics() click to toggle source

Clears and retrieves metrics associated with span creation and submission

# File lib/instana/tracing/processor.rb, line 51
def span_metrics
  response = {
    opened: @spans_opened.value,
    closed: @spans_closed.value,
    filtered: 0,
    dropped: 0
  }

  @spans_opened.value = 0
  @spans_closed.value = 0

  response
end
start_span(_) click to toggle source

Note that we've started a new span. Used to generate monitoring metrics.

# File lib/instana/tracing/processor.rb, line 46
def start_span(_)
  @spans_opened.increment
end