elastalert2 icon indicating copy to clipboard operation
elastalert2 copied to clipboard

Bugfix/opsgenie alerter handle keyerror in custom message

Open mrsymlove opened this issue 8 months ago • 1 comments

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

  • Issue: KeyError when formatting custom messages
  • File Affected: opsgenie.py
  • Change Description:
    • Added a try-except block to catch KeyError
    • Provided a default fallback message indicating the missing key

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

  • [x] I have reviewed the contributing guidelines.
  • [ ] I have included unit tests for my changes or additions.
  • [x] I have successfully run make test-docker with my changes.
  • [x] I have manually tested all relevant modes of the change in this PR.
  • [ ] I have updated the documentation.
  • [x] I have updated the changelog.

Questions or Comments

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

mrsymlove avatar Jun 11 '24 14:06 mrsymlove