Module: Anomonitor::Notifiers

Defined in:
lib/anomonitor/notifiers.rb,
lib/anomonitor/notifiers/webhook.rb,
lib/anomonitor/notifiers/callable.rb,
lib/anomonitor/notifiers/composite.rb,
lib/anomonitor/notifiers/rate_limited.rb

Defined Under Namespace

Classes: Callable, Composite, RateLimited, Webhook

Constant Summary collapse

DETECTED =
"anomaly.detected"
RESOLVED =
"anomaly.resolved"
ACKED =
"anomaly.acked"
DIGEST =
"anomaly.digest"

Class Method Summary collapse

Class Method Details

.build(config = Anomonitor.config) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/anomonitor/notifiers.rb', line 12

def build(config = Anomonitor.config)
  raw = config.notifier
  notifier =
    if raw.nil?
      Webhook.new
    else
      list = Array(raw).map { |entry| wrap(entry) }
      list.size == 1 ? list.first : Composite.new(list)
    end

  limit = config.notifier_rate_limit.to_i
  limit.positive? ? RateLimited.new(notifier, limit_per_minute: limit) : notifier
end

.payload(anomaly, event: DETECTED) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/anomonitor/notifiers.rb', line 42

def payload(anomaly, event: DETECTED)
  {
    gem: "anomonitor",
    event: event.to_s,
    severity: anomaly.severity,
    rule: anomaly.rule,
    source: anomaly.source,
    metric: anomaly.metric,
    value: anomaly.value,
    threshold: anomaly.threshold,
    sampled_at: anomaly.sampled_at&.iso8601,
    resolved_at: anomaly.resolved_at&.iso8601,
    dashboard_url: Anomonitor.config.anomaly_dashboard_url(anomaly.id),
    tags: anomaly.tags
  }
end

.wrap(entry) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/anomonitor/notifiers.rb', line 26

def wrap(entry)
  case entry
  when Class
    entry.new
  else
    if entry.respond_to?(:deliver)
      entry
    elsif entry.respond_to?(:call)
      Callable.new(entry)
    else
      raise ArgumentError,
            "Anomonitor notifier must respond to #deliver or #call (got #{entry.class})"
    end
  end
end