Module: ErrorRadar::Notifications::Discord

Defined in:
lib/error_radar/notifications/discord.rb

Constant Summary collapse

SEV_COLORS =
{
  'critical' => 0x7b001c,
  'error'    => 0xdc3545,
  'warning'  => 0xfd7e14,
  'info'     => 0x17a2b8
}.freeze
SPIKE_COLOR =
0xff6600

Class Method Summary collapse

Class Method Details

.build_payload(log, event) ⇒ Object



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
# File 'lib/error_radar/notifications/discord.rb', line 35

def self.build_payload(log, event)
  spike_data = log.instance_variable_get(:@spike_data)
  app        = ErrorRadar::Notifier.app_name
  link       = ErrorRadar::Notifier.error_url(log)

  prefix, color = case event
                  when :spike
                    ["⚠️ Spike (#{spike_data[:count]} hits / #{spike_data[:window_minutes]} min)", SPIKE_COLOR]
                  when :new_error
                    ['🆕 New error', SEV_COLORS[log.severity] || 0x6c757d]
                  else
                    ['🔁 Recurring error', SEV_COLORS[log.severity] || 0x6c757d]
                  end

  fields = [
    { name: 'Source',      value: (log.source || 'unknown').truncate(100), inline: true },
    { name: 'Category',    value: log.category.to_s,                       inline: true },
    { name: 'Severity',    value: log.severity.to_s,                       inline: true },
    { name: 'Occurrences', value: log.occurrences.to_s,                    inline: true }
  ]
  fields << { name: 'View', value: "[Error Radar](#{link})", inline: false } if link

  embed = {
    title:       "#{prefix}: #{log.error_class}",
    description: "```#{log.message.to_s.truncate(300)}```",
    color:       color,
    fields:      fields,
    footer:      { text: "[#{app}] Error Radar" },
    timestamp:   log.last_seen_at&.iso8601
  }.compact

  { embeds: [embed] }
end

.deliver(log, event = :recurring) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/error_radar/notifications/discord.rb', line 18

def self.deliver(log, event = :recurring)
  url = URI(ErrorRadar.config.discord_webhook_url)

  http              = Net::HTTP.new(url.host, url.port)
  http.use_ssl      = url.scheme == 'https'
  http.open_timeout = 5
  http.read_timeout = 5

  req                 = Net::HTTP::Post.new(url)
  req['Content-Type'] = 'application/json'
  req.body            = build_payload(log, event).to_json

  http.request(req)
rescue StandardError => e
  ErrorRadar::Tracking.warn_internal("Discord notification failed: #{e.message}")
end