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
14
# File 'lib/ddtrace/buffer.rb', line 8

def initialize(max_size)
  @max_size = max_size

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

Instance Method Details

#closeObject



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

def close
  @mutex.synchronize do
    @closed = true
  end
end

#closed?Boolean

Returns:

  • (Boolean)


60
61
62
63
64
# File 'lib/ddtrace/buffer.rb', line 60

def closed?
  @mutex.synchronise do
    return @closed
  end
end

#empty?Boolean

Return if the buffer is empty.

Returns:

  • (Boolean)


39
40
41
42
43
# File 'lib/ddtrace/buffer.rb', line 39

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

#lengthObject

Return the current number of stored traces.



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

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

#popObject

Stored traces are returned and the local buffer is reset.



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

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.



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

def push(trace)
  return if @closed
  @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