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.



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

def close
  @closed = true
end

#closed?Boolean

Returns:

  • (Boolean)


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

def closed?
  @closed
end

#concat(items) ⇒ Object

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



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ddtrace/buffer.rb', line 27

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)


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

def empty?
  @items.empty?
end

#lengthObject

Return the current number of stored traces.



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

def length
  @items.length
end

#popObject

Stored items are returned and the local buffer is reset.



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

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. In that case, a random item is discarded.



18
19
20
21
22
23
# File 'lib/ddtrace/buffer.rb', line 18

def push(item)
  return if closed?

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