module ExceptionNotifier::ErrorGrouping::ClassMethods

Public Instance Methods

error_count(error_key) click to toggle source
# File lib/exception_notifier/modules/error_grouping.rb, line 30
def error_count(error_key)
  count =
    begin
      error_grouping_cache.read(error_key)
    rescue StandardError => e
      log_cache_error(error_grouping_cache, e, :read)
      fallback_cache_store.read(error_key)
    end

  count&.to_i
end
fallback_cache_store() click to toggle source

Fallback to the memory store while the specified cache store doesn’t work

# File lib/exception_notifier/modules/error_grouping.rb, line 26
def fallback_cache_store
  @fallback_cache_store ||= ActiveSupport::Cache::MemoryStore.new
end
group_error!(exception, options) click to toggle source
# File lib/exception_notifier/modules/error_grouping.rb, line 49
def group_error!(exception, options)
  message_based_key = "exception:#{Zlib.crc32("#{exception.class.name}\nmessage:#{exception.message}")}"
  accumulated_errors_count = 1

  if (count = error_count(message_based_key))
    accumulated_errors_count = count + 1
    save_error_count(message_based_key, accumulated_errors_count)
  else
    backtrace_based_key =
      "exception:#{Zlib.crc32("#{exception.class.name}\npath:#{exception.backtrace.try(:first)}")}"

    if (count = error_grouping_cache.read(backtrace_based_key))
      accumulated_errors_count = count + 1
      save_error_count(backtrace_based_key, accumulated_errors_count)
    else
      save_error_count(backtrace_based_key, accumulated_errors_count)
      save_error_count(message_based_key, accumulated_errors_count)
    end
  end

  options[:accumulated_errors_count] = accumulated_errors_count
end
save_error_count(error_key, count) click to toggle source
# File lib/exception_notifier/modules/error_grouping.rb, line 42
def save_error_count(error_key, count)
  error_grouping_cache.write(error_key, count, expires_in: error_grouping_period)
rescue StandardError => e
  log_cache_error(error_grouping_cache, e, :write)
  fallback_cache_store.write(error_key, count, expires_in: error_grouping_period)
end
send_notification?(exception, count) click to toggle source
# File lib/exception_notifier/modules/error_grouping.rb, line 72
def send_notification?(exception, count)
  if notification_trigger.respond_to?(:call)
    notification_trigger.call(exception, count)
  else
    factor = Math.log2(count)
    factor.to_i == factor
  end
end

Private Instance Methods

log_cache_error(cache, exception, action) click to toggle source
# File lib/exception_notifier/modules/error_grouping.rb, line 83
def log_cache_error(cache, exception, action)
  "#{cache.inspect} failed to #{action}, reason: #{exception.message}. Falling back to memory cache store."
end