Sha256: f5ab6eaa2815061e4808e5d1031dcf5da406c5a3c09f8e96b42a283d42df3516

Contents?: true

Size: 1.9 KB

Versions: 3

Compression:

Stored size: 1.9 KB

Contents

# frozen_string_literal: true

module ElasticAPM
  # @api private
  class ErrorBuilder
    def initialize(agent)
      @agent = agent
    end

    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

    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

    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

    # rubocop:disable Metrics/AbcSize
    def add_current_transaction_fields(error, transaction)
      return unless transaction

      error.transaction_id = transaction.id
      error.transaction = { sampled: transaction.sampled? }
      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
    # rubocop:enable Metrics/AbcSize
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
elastic-apm-2.8.1 lib/elastic_apm/error_builder.rb
elastic-apm-2.8.0 lib/elastic_apm/error_builder.rb
elastic-apm-2.7.0 lib/elastic_apm/error_builder.rb