# encoding: utf-8 require 'one_apm/support/helper' # This class encapsulates an error that was noticed by OneApm in a managed app. class OneApm::NoticedError extend OneApm::CollectionHelper include OneApm::Coerce attr_accessor :path, :timestamp, :params, :message, :exception_class_name attr_reader :exception_id, :is_internal OA_STRIPPED_EXCEPTION_REPLACEMENT_MESSAGE = "Message removed by OneApm 'strip_exception_messages' setting" def initialize(path, data, exception, timestamp = Time.now) @exception_id = exception.object_id @path = path @params = OneApm::NoticedError.normalize_params(data) @exception_class_name = exception.is_a?(Exception) ? exception.class.name : 'Error' # It's critical that we not hold onto the exception class constant in this # class. These objects get serialized for Resque to a process that might # not have the original exception class loaded, so do all processing now # while we have the actual exception! @is_internal = (exception.class < OneApm::Agent::InternalAgentError) if exception.nil? @message = '' elsif exception.respond_to?('original_exception') @message = (exception.original_exception || exception).to_s else # exception is not nil, but does not respond to original_exception @message = exception.to_s end unless @message.is_a?(String) # In pre-1.9.3, Exception.new({}).to_s.class != String # That is, Exception#to_s may not return a String instance if one wasn't # passed in upon creation of the Exception. So, try to generate a useful # String representation of the exception message, falling back to failsafe @message = String(@message.inspect) rescue '' end # clamp long messages to 4k so that we don't send a lot of # overhead across the wire @message = @message[0..4095] if @message.length > 4096 # replace error message if enabled if OneApm::Manager.config[:'strip_exception_messages.enabled'] && !self.class.passes_message_whitelist(exception.class) @message = OA_STRIPPED_EXCEPTION_REPLACEMENT_MESSAGE end @timestamp = timestamp end def ==(other) if other.respond_to?(:exception_id) exception_id == other.exception_id else false end end def self.passes_message_whitelist(exception_class) OneApm::Manager.config.stripped_exceptions_whitelist.any? do |klass| exception_class <= klass end end def to_collector_array(encoder=nil) [ OneApm::Helper.time_to_millis(timestamp), string(path), string(message), string(exception_class_name), params ] end end