class Trailer::Recorder

Attributes

storage[RW]
tags[RW]
trace_id[RW]

Public Class Methods

new(storage) click to toggle source

Constructor.

@param storage [Object] A storage instance. See github.com/Shuttlerock/trailer#storage

# File lib/trailer/recorder.rb, line 8
def initialize(storage)
  @storage = storage
end

Public Instance Methods

add_exception(err) click to toggle source

Records the exception class and message on the current trace.

@param err [Exception] The exception to record.

# File lib/trailer/recorder.rb, line 15
def add_exception(err)
  write(tags.merge(exception: err.class.name, message: err.message, trace: Array(err.backtrace)[0..9]))
end
finish() click to toggle source

Finish tracing, and flush storage.

# File lib/trailer/recorder.rb, line 20
def finish
  storage.async.flush
  @trace_id = nil
  @tags     = {}
end
start() click to toggle source

Create a new trace ID to link log entries.

# File lib/trailer/recorder.rb, line 27
def start
  raise Trailer::Error, 'finish() must be called before a new trace can be started' unless @trace_id.nil?

  # See https://github.com/aws/aws-xray-sdk-ruby/blob/1869ca5/lib/aws-xray-sdk/model/segment.rb#L26-L30
  @trace_id = %(1-#{Time.now.to_i.to_s(16)}-#{SecureRandom.hex(12)})
  @tags     = {} # This is used to accumulate tags in case we have an exception.
end
write(data) click to toggle source

Write the given hash to storage.

@param data [Hash] A key-value hash of trace data to write to storage.

# File lib/trailer/recorder.rb, line 38
def write(data)
  raise Trailer::Error, 'start() must be called before write()' if @trace_id.nil?
  raise Trailer::Error, 'data must be an instance of Hash' unless data.is_a?(Hash)

  # Include some standard tags.
  data[:environment]  ||= Trailer.config.environment
  data[:host_name]    ||= Trailer.config.host_name
  data[:service_name] ||= Trailer.config.service_name
  data                  = data.compact.merge(trace_id: trace_id)

  storage.async.write(data)
  @tags.merge!(data)
end