Class: Datadog::ContextFlush

Inherits:
Object
  • Object
show all
Defined in:
lib/ddtrace/context_flush.rb

Overview

ContextFlush is used to cap context size and avoid it using too much memory. It performs memory flushes when required.

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ContextFlush

Returns a new instance of ContextFlush.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ddtrace/context_flush.rb', line 20

def initialize(options = {})
  # max_spans_before_partial_flush is the amount of spans collected before
  # the context starts to partially flush parts of traces. With a setting of 10k,
  # the memory overhead is about 10Mb per thread/context (depends on spans metadata,
  # this is just an order of magnitude).
  @max_spans_before_partial_flush = options.fetch(:max_spans_before_partial_flush,
                                                  DEFAULT_MAX_SPANS_BEFORE_PARTIAL_FLUSH)
  # min_spans_before_partial_flush is the minimum number of spans required
  # for a partial flush to happen on a timeout. This is to prevent partial flush
  # of traces which last a very long time but yet have few spans.
  @min_spans_before_partial_flush = options.fetch(:min_spans_before_partial_flush,
                                                  DEFAULT_MIN_SPANS_BEFORE_PARTIAL_FLUSH)
  # partial_flush_timeout is the limit (in seconds) above which the context
  # considers flushing parts of the trace. Partial flushes should not be done too
  # late else the agent rejects them with a "too far in the past" error.
  @partial_flush_timeout = options.fetch(:partial_flush_timeout,
                                         DEFAULT_PARTIAL_FLUSH_TIMEOUT)
  @partial_traces = []
end

Instance Method Details

#each_partial_trace(context) ⇒ Object

Performs an operation which each partial trace it can get from the context.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/ddtrace/context_flush.rb', line 110

def each_partial_trace(context)
  start_time = context.send(:start_time)
  length = context.send(:length)
  # Stop and do not flush anything if there are not enough spans.
  return if length <= @min_spans_before_partial_flush
  # If there are enough spans, but not too many, check for start time.
  # If timeout is not given or 0, then wait
  return if length <= @max_spans_before_partial_flush &&
            (@partial_flush_timeout.nil? || @partial_flush_timeout <= 0 ||
             (start_time && start_time > Time.now.utc - @partial_flush_timeout))
  # Here, either the trace is old or we have too many spans, flush it.
  traces = partial_flush(context)
  return unless traces
  traces.each do |trace|
    yield trace
  end
end