Skip to content

Commit 22bb1ff

Browse files
mcwqy9JKHiggins
authored andcommitted
Add fallback_groups feature
1 parent b6ba7e3 commit 22bb1ff

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

lib/interferon.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,15 @@ def build_alerts_queue(hosts, alerts, groups)
306306
people += (groups[g] || [])
307307
end
308308

309+
# Convenience for alerts that select groups dynamically, since
310+
# those groups may not exist. Allow configuration of one or more
311+
# fallback_groups, that are static and are known to exist
312+
if people.empty?
313+
alert[:notify][:fallback_groups].each do |g|
314+
people += (groups[g] || [])
315+
end
316+
end
317+
309318
# queue the alert up for creation; we clone the alert to save the current state
310319
alerts_generated[alert[:name]] = [alert.clone, people]
311320
break if alert[:applies] == :once

lib/interferon/alert_dsl.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ def groups(v = nil, &block)
129129
get_or_set(:@groups, v, block, [])
130130
end
131131

132+
def fallback_groups(v=nil, &block)
133+
get_or_set(:@fallback_groups, v, block, [])
134+
end
135+
132136
def audit(v = nil, &block)
133137
get_or_set(:@audit, v, block, false)
134138
end

spec/lib/interferon_spec.rb

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,40 @@
126126
end
127127
end
128128

129-
context 'dry_run_update_alerts_on_destination' do
129+
context "#build_alerts_queue(hosts, alerts, groups)" do
130+
let(:interferon) { Interferon::Interferon.new({ 'processes' => 0 }, true) }
131+
132+
before do
133+
allow_any_instance_of(MockAlert).to receive(:evaluate)
134+
end
135+
136+
it 'adds people to alerts when notify.groups{} is used' do
137+
added = create_test_alert('name1', 'testquery3', '')
138+
groups = {'a' => ['foo', 'bar']}
139+
result = interferon.build_alerts_queue(['host'], [added], groups)
140+
expect(result[0]['name1'][1]).to eq(['foo', 'bar'].to_set)
141+
end
142+
143+
context 'when notify.fallback_groups{} is used' do
144+
it 'adds fallback people to alerts when no other groups are found' do
145+
added = create_test_alert_with_groups_and_fallback_groups(['nonexistent_group'],['fallback_group'])
146+
groups = {'fallback_group' => ['biz', 'baz']}
147+
result = interferon.build_alerts_queue(['host'], [added], groups)
148+
expect(result[0]['name1'][1]).to eq(['biz', 'baz'].to_set)
149+
end
150+
151+
it 'does not add fallback people to alerts when other groups are found' do
152+
added = create_test_alert_with_groups_and_fallback_groups(['group'],['fallback_group'])
153+
groups = {}
154+
groups['group'] = ['foo', 'bar']
155+
groups['fallback_groups'] = ['biz', 'baz']
156+
result = interferon.build_alerts_queue(['host'], [added], groups)
157+
expect(result[0]['name1'][1]).to eq(['foo', 'bar'].to_set)
158+
end
159+
end
160+
end
161+
162+
context "dry_run_update_alerts_on_destination" do
130163
let(:interferon) { Interferon::Interferon.new({ 'processes' => 0 }, true) }
131164

132165
before do
@@ -380,4 +413,18 @@ def create_test_alert(name, datadog_query, message, options = {})
380413

381414
MockAlert.new(alert_dsl)
382415
end
416+
417+
def create_test_alert_with_groups_and_fallback_groups(groups=[], fallback_groups=[])
418+
alert_dsl = AlertDSL.new({})
419+
420+
notify_dsl = NotifyDSL.new({})
421+
notify_dsl.groups(groups)
422+
notify_dsl.fallback_groups(fallback_groups)
423+
alert_dsl.instance_variable_set(:@notify, notify_dsl)
424+
425+
alert_dsl.name('name1')
426+
alert_dsl.applies(true)
427+
428+
MockAlert.new(alert_dsl)
429+
end
383430
end

0 commit comments

Comments
 (0)