Class: Datadog::Statsd::MessageBuffer
- Inherits:
-
Object
- Object
- Datadog::Statsd::MessageBuffer
- Defined in:
- lib/datadog/statsd/message_buffer.rb
Constant Summary collapse
- PAYLOAD_SIZE_TOLERANCE =
0.05
Instance Method Summary collapse
- #add(message) ⇒ Object
- #flush ⇒ Object
-
#initialize(connection, max_payload_size: nil, max_pool_size: DEFAULT_BUFFER_POOL_SIZE, overflowing_stategy: :drop, serializer:) ⇒ MessageBuffer
constructor
A new instance of MessageBuffer.
- #reset ⇒ Object
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.
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 52 |
# File 'lib/datadog/statsd/message_buffer.rb', line 27 def add() # Serializes the message if it hasn't been already. Part of the # delay_serialization feature. if .is_a?(Array) stat, delta, type, , sample_rate = = @serializer.to_stat(stat, delta, type, tags: , sample_rate: sample_rate) end = .bytesize return nil unless > 0 # to avoid adding empty messages to the buffer return nil unless ensure_sendable!() flush if should_flush?() buffer << "\n" unless buffer.empty? buffer << @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 |
#flush ⇒ Object
59 60 61 62 63 64 |
# File 'lib/datadog/statsd/message_buffer.rb', line 59 def flush return if buffer.empty? connection.write(buffer) clear_buffer end |
#reset ⇒ Object
54 55 56 57 |
# File 'lib/datadog/statsd/message_buffer.rb', line 54 def reset clear_buffer connection.reset_telemetry end |