Class: Findbug::Alerts::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/findbug/alerts/dispatcher.rb

Overview

Dispatcher routes alerts to configured channels.

ALERT FLOW

  1. Error captured → PersistJob runs

  2. PersistJob calls Dispatcher.notify(error_event)

  3. Dispatcher checks throttling (avoid spam)

  4. Dispatcher sends to enabled channels (async via AlertJob)

CHANNEL SOURCE

Alert channels are stored in the database (findbug_alert_channels table) and managed via the dashboard UI at /findbug/alerts.

THROTTLING

If your app throws 1000 errors in a minute, you don’t want 1000 Slack messages. Throttling limits alerts to one per error fingerprint per throttle period (default 5 minutes).

CHANNEL PRIORITY

Different channels for different severities:

  • Critical errors → All channels (email, Slack, etc.)

  • Warnings → Maybe just Slack

  • Info → Maybe just logged, no alerts

Class Method Summary collapse

Class Method Details

.any_enabled?Boolean

Check if any alert channels are enabled in the database

Returns:

  • (Boolean)


74
75
76
77
78
# File 'lib/findbug/alerts/dispatcher.rb', line 74

def any_enabled?
  Findbug::AlertChannel.enabled.exists?
rescue StandardError
  false
end

.notify(error_event, async: true) ⇒ Object

Send alert for an error event

Parameters:

  • error_event (ErrorEvent)

    the error to alert about

  • async (Boolean) (defaults to: true)

    whether to send asynchronously (default: true)



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/findbug/alerts/dispatcher.rb', line 43

def notify(error_event, async: true)
  return unless Findbug.enabled?
  return unless any_enabled?
  return unless should_alert?(error_event)
  return if throttled?(error_event)

  if async
    Jobs::AlertJob.perform_later(error_event.id)
  else
    send_alerts(error_event)
  end

  record_alert(error_event)
end

.send_alerts(error_event) ⇒ Object

Actually send alerts to all enabled channels

Parameters:

  • error_event (ErrorEvent)

    the error to alert about



62
63
64
65
66
67
68
69
70
71
# File 'lib/findbug/alerts/dispatcher.rb', line 62

def send_alerts(error_event)
  Findbug::AlertChannel.enabled.find_each do |channel_record|
    channel_instance = channel_record.channel_class.new(channel_record.config.symbolize_keys)
    channel_instance.send_alert(error_event)
  rescue StandardError => e
    Findbug.logger.error(
      "[Findbug] Failed to send alert to #{channel_record.name}: #{e.message}"
    )
  end
end