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.



8
9
10
11
12
13
# File 'lib/ddtrace/buffer.rb', line 8

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)


37
38
39
40
41
# File 'lib/ddtrace/buffer.rb', line 37

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

#lengthObject

Return the current number of stored traces.



30
31
32
33
34
# File 'lib/ddtrace/buffer.rb', line 30

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

#popObject

Stored traces are returned and the local buffer is reset.



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

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.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ddtrace/buffer.rb', line 17

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