Class: NewRelic::TelemetrySdk::Buffer

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/newrelic/telemetry_sdk/buffer.rb

Overview

Buffers store discrete pieces of data (e.g. 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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

#clear_already_logged, #log_error, #log_once, logger, #logger, logger=, #logger=

Constructor Details

#initialize(common_attributes = nil) ⇒ Buffer

Record a discrete piece of data (e.g. a Span) into the buffer for batching purposes.

Parameters:

  • common_attributes (optional, Hash) (defaults to: nil)

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



29
30
31
32
33
# File 'lib/newrelic/telemetry_sdk/buffer.rb', line 29

def initialize common_attributes=nil
  @items = []
  @common_attributes = common_attributes
  @lock = Mutex.new
end

Instance Attribute Details

#common_attributesObject

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



20
21
22
# File 'lib/newrelic/telemetry_sdk/buffer.rb', line 20

def common_attributes
  @common_attributes
end

Instance Method Details

#flushObject

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.



52
53
54
55
56
57
58
59
60
61
62
# 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) ⇒ Object

Record a discrete piece of data (e.g. a Span) into the buffer.

Parameters:

  • item (Span, etc.)

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



41
42
43
44
45
# 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