jertel / elastalert2

ElastAlert 2 is a continuation of the original yelp/elastalert project. Pull requests are appreciated!
https://elastalert2.readthedocs.org
Apache License 2.0
859 stars 277 forks source link

Bugfix/opsgenie alerter handle keyerror in custom message #1467

Open mrsymlove opened 1 month ago

mrsymlove commented 1 month ago

Description

This PR addresses a bug related to the handling of KeyError when formatting custom messages in the OpsGenieAlerter class. The change ensures that the alert message does not cause a failure if a key is missing by providing a default fallback message.

Bug Details

ElastAlert2 Logs

2024-06-06T11:57:26.558778913Z ERROR:elastalert
(most recent call last):
2024-06-06T11:57:26.558807924Z File "/usr/local/lib/python3.12/site-packages/elastalert/elastalert.py", line 1324, in alert
2024-06-06T11:57:26.558811232Z return self.send_alert(matches, rule, alert_time=alert_time, retried=retried)
2024-06-06T11:57:26.558814067Z ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-06-06T11:57:26.558816392Z File "/usr/local/lib/python3.12/site-packages/elastalert/elastalert.py", line 1409, in send_alert
2024-06-06T11:57:26.558818728Z alert.alert(matches)
2024-06-06T11:57:26.558820932Z File "/usr/local/lib/python3.12/site-packages/elastalert/alerters/opsgenie.py", line 70, in alert
2024-06-06T11:57:26.558823318Z self.message = self.custom_message.format(**matches[0])
2024-06-06T11:57:26.558825477Z ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-06-06T11:57:26.558827645Z KeyError: 'stack_trace'
2024-06-06T11:57:26.558829715Z
2024-06-06T11:57:26.558832096Z ERROR:elastalert
exception running rule LIVE ERROR ALARM: 'stack_trace'

Opsgenie Rule Snippet

opsgenie_message: "LIVE ERROR:{app_name}:{message:.50}:{stack_trace:.70}"
opsgenie_priority: "P1"
opsgenie_alias: "{app_name}-{message:.50}-{stack_trace:.70}"

Checklist

Questions or Comments

Update of the documentation is not needed since it is only a bugfix.

jertel commented 4 weeks ago

Hello, thanks for the PR. The change looks ok to me. Once you add a unit test for the change we can get it merged in. Also the changelog needs to be updated with a link to this PR, similar to the other changelog entries.