Skip to content

Commit

Permalink
Merge pull request #1 from merit-network/add-retry-support
Browse files Browse the repository at this point in the history
Add retry support
  • Loading branch information
schelcj authored Jun 17, 2021
2 parents aa939d1 + 1a7dccf commit 9b3a626
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
21 changes: 19 additions & 2 deletions ZenPacks/zenoss/PagerDuty/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
##############################################################################
import json
import urllib2
from requests import PagerDutyRequestLimitExceeded
from retry import retry

import logging
log = logging.getLogger("zen.pagerduty.actions")
Expand Down Expand Up @@ -129,6 +131,7 @@ def execute(self, notification, signal):

self._performRequest(body, environ)

@retry(exceptions=PagerDutyRequestLimitExceeded, delay=60, jitter=(1, 5))
def _performRequest(self, body, environ):
"""
Actually performs the request to PagerDuty's Event API.
Expand All @@ -141,7 +144,17 @@ def _performRequest(self, body, environ):
dmdRoot = _dmdRoot(self.dmd)
apiTimeout = getattr(dmdRoot, ACCOUNT_ATTR).apiTimeout
bodyWithProcessedTalesExpressions = self._processTalExpressions(body, environ)
bodyWithProcessedTalesExpressions['payload']['severity'] = EVENT_MAPPING[bodyWithProcessedTalesExpressions['payload']['severity']]

try:
bodyWithProcessedTalesExpressions['payload']['severity'] = EVENT_MAPPING[bodyWithProcessedTalesExpressions['payload']['severity']]
except KeyError:
if bodyWithProcessedTalesExpressions['payload']['severity'] in EVENT_MAPPING.values():
pass
else:
raise
except Exception:
raise

requestBody = json.dumps(bodyWithProcessedTalesExpressions)

headers = {'Content-Type': 'application/json'}
Expand All @@ -156,7 +169,11 @@ def _performRequest(self, body, environ):
raise ActionExecutionException(msg)
elif hasattr(e, 'code'):
msg = 'The PagerDuty server couldn\'t fulfill the request: HTTP %d (%s)' % (e.code, e.msg)
raise ActionExecutionException(msg)

if e.code == 429:
raise PagerDutyRequestLimitExceeded(msg)
else:
raise ActionExecutionException(msg)
else:
raise ActionExecutionException('Unknown URLError occurred')

Expand Down
2 changes: 2 additions & 0 deletions ZenPacks/zenoss/PagerDuty/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class InvalidTokenException(Exception):
class PagerDutyUnreachableException(Exception):
pass

class PagerDutyRequestLimitExceeded(Exception):
pass

class ParseException(Exception):
pass
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
LICENSE = ""
NAMESPACE_PACKAGES = ['ZenPacks', 'ZenPacks.zenoss']
PACKAGES = ['ZenPacks', 'ZenPacks.zenoss', 'ZenPacks.zenoss.PagerDuty']
INSTALL_REQUIRES = []
INSTALL_REQUIRES = ['retry']
COMPAT_ZENOSS_VERS = ">= 4.2.5"
PREV_ZENPACK_NAME = "ZenPacks.PagerDuty.APINotification"
# STOP_REPLACEMENTS
Expand Down

0 comments on commit 9b3a626

Please sign in to comment.