Class: Exceptify::ErrorGrouping::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/exceptify/modules/error_grouping.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache:, fallback_cache_store:, period:, notification_trigger:, logger:) ⇒ Service

Returns a new instance of Service.



14
15
16
17
18
19
20
# File 'lib/exceptify/modules/error_grouping.rb', line 14

def initialize(cache:, fallback_cache_store:, period:, notification_trigger:, logger:)
  @cache = cache
  @fallback_cache_store = fallback_cache_store
  @period = period
  @notification_trigger = notification_trigger
  @logger = logger
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



12
13
14
# File 'lib/exceptify/modules/error_grouping.rb', line 12

def cache
  @cache
end

#fallback_cache_storeObject (readonly)

Returns the value of attribute fallback_cache_store.



12
13
14
# File 'lib/exceptify/modules/error_grouping.rb', line 12

def fallback_cache_store
  @fallback_cache_store
end

#loggerObject (readonly)

Returns the value of attribute logger.



12
13
14
# File 'lib/exceptify/modules/error_grouping.rb', line 12

def logger
  @logger
end

#notification_triggerObject (readonly)

Returns the value of attribute notification_trigger.



12
13
14
# File 'lib/exceptify/modules/error_grouping.rb', line 12

def notification_trigger
  @notification_trigger
end

#periodObject (readonly)

Returns the value of attribute period.



12
13
14
# File 'lib/exceptify/modules/error_grouping.rb', line 12

def period
  @period
end

Instance Method Details

#error_count(error_key) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/exceptify/modules/error_grouping.rb', line 22

def error_count(error_key)
  count =
    begin
      cache_store.read(error_key)
    rescue => e
      log_cache_error(cache_store, e, :read)
      fallback_cache_store.read(error_key)
    end

  count&.to_i
end

#group_error!(exception, options) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/exceptify/modules/error_grouping.rb', line 41

def group_error!(exception, options)
  message_based_key = key_for_message(exception)
  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 = key_for_backtrace(exception)

    if (count = error_count(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



34
35
36
37
38
39
# File 'lib/exceptify/modules/error_grouping.rb', line 34

def save_error_count(error_key, count)
  cache_store.write(error_key, count, expires_in: period)
rescue => e
  log_cache_error(cache_store, e, :write)
  fallback_cache_store.write(error_key, count, expires_in: period)
end

#send_notification?(exception, count) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
# File 'lib/exceptify/modules/error_grouping.rb', line 63

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