Class: Datadog::Statsd::Batch

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

Instance Method Summary collapse

Constructor Details

#initialize(connection, max_buffer_bytes) ⇒ Batch

Returns a new instance of Batch.



103
104
105
106
107
108
# File 'lib/datadog/statsd.rb', line 103

def initialize(connection, max_buffer_bytes)
  @connection = connection
  @max_buffer_bytes = max_buffer_bytes
  @depth = 0
  reset
end

Instance Method Details

#add(message) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/datadog/statsd.rb', line 122

def add(message)
  message_bytes = message.bytesize

  unless @buffer_bytes == 0
    if @buffer_bytes + 1 + message_bytes >= @max_buffer_bytes
      flush
    else
      @buffer << NEW_LINE
      @buffer_bytes += 1
    end
  end

  @buffer << message
  @buffer_bytes += message_bytes
end

#flushObject



138
139
140
141
142
# File 'lib/datadog/statsd.rb', line 138

def flush
  return if @buffer_bytes == 0
  @connection.write @buffer
  reset
end

#openObject



110
111
112
113
114
115
116
# File 'lib/datadog/statsd.rb', line 110

def open
  @depth += 1
  yield
ensure
  @depth -= 1
  flush if !open?
end

#open?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/datadog/statsd.rb', line 118

def open?
  @depth > 0
end