module Roqua module Support module Errors # global extra parameters to be added to all reports. def self.extra_parameters @extra_parameters || {} end def self.extra_parameters=(hash) fail 'not a hash' unless hash.is_a? Hash @extra_parameters = hash end # adds instance variable roqua_extra_parameters to current exception. def self.add_parameters(params) return unless params.is_a?(Hash) && $! $!.instance_eval do (@roqua_extra_parameters ||= {}).merge! params end end def self.report(exception, context = {}) return if const_defined?(:Rails) && Rails.env.test? namespace, parameters, _controller, skip_backtrace = merge_parameters(exception, context) notification_urls = [notify_appsignal(exception, parameters, namespace)] # Notify Roqua logging log_exception(exception, parameters, notification_urls.compact, skip_backtrace) end def self.appsignal_namespace_for_category(category) case category when :background then Appsignal::Transaction::BACKGROUND_JOB when :web then Appsignal::Transaction::HTTP_REQUEST when :hl7 then 'hl7' else Appsignal::Transaction::BLANK end end class << self private def merge_parameters(exception, context = {}) controller = context.delete :controller skip_backtrace = context.delete :skip_backtrace namespace = context.delete(:namespace) || :background [namespace, extra_parameters.merge(context).merge(extra_parameters_from_exception(exception)), controller, skip_backtrace] end def extra_parameters_from_exception(exception) exception.instance_variable_get(:@roqua_extra_parameters) || {} end def log_exception(exception, parameters = {}, notification_urls = [], skip_backtrace = false) if Roqua.respond_to?(:logger) exception_info = {class_name: exception.class.to_s, message: exception.message, parameters: parameters} exception_info[:notification_urls] = notification_urls if notification_urls.present? exception_info[:backtrace] = exception.backtrace unless skip_backtrace Roqua.logger.error('roqua.exception', exception_info) end rescue Exception end def notify_appsignal?(exception) const_defined?(:Appsignal) && Appsignal.active? end def notify_appsignal(exception, labels, namespace) return unless notify_appsignal?(exception) namespace = appsignal_namespace_for_category(namespace) current_transaction = Appsignal::Transaction.current if current_transaction.namespace == Appsignal::Transaction::HTTP_REQUEST current_transaction.set_tags(labels) current_transaction.add_exception(exception) else Appsignal.send_error exception do |transaction| transaction.set_tags labels transaction.set_namespace namespace end end end end end end end