-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AP-531] Add VictorOps alert handler (#469)
- Loading branch information
Showing
7 changed files
with
157 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
pipelinewise/cli/alert_handlers/victorops_alert_handler.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
""" | ||
PipelineWise CLI - VictorOps alert handler | ||
""" | ||
import json | ||
import requests | ||
|
||
from .errors import InvalidAlertHandlerException | ||
from .base_alert_handler import BaseAlertHandler | ||
|
||
# Map alert levels to victorops compatible message types | ||
ALERT_LEVEL_MESSAGE_TYPES = { | ||
BaseAlertHandler.LOG: 'INFO', | ||
BaseAlertHandler.INFO: 'INFO', | ||
BaseAlertHandler.WARNING: 'WARNING', | ||
BaseAlertHandler.ERROR: 'CRITICAL' | ||
} | ||
|
||
|
||
# pylint: disable=too-few-public-methods | ||
class VictoropsAlertHandler(BaseAlertHandler): | ||
""" | ||
VictorOps Alert Handler class | ||
""" | ||
def __init__(self, config: dict) -> None: | ||
if config is not None: | ||
if 'base_url' not in config: | ||
raise InvalidAlertHandlerException('Missing REST Endpoint URL in VictorOps connection') | ||
self.base_url = config['base_url'] | ||
|
||
if 'routing_key' not in config: | ||
raise InvalidAlertHandlerException('Missing routing key in VictorOps connection') | ||
self.routing_key = config['routing_key'] | ||
|
||
else: | ||
raise InvalidAlertHandlerException('No valid VictorOps config supplied.') | ||
|
||
def send(self, message: str, level: str = BaseAlertHandler.ERROR, exc: Exception = None) -> None: | ||
""" | ||
Send alert | ||
Args: | ||
message: the alert message | ||
level: alert level | ||
exc: optional exception that triggered the alert | ||
Returns: | ||
Initialised alert handler object | ||
""" | ||
# Send alert to VictorOps REST Endpoint as a HTTP post request | ||
response = requests.post( | ||
f'{self.base_url}/{self.routing_key}', | ||
data=json.dumps({ | ||
'message_type': ALERT_LEVEL_MESSAGE_TYPES.get(level, BaseAlertHandler.ERROR), | ||
'entity_display_name': message, | ||
'state_message': exc}), | ||
headers={'Content-Type': 'application/json'}) | ||
|
||
# Success victorops message should return 200 | ||
if response.status_code != 200: | ||
raise ValueError('Request to victorops returned an error {}. {}'.format(response.status_code, | ||
response.text)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters