Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom description for OpsGenie #457

Merged
merged 6 commits into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- None

## New features
- [OpsGenie] Add support for custom description - [#457](https://github.com/jertel/elastalert2/pull/457) - @nickbabkin
- Added support for markdown style formatting of aggregation tables - [#415](https://github.com/jertel/elastalert2/pull/415) - @Neuro-HSOC

## Other changes
Expand Down
2 changes: 2 additions & 0 deletions docs/source/ruletypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2364,6 +2364,8 @@ Optional:

``opsgenie_message``: Set the OpsGenie message to something other than the rule name. The message can be formatted with fields from the first match e.g. "Error occurred for {app_name} at {timestamp}.".

``opsgenie_description``: Set the OpsGenie description to something other than the rule body. The message can be formatted with fields from the first match e.g. "Error occurred for {app_name} at {timestamp}.".

``opsgenie_alias``: Set the OpsGenie alias. The alias can be formatted with fields from the first match e.g "{app_name} error".

``opsgenie_subject``: A string used to create the title of the OpsGenie alert. Can use Python string formatting.
Expand Down
6 changes: 5 additions & 1 deletion elastalert/alerters/opsgenie.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def __init__(self, *args):
self.teams_args = self.rule.get('opsgenie_teams_args')
self.tags = self.rule.get('opsgenie_tags', []) + ['ElastAlert', self.rule['name']]
self.to_addr = self.rule.get('opsgenie_addr', 'https://api.opsgenie.com/v2/alerts')
self.description = self.rule.get('opsgenie_description', None)
self.custom_message = self.rule.get('opsgenie_message')
self.opsgenie_subject = self.rule.get('opsgenie_subject')
self.opsgenie_subject_args = self.rule.get('opsgenie_subject_args')
Expand Down Expand Up @@ -77,7 +78,10 @@ def alert(self, matches):
post['responders'] = [{'username': r, 'type': 'user'} for r in self.recipients]
if self.teams:
post['teams'] = [{'name': r, 'type': 'team'} for r in self.teams]
post['description'] = body
if self.description:
post['description'] = self.description.format(**matches[0])
else:
post['description'] = body
if self.entity:
post['entity'] = self.entity.format(**matches[0])
if self.source:
Expand Down
45 changes: 45 additions & 0 deletions tests/alerters/opsgenie_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,51 @@ def test_opsgenie_create_custom_title():
assert excepted == actual


def test_opsgenie_create_custom_description():
rule = {
'name': 'Opsgenie Details',
'type': mock_rule(),
'opsgenie_account': 'genies',
'opsgenie_key': 'ogkey',
'opsgenie_description': 'Custom Description',
'opsgenie_details': {
'Message': {'field': 'message'},
'Missing': {'field': 'missing'}
},
'opsgenie_subject': 'test1'
}
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': 'Custom Description',
'details': {'Message': 'Testing'},
'message': 'test1',
'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_get_details():
rule = {
'name': 'Opsgenie Details',
Expand Down