lib/elastic_apm/error_builder.rb in elastic-apm-0.1.0 vs lib/elastic_apm/error_builder.rb in elastic-apm-0.2.0

- old
+ new

@@ -7,34 +7,50 @@ @config = config end attr_reader :config - def build_exception(exception, rack_env: nil, handled: true) + def build_exception(exception, handled: true) error = Error.new error.exception = Error::Exception.new(exception, handled: handled) - if (stacktrace = Stacktrace.build(config, exception.backtrace)) - error.exception.stacktrace = stacktrace - error.culprit = stacktrace.frames.last.function - end + add_stacktrace error, :exception, exception.backtrace + add_transaction_id error - if rack_env - error.context.request = Error::Context::Request.from_rack_env rack_env + if (transaction = ElasticAPM.current_transaction) + error.context = transaction.context.dup end error end def build_log(message, backtrace: nil, **attrs) error = Error.new error.log = Error::Log.new(message, **attrs) - if (stacktrace = Stacktrace.build(config, backtrace)) + add_stacktrace error, :log, backtrace + add_transaction_id error + + error + end + + private + + def add_stacktrace(error, kind, backtrace) + return unless (stacktrace = Stacktrace.build(config, backtrace)) + + case kind + when :exception + error.exception.stacktrace = stacktrace + when :log error.log.stacktrace = stacktrace - error.culprit = stacktrace.frames.last.function end - error + error.culprit = stacktrace.frames.last.function + end + + def add_transaction_id(error) + return unless (transaction = ElasticAPM.current_transaction) + error.transaction_id = transaction.id end end end