Class: DebugBundle::Suppression::Tracker

Inherits:
Object
  • Object
show all
Defined in:
lib/debugbundle/suppression.rb

Instance Method Summary collapse

Constructor Details

#initializeTracker

Returns a new instance of Tracker.



30
31
32
# File 'lib/debugbundle/suppression.rb', line 30

def initialize
  @states = {}
end

Instance Method Details

#drain_aggregates(now:) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/debugbundle/suppression.rb', line 70

def drain_aggregates(now:)
  @states.each_with_object([]) do |(key, state), aggregates|
    next if state.pending_suppressed_count.zero?
    next if state.pending_first_seen_at.nil? || state.pending_last_seen_at.nil?
    next if checkpoint_not_due?(state, now)

    aggregates << {
      'fingerprint' => Digest::SHA256.hexdigest(key),
      'suppressed_count' => state.pending_suppressed_count,
      'first_seen' => Time.at(state.pending_first_seen_at).utc.iso8601,
      'last_seen' => Time.at(state.pending_last_seen_at).utc.iso8601,
      'window_seconds' => DUPLICATE_WINDOW_SECONDS.to_i
    }

    state.pending_suppressed_count = 0
    state.pending_first_seen_at = nil
    state.pending_last_seen_at = nil
    state.last_aggregate_emitted_at = now
  end
end

#should_capture(key, now:) ⇒ Object



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
# File 'lib/debugbundle/suppression.rb', line 34

def should_capture(key, now:)
  state = (@states[key] ||= new_state(now))

  if state.suppression_mode && (now - state.last_seen_at) >= LOOP_RESET_AFTER_SECONDS
    @states[key] = new_state(now)
    state = @states[key]
  end

  if (now - state.window_started_at) >= DUPLICATE_WINDOW_SECONDS
    state.window_started_at = now
    state.emitted_count = 0
  end

  if (now - state.loop_window_started_at) >= LOOP_WINDOW_SECONDS
    state.loop_window_started_at = now
    state.loop_hit_count = 0
  end

  state.loop_hit_count += 1
  state.last_seen_at = now
  state.suppression_mode = true if state.loop_hit_count > LOOP_THRESHOLD

  if state.suppression_mode
    mark_suppressed(state, now)
    return false
  end

  if state.emitted_count < MAX_NORMAL_EVENTS_PER_WINDOW
    state.emitted_count += 1
    return true
  end

  mark_suppressed(state, now)
  false
end