Class: RailsErrorDashboard::Services::BaselineAlertThrottler
- Inherits:
-
Object
- Object
- RailsErrorDashboard::Services::BaselineAlertThrottler
- 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
-
.cleanup!(max_age_hours: 24) ⇒ Object
Clean up old entries (older than max_age_hours) Call periodically to prevent memory growth.
-
.clear! ⇒ Object
Clear all alert records (useful for testing).
-
.minutes_since_last_alert(error_type, platform) ⇒ Integer?
Get time since last alert.
-
.record_alert(error_type, platform) ⇒ Object
Record that an alert was sent.
-
.should_alert?(error_type, platform, cooldown_minutes: 120) ⇒ Boolean
Check if an alert should be sent (not in cooldown period).
Class Method Details
.cleanup!(max_age_hours: 24) ⇒ Object
Clean up old entries (older than max_age_hours) Call periodically to prevent memory growth
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
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
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)
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 |