class Datadog::ContextFlush::Partial

Performs partial trace flushing to avoid large traces residing in memory for too long

Constants

DEFAULT_MIN_SPANS_FOR_PARTIAL_FLUSH

Start flushing partial trace after this many active spans in one trace

Public Class Methods

new(options = {}) click to toggle source
# File lib/ddtrace/context_flush.rb, line 29
def initialize(options = {})
  @min_spans_for_partial = options.fetch(:min_spans_before_partial_flush, DEFAULT_MIN_SPANS_FOR_PARTIAL_FLUSH)
end

Public Instance Methods

consume!(context) click to toggle source

Consumes and returns completed or partially completed traces from the provided context, if any.

Partially completed traces, where not all spans have finished, will only be returned if there are at least +@min_spans_for_partial+ finished spans.

Any spans consumed are removed from context as a side effect.

@return [Array<Span>] partial or complete trace to be flushed, or nil if no spans are finished

# File lib/ddtrace/context_flush.rb, line 43
def consume!(context)
  trace, sampled = get_trace(context)

  return nil unless sampled
  return trace if trace && !trace.empty?

  partial_trace(context)
end

Protected Instance Methods

get_trace(context) click to toggle source
# File lib/ddtrace/context_flush.rb, line 54
def get_trace(context)
  context.get
end

Private Instance Methods

finished_spans(context) click to toggle source
# File lib/ddtrace/context_flush.rb, line 66
def finished_spans(context)
  trace = context.delete_span_if(&:finished?)

  # Ensure that the first span in a partial trace has
  # sampling and origin information.
  if trace[0]
    context.annotate_for_flush!(trace[0])
  else
    Datadog.logger.debug('Tried to retrieve trace from context, but got nothing. ' \
      "Is there another consumer for this context? #{context.trace_id}")
  end

  trace unless trace.empty?
end
partial_trace(context) click to toggle source
# File lib/ddtrace/context_flush.rb, line 60
def partial_trace(context)
  return nil if context.finished_span_count < @min_spans_for_partial

  finished_spans(context)
end