Module: ExceptionNotifier::ErrorGrouping::ClassMethods

Defined in:
lib/exception_notifier/modules/error_grouping.rb

Instance Method Summary collapse

Instance Method Details

#error_count(error_key) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/exception_notifier/modules/error_grouping.rb', line 27

def error_count(error_key)
  count = begin
    error_grouping_cache.read(error_key)
  rescue => e
    ExceptionNotifier.logger.warn("#{error_grouping_cache.inspect} failed to read, reason: #{e.message}. Falling back to memory cache store.")
    fallback_cache_store.read(error_key)
  end

  count.to_i if count
end

#fallback_cache_storeObject

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



23
24
25
# File 'lib/exception_notifier/modules/error_grouping.rb', line 23

def fallback_cache_store
  @fallback_cache_store ||= ActiveSupport::Cache::MemoryStore.new
end

#group_error!(exception, options) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/exception_notifier/modules/error_grouping.rb', line 45

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 = Rails.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) ⇒ Object



38
39
40
41
42
43
# File 'lib/exception_notifier/modules/error_grouping.rb', line 38

def save_error_count(error_key, count)
  error_grouping_cache.write(error_key, count, expires_in: error_grouping_period)
rescue => e
  ExceptionNotifier.logger.warn("#{error_grouping_cache.inspect} failed to write, reason: #{e.message}. Falling back to memory cache store.")
  fallback_cache_store.write(error_key, count, expires_in: error_grouping_period)
end

#send_notification?(exception, count) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
# File 'lib/exception_notifier/modules/error_grouping.rb', line 67

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