forked from rails/exception_notification
-
Notifications
You must be signed in to change notification settings - Fork 414
/
error_grouping.rb
77 lines (63 loc) · 2.64 KB
/
error_grouping.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
require 'active_support/core_ext/numeric/time'
require 'active_support/concern'
module ExceptionNotifier
module ErrorGrouping
extend ActiveSupport::Concern
included do
mattr_accessor :error_grouping
self.error_grouping = false
mattr_accessor :error_grouping_period
self.error_grouping_period = 5.minutes
mattr_accessor :notification_trigger
mattr_accessor :error_grouping_cache
end
module ClassMethods
# Fallback to the memory store while the specified cache store doesn't work
#
def fallback_cache_store
@fallback_cache_store ||= ActiveSupport::Cache::MemoryStore.new
end
def error_count(error_key)
count = begin
error_grouping_cache.read(error_key)
rescue StandardError => 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
def save_error_count(error_key, count)
error_grouping_cache.write(error_key, count, expires_in: error_grouping_period)
rescue StandardError => 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
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
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
end
end
end