Class: Statsd::Batch
Overview
Some more unabashed borrowing: github.com/reinh/statsd/blob/master/lib/statsd.rb#L410 The big difference between this implementation and reinh’s is that we don’t support namespaces, and we have a bunch of hacks for introducing prefixes to the namespaces we’re acting against.
Batch: A batching statsd proxy
Batch is a subclass of Statsd, but with a constructor that proxies to a normal Statsd instance. It has it’s own batch_size parameters (that inherit defaults from the supplied Statsd instance). It is recommended that some care is taken if setting very large batch sizes. If the batch size exceeds the allowed packet size for UDP on your network, communication troubles may occur and data will be lost.
Instance Attribute Summary collapse
-
#batch_size ⇒ Object
Returns the value of attribute batch_size.
Attributes inherited from Client
#host, #port, #prefix, #resolve_always
Instance Method Summary collapse
-
#easy ⇒ Object
A convenience method to ensure that data is not lost in the event of an exception being thrown.
- #flush ⇒ Object
-
#initialize(statsd) ⇒ Batch
constructor
A new instance of Batch.
- #send_batch_data(message) ⇒ Object
- #send_method ⇒ Object
Methods inherited from Client
#batch, #decrement, #gauge, #host_ip_addr, #increment, #send_data, #timing, #update_counter
Constructor Details
#initialize(statsd) ⇒ Batch
Returns a new instance of Batch.
178 179 180 181 182 183 184 185 186 |
# File 'lib/statsd.rb', line 178 def initialize(statsd) @statsd = statsd @batch_size = statsd.batch_size @backlog = [] @send_data = send_method @host = statsd.host @port = statsd.port @prefix = statsd.prefix end |
Instance Attribute Details
#batch_size ⇒ Object
Returns the value of attribute batch_size.
175 176 177 |
# File 'lib/statsd.rb', line 175 def batch_size @batch_size end |
Instance Method Details
#easy ⇒ Object
A convenience method to ensure that data is not lost in the event of an exception being thrown. Batches will be transmitted on the parent socket as soon as the batch is full, and when the block finishes.
193 194 195 196 197 |
# File 'lib/statsd.rb', line 193 def easy yield self ensure flush end |
#flush ⇒ Object
199 200 201 202 203 204 |
# File 'lib/statsd.rb', line 199 def flush unless @backlog.empty? @statsd.send_data @backlog.join("\n") @backlog.clear end end |
#send_batch_data(message) ⇒ Object
206 207 208 209 210 211 |
# File 'lib/statsd.rb', line 206 def send_batch_data() @backlog << if @backlog.size >= @batch_size flush end end |
#send_method ⇒ Object
213 214 215 216 217 218 219 220 |
# File 'lib/statsd.rb', line 213 def send_method lambda { |data| @backlog << data if @backlog.size >= @batch_size flush end } end |