class NewRelic::TelemetrySdk::Buffer

Buffers store discrete pieces of data (e.g. {Span Spans}) until they are sent via a timed {Harvester}. Batches of data may also be flushed from a buffer and sent directly through the client.

@api public

Attributes

common_attributes[RW]

@!attribute common_attributes [optional, Hash] Attributes that should be added to every item in the batch e.g. +{host: 'my_host'}+

Public Class Methods

new(common_attributes=nil) click to toggle source

Record a discrete piece of data (e.g. a Span) into the buffer for batching purposes. @param common_attributes [optional, Hash]

Attributes that should be added to every item in the batch
e.g. +{host: 'my_host'}+

@api public

# File lib/newrelic/telemetry_sdk/buffer.rb, line 29
def initialize common_attributes=nil
  @items = []
  @common_attributes = common_attributes
  @lock = Mutex.new
end

Public Instance Methods

flush() click to toggle source

Return a batch of data that has been collected in this buffer as an Array of Hashes. Also returns a Hash of any common attributes that have been set on the buffer to be attached to each individual data item.

@api public

# File lib/newrelic/telemetry_sdk/buffer.rb, line 52
def flush
  data = nil
  @lock.synchronize do
    data = @items
    @items = []
  end
  data.map!(&:to_h)
  return data, @common_attributes
rescue => e
  log_error "Encountered error while flushing buffer", e
end
record(item) click to toggle source

Record a discrete piece of data (e.g. a {Span}) into the {Buffer buffer}. @param item [Span, etc.]

A piece of data to record into the buffer. Must have a to_h method
for transformation.

@api public

# File lib/newrelic/telemetry_sdk/buffer.rb, line 41
def record item
  @lock.synchronize { @items << item }
rescue => e
  log_error "Encountered error while recording in buffer", e
end