class ElasticAPM::ErrorBuilder

@api private

Public Class Methods

new(agent) click to toggle source
# File lib/elastic_apm/error_builder.rb, line 5
def initialize(agent)
  @agent = agent
end

Public Instance Methods

build_exception(exception, context: nil, handled: true) click to toggle source
# File lib/elastic_apm/error_builder.rb, line 9
def build_exception(exception, context: nil, handled: true)
  error = Error.new context: context || Context.new
  error.exception = Error::Exception.new(exception, handled: handled)

  Util.reverse_merge!(error.context.tags, @agent.config.default_tags)

  if exception.backtrace
    add_stacktrace error, :exception, exception.backtrace
  end

  add_current_transaction_fields error, ElasticAPM.current_transaction

  error
end
build_log(message, context: nil, backtrace: nil, **attrs) click to toggle source
# File lib/elastic_apm/error_builder.rb, line 24
def build_log(message, context: nil, backtrace: nil, **attrs)
  error = Error.new context: context || Context.new
  error.log = Error::Log.new(message, **attrs)

  if backtrace
    add_stacktrace error, :log, backtrace
  end

  add_current_transaction_fields error, ElasticAPM.current_transaction

  error
end

Private Instance Methods

add_current_transaction_fields(error, transaction) click to toggle source

rubocop:disable Metrics/MethodLength, Metrics/AbcSize

# File lib/elastic_apm/error_builder.rb, line 55
def add_current_transaction_fields(error, transaction)
  return unless transaction

  error.transaction_id = transaction.id
  error.transaction = {
    sampled: transaction.sampled?,
    type: transaction.type
  }
  error.trace_id = transaction.trace_id
  error.parent_id = ElasticAPM.current_span&.id || transaction.id

  return unless transaction.context

  Util.reverse_merge!(error.context.tags, transaction.context.tags)
  Util.reverse_merge!(error.context.custom, transaction.context.custom)
end
add_stacktrace(error, kind, backtrace) click to toggle source
# File lib/elastic_apm/error_builder.rb, line 39
def add_stacktrace(error, kind, backtrace)
  stacktrace =
    @agent.stacktrace_builder.build(backtrace, type: :error)
  return unless stacktrace

  case kind
  when :exception
    error.exception.stacktrace = stacktrace
  when :log
    error.log.stacktrace = stacktrace
  end

  error.culprit = stacktrace.frames.first.function
end