Module: Tocsin

Defined in:
lib/tocsin.rb,
lib/tocsin/config.rb,
lib/tocsin/version.rb,
lib/tocsin/notifiers.rb,
lib/tocsin/notifiers/email_notifier.rb

Overview

Library for escalating and logging errors.

Defined Under Namespace

Modules: Notifiers Classes: Config, NotificationJob

Constant Summary collapse

VERSION =
"0.1.2"

Class Method Summary collapse

Class Method Details

.configObject



88
89
90
# File 'lib/tocsin.rb', line 88

def self.config
  @config ||= Tocsin::Config.new
end

.configure {|config| ... } ⇒ Object

Yields:



83
84
85
86
# File 'lib/tocsin.rb', line 83

def self.configure
  @config = nil
  yield config
end

.loggerObject



96
97
98
# File 'lib/tocsin.rb', line 96

def self.logger
  @logger ||= config.logger
end

.notify(options) ⇒ Object

Synonyms for raise_alert when no exception is involved.



33
34
35
# File 'lib/tocsin.rb', line 33

def self.notify(options)
  self.raise_alert(nil, {:severity => "notification"}.merge(options))
end

.raise_alert(exception, options = {}) ⇒ Tocsin::Alert

Raise an alarm and escalate as configured in etc/tocsin.yml. medium, high, critical, or notification (arbitrary, though) this field.

Parameters:

  • exception (Exception)

    exception object if one was raised

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :severity (Object)

    severity of this notication. Should be low,

  • :message (Object)

    any info attached to this notification

  • :category (Object)

    totally arbitrary, but escalation can be configured based on

Returns:

  • (Tocsin::Alert)

    the created alert



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tocsin.rb', line 18

def self.raise_alert(exception, options={})
  urgent = options.delete(:now) || options.delete(:urgent) || options.delete(:synchronous)
  alert  = alert_for(exception, options)

  begin
    urgent ? sound(alert) : queue(alert)
  rescue => e
    sound cannot_enqueue_alert(e)
    sound alert
  end

  alert
end

.recipients(alert) ⇒ Object

Determine the recipients per notification method for a particular alert.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/tocsin.rb', line 61

def self.recipients(alert)
  # Each recipient group should look like (where the filter values are regexps):
  # - category: .*
  # severity: (critical|high)
  # recipients:
  # - [email protected]
  # notifier: email
  return {} unless config.recipient_groups

  recipients_per_notifier = config.recipient_groups.inject({}) do |rec_lists, group|
    if alert_matches_group(alert, group)
      notifier = group[:notifier] || Tocsin::Notifiers.default_notifier
      rec_lists[notifier] ||= []
      rec_lists[notifier] += group[:recipients]
    end

    rec_lists
  end

  recipients_per_notifier.each do |k, v| v.uniq!; v.sort! end # Filter duplicates and sort for sanity
end

.warn!(options) ⇒ Object



37
38
39
# File 'lib/tocsin.rb', line 37

def self.warn!(options)
  self.raise_alert(nil, options)
end

.watch(options = {}) ⇒ Object

Watch the yielded block for exceptions, and log one if it’s raised.



42
43
44
45
46
47
48
# File 'lib/tocsin.rb', line 42

def self.watch(options={})
  begin
    yield
  rescue exception_level => e
    raise_alert(e, options)
  end
end

.watch!(options = {}) ⇒ Object

Same as watch, but re-raises the exception after catching it.



51
52
53
54
55
56
57
58
# File 'lib/tocsin.rb', line 51

def self.watch!(options={})
  begin
    yield
  rescue exception_level => e
    raise_alert(e, options)
    raise e
  end
end