-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2489 from JeffAshton/opsgenie_details_fields
Adding ability to specify opsgenie alert details
- Loading branch information
Showing
4 changed files
with
300 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -456,6 +456,261 @@ def test_opsgenie_default_alert_routing(): | |
assert alert.get_info()['recipients'] == ['[email protected]'] | ||
|
||
|
||
def test_opsgenie_details_with_constant_value(): | ||
rule = { | ||
'name': 'Opsgenie Details', | ||
'type': mock_rule(), | ||
'opsgenie_account': 'genies', | ||
'opsgenie_key': 'ogkey', | ||
'opsgenie_details': {'Foo': 'Bar'} | ||
} | ||
match = { | ||
'@timestamp': '2014-10-31T00:00:00' | ||
} | ||
alert = OpsGenieAlerter(rule) | ||
|
||
with mock.patch('requests.post') as mock_post_request: | ||
alert.alert([match]) | ||
|
||
mock_post_request.assert_called_once_with( | ||
'https://api.opsgenie.com/v2/alerts', | ||
headers={ | ||
'Content-Type': 'application/json', | ||
'Authorization': 'GenieKey ogkey' | ||
}, | ||
json=mock.ANY, | ||
proxies=None | ||
) | ||
|
||
expected_json = { | ||
'description': BasicMatchString(rule, match).__str__(), | ||
'details': {'Foo': 'Bar'}, | ||
'message': 'ElastAlert: Opsgenie Details', | ||
'priority': None, | ||
'source': 'ElastAlert', | ||
'tags': ['ElastAlert', 'Opsgenie Details'], | ||
'user': 'genies' | ||
} | ||
actual_json = mock_post_request.call_args_list[0][1]['json'] | ||
assert expected_json == actual_json | ||
|
||
|
||
def test_opsgenie_details_with_field(): | ||
rule = { | ||
'name': 'Opsgenie Details', | ||
'type': mock_rule(), | ||
'opsgenie_account': 'genies', | ||
'opsgenie_key': 'ogkey', | ||
'opsgenie_details': {'Foo': {'field': 'message'}} | ||
} | ||
match = { | ||
'message': 'Bar', | ||
'@timestamp': '2014-10-31T00:00:00' | ||
} | ||
alert = OpsGenieAlerter(rule) | ||
|
||
with mock.patch('requests.post') as mock_post_request: | ||
alert.alert([match]) | ||
|
||
mock_post_request.assert_called_once_with( | ||
'https://api.opsgenie.com/v2/alerts', | ||
headers={ | ||
'Content-Type': 'application/json', | ||
'Authorization': 'GenieKey ogkey' | ||
}, | ||
json=mock.ANY, | ||
proxies=None | ||
) | ||
|
||
expected_json = { | ||
'description': BasicMatchString(rule, match).__str__(), | ||
'details': {'Foo': 'Bar'}, | ||
'message': 'ElastAlert: Opsgenie Details', | ||
'priority': None, | ||
'source': 'ElastAlert', | ||
'tags': ['ElastAlert', 'Opsgenie Details'], | ||
'user': 'genies' | ||
} | ||
actual_json = mock_post_request.call_args_list[0][1]['json'] | ||
assert expected_json == actual_json | ||
|
||
|
||
def test_opsgenie_details_with_nested_field(): | ||
rule = { | ||
'name': 'Opsgenie Details', | ||
'type': mock_rule(), | ||
'opsgenie_account': 'genies', | ||
'opsgenie_key': 'ogkey', | ||
'opsgenie_details': {'Foo': {'field': 'nested.field'}} | ||
} | ||
match = { | ||
'nested': { | ||
'field': 'Bar' | ||
}, | ||
'@timestamp': '2014-10-31T00:00:00' | ||
} | ||
alert = OpsGenieAlerter(rule) | ||
|
||
with mock.patch('requests.post') as mock_post_request: | ||
alert.alert([match]) | ||
|
||
mock_post_request.assert_called_once_with( | ||
'https://api.opsgenie.com/v2/alerts', | ||
headers={ | ||
'Content-Type': 'application/json', | ||
'Authorization': 'GenieKey ogkey' | ||
}, | ||
json=mock.ANY, | ||
proxies=None | ||
) | ||
|
||
expected_json = { | ||
'description': BasicMatchString(rule, match).__str__(), | ||
'details': {'Foo': 'Bar'}, | ||
'message': 'ElastAlert: Opsgenie Details', | ||
'priority': None, | ||
'source': 'ElastAlert', | ||
'tags': ['ElastAlert', 'Opsgenie Details'], | ||
'user': 'genies' | ||
} | ||
actual_json = mock_post_request.call_args_list[0][1]['json'] | ||
assert expected_json == actual_json | ||
|
||
|
||
def test_opsgenie_details_with_non_string_field(): | ||
rule = { | ||
'name': 'Opsgenie Details', | ||
'type': mock_rule(), | ||
'opsgenie_account': 'genies', | ||
'opsgenie_key': 'ogkey', | ||
'opsgenie_details': { | ||
'Age': {'field': 'age'}, | ||
'Message': {'field': 'message'} | ||
} | ||
} | ||
match = { | ||
'age': 10, | ||
'message': { | ||
'format': 'The cow goes %s!', | ||
'arg0': 'moo' | ||
} | ||
} | ||
alert = OpsGenieAlerter(rule) | ||
|
||
with mock.patch('requests.post') as mock_post_request: | ||
alert.alert([match]) | ||
|
||
mock_post_request.assert_called_once_with( | ||
'https://api.opsgenie.com/v2/alerts', | ||
headers={ | ||
'Content-Type': 'application/json', | ||
'Authorization': 'GenieKey ogkey' | ||
}, | ||
json=mock.ANY, | ||
proxies=None | ||
) | ||
|
||
expected_json = { | ||
'description': BasicMatchString(rule, match).__str__(), | ||
'details': { | ||
'Age': '10', | ||
'Message': "{'format': 'The cow goes %s!', 'arg0': 'moo'}" | ||
}, | ||
'message': 'ElastAlert: Opsgenie Details', | ||
'priority': None, | ||
'source': 'ElastAlert', | ||
'tags': ['ElastAlert', 'Opsgenie Details'], | ||
'user': 'genies' | ||
} | ||
actual_json = mock_post_request.call_args_list[0][1]['json'] | ||
assert expected_json == actual_json | ||
|
||
|
||
def test_opsgenie_details_with_missing_field(): | ||
rule = { | ||
'name': 'Opsgenie Details', | ||
'type': mock_rule(), | ||
'opsgenie_account': 'genies', | ||
'opsgenie_key': 'ogkey', | ||
'opsgenie_details': { | ||
'Message': {'field': 'message'}, | ||
'Missing': {'field': 'missing'} | ||
} | ||
} | ||
match = { | ||
'message': 'Testing', | ||
'@timestamp': '2014-10-31T00:00:00' | ||
} | ||
alert = OpsGenieAlerter(rule) | ||
|
||
with mock.patch('requests.post') as mock_post_request: | ||
alert.alert([match]) | ||
|
||
mock_post_request.assert_called_once_with( | ||
'https://api.opsgenie.com/v2/alerts', | ||
headers={ | ||
'Content-Type': 'application/json', | ||
'Authorization': 'GenieKey ogkey' | ||
}, | ||
json=mock.ANY, | ||
proxies=None | ||
) | ||
|
||
expected_json = { | ||
'description': BasicMatchString(rule, match).__str__(), | ||
'details': {'Message': 'Testing'}, | ||
'message': 'ElastAlert: Opsgenie Details', | ||
'priority': None, | ||
'source': 'ElastAlert', | ||
'tags': ['ElastAlert', 'Opsgenie Details'], | ||
'user': 'genies' | ||
} | ||
actual_json = mock_post_request.call_args_list[0][1]['json'] | ||
assert expected_json == actual_json | ||
|
||
|
||
def test_opsgenie_details_with_environment_variable_replacement(environ): | ||
environ.update({ | ||
'TEST_VAR': 'Bar' | ||
}) | ||
rule = { | ||
'name': 'Opsgenie Details', | ||
'type': mock_rule(), | ||
'opsgenie_account': 'genies', | ||
'opsgenie_key': 'ogkey', | ||
'opsgenie_details': {'Foo': '$TEST_VAR'} | ||
} | ||
match = { | ||
'@timestamp': '2014-10-31T00:00:00' | ||
} | ||
alert = OpsGenieAlerter(rule) | ||
|
||
with mock.patch('requests.post') as mock_post_request: | ||
alert.alert([match]) | ||
|
||
mock_post_request.assert_called_once_with( | ||
'https://api.opsgenie.com/v2/alerts', | ||
headers={ | ||
'Content-Type': 'application/json', | ||
'Authorization': 'GenieKey ogkey' | ||
}, | ||
json=mock.ANY, | ||
proxies=None | ||
) | ||
|
||
expected_json = { | ||
'description': BasicMatchString(rule, match).__str__(), | ||
'details': {'Foo': 'Bar'}, | ||
'message': 'ElastAlert: Opsgenie Details', | ||
'priority': None, | ||
'source': 'ElastAlert', | ||
'tags': ['ElastAlert', 'Opsgenie Details'], | ||
'user': 'genies' | ||
} | ||
actual_json = mock_post_request.call_args_list[0][1]['json'] | ||
assert expected_json == actual_json | ||
|
||
|
||
def test_jira(): | ||
description_txt = "Description stuff goes here like a runbook link." | ||
rule = { | ||
|