Skip to content

Commit

Permalink
Merge pull request #1563 from mohamedelema17/master
Browse files Browse the repository at this point in the history
Support dynamic values via field substitution for the opsgenie_addr field
  • Loading branch information
jertel authored Nov 11, 2024
2 parents 95342c4 + 0e2f910 commit bb21503
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- Add support for Kibana 8.14/8.15 for Kibana Discover - [#1547](https://github.com/jertel/elastalert2/pull/1547) - @nsano-rururu
- Upgrade pylint 3.1.0 to 3.3.1, pytest 8.0.2 to 8.3.3, pytest-cov 4.1.0 to 5.0.0, pytest-xdist 3.5.0 to 3.6.1, sphinx 7.2.6 to 8.0.2, sphinx_rtd_theme 2.0.0 to 3.0.1, tox 4.13.0 to 4.21.2 - [#1550](https://github.com/jertel/elastalert2/pull/1550) - @nsano-rururu
- Upgrade to Python 3.13 - [#1551](https://github.com/jertel/elastalert2/pull/1551) - @nsano-rururu
- [OpsGenie] Support dynamic `opsgenie_addr` values - [#1563](https://github.com/jertel/elastalert2/pull/1563) - @mohamedelema17

# 2.20.0

Expand Down
2 changes: 1 addition & 1 deletion docs/source/alerts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1786,7 +1786,7 @@ Optional:

``opsgenie_account``: The OpsGenie account to integrate with.

``opsgenie_addr``: The OpsGenie URL to to connect against, default is ``https://api.opsgenie.com/v2/alerts``. If using the EU instance of Opsgenie, the URL needs to be ``https://api.eu.opsgenie.com/v2/alerts`` for requests to be successful.
``opsgenie_addr``: The OpsGenie URL to to connect against, default is ``https://api.opsgenie.com/v2/alerts``. If using the EU instance of Opsgenie, the URL needs to be ``https://api.eu.opsgenie.com/v2/alerts`` for requests to be successful. The address can be formatted with fields from the first match e.g "https://api.opsgenie.com/v2/alerts/{my_alias}/close?identifierType=alias"

``opsgenie_recipients``: A list OpsGenie recipients who will be notified by the alert.

Expand Down
2 changes: 1 addition & 1 deletion elastalert/alerters/opsgenie.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def alert(self, matches):
proxies = {'https': self.opsgenie_proxy} if self.opsgenie_proxy else None

try:
r = requests.post(self.to_addr, json=post, headers=headers, proxies=proxies)
r = requests.post(self.to_addr.format(**matches[0]), json=post, headers=headers, proxies=proxies)

elastalert_logger.debug('request response: {0}'.format(r))
if r.status_code != 202:
Expand Down
34 changes: 34 additions & 0 deletions tests/alerters/opsgenie_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1258,3 +1258,37 @@ def test_opsgenie_get_details2():
actual = alert.get_details(match)
expected = {}
assert expected == actual


def test_formatted_opsgenie_addr(caplog):
caplog.set_level(logging.INFO)
rule = {
'name': 'testOGalert',
'opsgenie_key': 'ogkey',
'opsgenie_addr': 'https://api.opsgenie.com/v2/alerts/{alert_id}',
'type': mock_rule()
}
with (mock.patch('requests.post') as mock_post):
rep = requests
rep.status_code = 202
url = rule.get('opsgenie_addr')
matches = [
{
'alert_id': '1234',
'@timestamp': '2014-10-31T00:00:00'
}
]
mock_post.return_value = rep
url.format(**matches[0])

alert = OpsGenieAlerter(rule)
alert.alert(matches)
mcal = mock_post._mock_call_args_list
assert mcal[0][0][0] == f'https://api.opsgenie.com/v2/alerts/{matches[0].get("alert_id")}'
assert mock_post.called

assert mcal[0][1]['headers']['Authorization'] == 'GenieKey ogkey'
assert mcal[0][1]['json']['source'] == 'ElastAlert'
user, level, message = caplog.record_tuples[0]
assert "Error response from https://api.opsgenie.com/v2/alerts \n API Response: <MagicMock name='post()' id=" not in message
assert ('elastalert', logging.INFO, 'Alert sent to OpsGenie') == caplog.record_tuples[0]

0 comments on commit bb21503

Please sign in to comment.