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.



114
115
116
117
118
119
# File 'lib/datadog/statsd.rb', line 114

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

Instance Method Details

#add(message) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/datadog/statsd.rb', line 133

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



149
150
151
152
153
# File 'lib/datadog/statsd.rb', line 149

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

#openObject



121
122
123
124
125
126
127
# File 'lib/datadog/statsd.rb', line 121

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

#open?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/datadog/statsd.rb', line 129

def open?
  @depth > 0
end