-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
132 additions
and
82 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,26 @@ | ||
import logging | ||
|
||
|
||
from prom2teams.teams.alarm_mapper import map_and_group, map_prom_alerts_to_teams_alarms | ||
from prom2teams.teams.composer import TemplateComposer | ||
from .teams_client import post | ||
from .teams_client import TeamsClient | ||
|
||
log = logging.getLogger('prom2teams') | ||
|
||
|
||
class AlarmSender: | ||
|
||
def __init__(self, template_path=None, group_alerts_by=False): | ||
def __init__(self, template_path=None, group_alerts_by=False, teams_client_config=None): | ||
self.json_composer = TemplateComposer(template_path) | ||
self.group_alerts_by = group_alerts_by | ||
if template_path: | ||
self.json_composer = TemplateComposer(template_path) | ||
else: | ||
self.json_composer = TemplateComposer() | ||
self.teams_client = TeamsClient(teams_client_config) | ||
self.max_payload = self.teams_client.max_payload_length | ||
|
||
def _create_alarms(self, alerts): | ||
if self.group_alerts_by: | ||
alarms = map_and_group(alerts, self.group_alerts_by) | ||
def _create_alarms(self, alerts, group_alerts_by): | ||
if group_alerts_by: | ||
alarms = map_and_group(alerts, group_alerts_by, self.json_composer.compose, self.max_payload) | ||
else: | ||
alarms = map_prom_alerts_to_teams_alarms(alerts) | ||
return self.json_composer.compose_all(alarms) | ||
|
||
def send_alarms(self, alerts, teams_webhook_url): | ||
sending_alarms = self._create_alarms(alerts) | ||
sending_alarms = self._create_alarms(alerts, self.group_alerts_by) | ||
for team_alarm in sending_alarms: | ||
log.debug('The message that will be sent is: %s', str(team_alarm)) | ||
post(teams_webhook_url, team_alarm) | ||
self.teams_client.post(teams_webhook_url, team_alarm) |
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 |
---|---|---|
@@ -1,20 +1,55 @@ | ||
import json | ||
import logging | ||
import requests | ||
from tenacity import retry, wait_fixed, after_log | ||
|
||
from .exceptions import MicrosoftTeamsRequestException | ||
|
||
session = requests.Session() | ||
session.headers.update({'Content-Type': 'application/json'}) | ||
|
||
|
||
def post(teams_webhook_url, message): | ||
response = session.post(teams_webhook_url, data=message) | ||
if not response.ok or response.text is not '1': | ||
exception_msg = 'Error performing request to: {}.\n' \ | ||
' Returned status code: {}.\n' \ | ||
' Returned data: {}\n' \ | ||
' Sent message: {}\n' | ||
raise MicrosoftTeamsRequestException(exception_msg.format(teams_webhook_url, | ||
str(response.status_code), | ||
str(response.text), | ||
str(message)), | ||
code=response.status_code) | ||
logger = logging.getLogger('prom2teams') | ||
|
||
|
||
class TeamsClient: | ||
DEFAULT_CONFIG = { | ||
'MAX_PAYLOAD': 24576, | ||
'RETRY_ENABLE': False, | ||
'RETRY_WAIT_TIME': 60 | ||
} | ||
|
||
def __init__(self, config=None): | ||
self.session = requests.Session() | ||
self.session.headers.update({'Content-Type': 'application/json'}) | ||
|
||
if config is None: | ||
config = {} | ||
config = {**TeamsClient.DEFAULT_CONFIG, **config} | ||
self.max_payload_length = config['MAX_PAYLOAD'] | ||
self.retry = config['RETRY_ENABLE'] | ||
self.wait_time = config['RETRY_WAIT_TIME'] | ||
|
||
def post(self, teams_webhook_url, message): | ||
@retry(wait=wait_fixed(self.wait_time), after=after_log(logger, logging.WARN)) | ||
def post_with_retry(teams_webhook_url, message): | ||
self._do_post(teams_webhook_url, message) | ||
|
||
def simple_post(teams_webhook_url, message): | ||
self._do_post(teams_webhook_url, message) | ||
|
||
logger.debug(f'The message that will be sent is: {message}') | ||
if self.retry: | ||
post_with_retry(teams_webhook_url, message) | ||
else: | ||
simple_post(teams_webhook_url, message) | ||
|
||
def _do_post(self, teams_webhook_url, message): | ||
response = self.session.post(teams_webhook_url, data=message) | ||
if not response.ok or response.text != '1': | ||
exception_msg = 'Error performing request to: {}.\n' \ | ||
' Returned status code: {}.\n' \ | ||
' Returned data: {}\n' \ | ||
' Sent message: {}\n' | ||
exception_msg.format(teams_webhook_url, | ||
str(response.status_code), | ||
str(response.text), | ||
str(message)) | ||
raise MicrosoftTeamsRequestException( | ||
exception_msg, code=response.status_code) |
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