Class: Datadog::Statsd::MessageBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/statsd/message_buffer.rb

Constant Summary collapse

PAYLOAD_SIZE_TOLERANCE =
0.05

Instance Method Summary collapse

Constructor Details

#initialize(connection, max_payload_size: nil, max_pool_size: DEFAULT_BUFFER_POOL_SIZE, overflowing_stategy: :drop, serializer:) ⇒ MessageBuffer

Returns a new instance of MessageBuffer.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/datadog/statsd/message_buffer.rb', line 8

def initialize(connection,
  max_payload_size: nil,
  max_pool_size: DEFAULT_BUFFER_POOL_SIZE,
  overflowing_stategy: :drop,
  serializer:
)
  raise ArgumentError, 'max_payload_size keyword argument must be provided' unless max_payload_size
  raise ArgumentError, 'max_pool_size keyword argument must be provided' unless max_pool_size

  @connection = connection
  @max_payload_size = max_payload_size
  @max_pool_size = max_pool_size
  @overflowing_stategy = overflowing_stategy
  @serializer = serializer

  @buffer = String.new
  clear_buffer
end

Instance Method Details

#add(message) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/datadog/statsd/message_buffer.rb', line 27

def add(message)
  # Serializes the message if it hasn't been already. Part of the
  # delay_serialization feature.
  if message.is_a?(Array)
    message = @serializer.to_stat(*message[0], **message[1])
  end

  message_size = message.bytesize

  return nil unless message_size > 0 # to avoid adding empty messages to the buffer
  return nil unless ensure_sendable!(message_size)

  flush if should_flush?(message_size)

  buffer << "\n" unless buffer.empty?
  buffer << message

  @message_count += 1

  # flush when we're pretty sure that we won't be able
  # to add another message to the buffer
  flush if preemptive_flush?

  true
end

#flushObject



58
59
60
61
62
63
# File 'lib/datadog/statsd/message_buffer.rb', line 58

def flush
  return if buffer.empty?

  connection.write(buffer)
  clear_buffer
end

#resetObject



53
54
55
56
# File 'lib/datadog/statsd/message_buffer.rb', line 53

def reset
  clear_buffer
  connection.reset_telemetry
end