Class: RailsErrorDashboard::Services::BaselineAlertThrottler

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_error_dashboard/services/baseline_alert_throttler.rb

Overview

Throttles baseline alerts to prevent alert fatigue

Tracks when alerts were last sent for each error_type/platform combination and prevents sending duplicate alerts within the cooldown window.

Uses an in-memory cache (class variable) for simplicity. For distributed systems, consider using Redis or a database-backed solution.

Class Method Summary collapse

Class Method Details

.cleanup!(max_age_hours: 24) ⇒ Object

Clean up old entries (older than max_age_hours) Call periodically to prevent memory growth

Parameters:

  • max_age_hours (Integer) (defaults to: 24)

    Remove entries older than this (default: 24)



72
73
74
75
76
77
78
# File 'lib/rails_error_dashboard/services/baseline_alert_throttler.rb', line 72

def cleanup!(max_age_hours: 24)
  cutoff_time = max_age_hours.hours.ago

  @mutex.synchronize do
    @last_alert_times.delete_if { |_, time| time < cutoff_time }
  end
end

.clear!Object

Clear all alert records (useful for testing)



63
64
65
66
67
# File 'lib/rails_error_dashboard/services/baseline_alert_throttler.rb', line 63

def clear!
  @mutex.synchronize do
    @last_alert_times.clear
  end
end

.minutes_since_last_alert(error_type, platform) ⇒ Integer?

Get time since last alert

Parameters:

  • error_type (String)

    The error type

  • platform (String)

    The platform

Returns:

  • (Integer, nil)

    Minutes since last alert, or nil if never alerted



51
52
53
54
55
56
57
58
59
60
# File 'lib/rails_error_dashboard/services/baseline_alert_throttler.rb', line 51

def minutes_since_last_alert(error_type, platform)
  key = alert_key(error_type, platform)

  @mutex.synchronize do
    last_time = @last_alert_times[key]
    return nil if last_time.nil?

    ((Time.current - last_time) / 60).to_i
  end
end

.record_alert(error_type, platform) ⇒ Object

Record that an alert was sent

Parameters:

  • error_type (String)

    The error type

  • platform (String)

    The platform



39
40
41
42
43
44
45
# File 'lib/rails_error_dashboard/services/baseline_alert_throttler.rb', line 39

def record_alert(error_type, platform)
  key = alert_key(error_type, platform)

  @mutex.synchronize do
    @last_alert_times[key] = Time.current
  end
end

.should_alert?(error_type, platform, cooldown_minutes: 120) ⇒ Boolean

Check if an alert should be sent (not in cooldown period)

Parameters:

  • error_type (String)

    The error type

  • platform (String)

    The platform

  • cooldown_minutes (Integer) (defaults to: 120)

    Cooldown period in minutes

Returns:

  • (Boolean)

    True if alert should be sent



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rails_error_dashboard/services/baseline_alert_throttler.rb', line 22

def should_alert?(error_type, platform, cooldown_minutes: 120)
  key = alert_key(error_type, platform)

  @mutex.synchronize do
    last_time = @last_alert_times[key]

    # No previous alert, allow this one
    return true if last_time.nil?

    # Check if cooldown period has passed
    Time.current > (last_time + cooldown_minutes.minutes)
  end
end