Class: Datadog::TraceBuffer

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

Overview

Trace buffer that stores application traces. The buffer has a maximum size and when the buffer is full, a random trace is discarded. This class is thread-safe and is used automatically by the Tracer instance when a Span is finished.

Instance Method Summary collapse

Constructor Details

#initialize(max_size) ⇒ TraceBuffer

Returns a new instance of TraceBuffer.



33
34
35
36
37
38
# File 'lib/ddtrace/buffer.rb', line 33

def initialize(max_size)
  @max_size = max_size

  @mutex = Mutex.new()
  @traces = []
end

Instance Method Details

#empty?Boolean

Return if the buffer is empty.

Returns:

  • (Boolean)


62
63
64
65
66
# File 'lib/ddtrace/buffer.rb', line 62

def empty?
  @mutex.synchronize do
    return @traces.empty?
  end
end

#lengthObject

Return the current number of stored traces.



55
56
57
58
59
# File 'lib/ddtrace/buffer.rb', line 55

def length
  @mutex.synchronize do
    return @traces.length
  end
end

#popObject

Stored traces are returned and the local buffer is reset



69
70
71
72
73
74
75
# File 'lib/ddtrace/buffer.rb', line 69

def pop
  @mutex.synchronize do
    traces = @traces
    @traces = []
    return traces
  end
end

#push(trace) ⇒ Object

Add a new trace in the local queue. This method doesn’t block the execution even if the buffer is full. In that case, a random trace is discarded.



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ddtrace/buffer.rb', line 42

def push(trace)
  @mutex.synchronize do
    len = @traces.length
    if len < @max_size || @max_size <= 0
      @traces << trace
    else
      # we should replace a random trace with the new one
      @traces[rand(len)] = trace
    end
  end
end