Class: Datadog::Buffer

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

Overview

Buffer that stores objects. The buffer has a maximum size and when the buffer is full, a random object is discarded.

Direct Known Subclasses

CRubyBuffer, ThreadSafeBuffer

Instance Method Summary collapse

Constructor Details

#initialize(max_size) ⇒ Buffer

Returns a new instance of Buffer.



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

def initialize(max_size)
  @max_size = max_size
  @items = []
  @closed = false
end

Instance Method Details

#closeObject

Closes this buffer, preventing further pushing. Draining is still allowed.



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

def close
  @closed = true
end

#closed?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/ddtrace/buffer.rb', line 65

def closed?
  @closed
end

#concat(items) ⇒ Object

A bulk push alternative to +#push+. Use this method if pushing more than one item for efficiency.



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ddtrace/buffer.rb', line 31

def concat(items)
  return if closed?

  # Segment items into underflow and overflow
  underflow, overflow = overflow_segments(items)

  # Concatenate items do not exceed capacity.
  add_all!(underflow) unless underflow.nil?

  # Iteratively replace items, to ensure pseudo-random replacement.
  overflow.each { |item| replace!(item) } unless overflow.nil?
end

#empty?Boolean

Return if the buffer is empty.

Returns:

  • (Boolean)


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

def empty?
  @items.empty?
end

#lengthObject

Return the current number of stored traces.



50
51
52
# File 'lib/ddtrace/buffer.rb', line 50

def length
  @items.length
end

#popObject

Stored items are returned and the local buffer is reset.



45
46
47
# File 'lib/ddtrace/buffer.rb', line 45

def pop
  drain!
end

#push(item) ⇒ Object

Add a new item in the local queue. This method doesn't block the execution even if the buffer is full.

When the buffer is full, we try to ensure that we are fairly sampling newly pushed traces by randomly inserting them into the buffer slots. This discards old traces randomly while trying to ensure that recent traces are still captured.



22
23
24
25
26
27
# File 'lib/ddtrace/buffer.rb', line 22

def push(item)
  return if closed?

  full? ? replace!(item) : add!(item)
  item
end