-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Route SNS notifications through a Lambda function (#5246)
- Loading branch information
1 parent
7fb38a6
commit 497b492
Showing
6 changed files
with
170 additions
and
4 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
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,53 @@ | ||
import json | ||
|
||
import chalice.app | ||
from more_itertools import ( | ||
one, | ||
) | ||
|
||
from azul import ( | ||
JSON, | ||
cached_property, | ||
) | ||
Check notice Code scanning / CodeQL Unused import Note
Import of 'JSON' is not used.
|
||
from azul.chalice import ( | ||
AppController, | ||
) | ||
from azul.indexer.notify_service import ( | ||
AzulEmailNotificationService, | ||
) | ||
|
||
|
||
class NotifyController(AppController): | ||
|
||
@cached_property | ||
def email(self): | ||
return AzulEmailNotificationService() | ||
|
||
def digest(self, event: chalice.app.SNSEvent) -> None: | ||
email_body = self._format_to_html_body(json.loads(event.message)) | ||
self.email.notify_group(subject=event.subject, html=email_body) | ||
|
||
def _format_to_html_body(self, body: dict) -> str: | ||
trigger: dict = body['Trigger'] | ||
metric: dict = one([m for m in trigger.pop('Metrics') | ||
if 'MetricStat' in m])['MetricStat']['Metric'] | ||
alarm_name = body['AlarmName'] | ||
trigger = { | ||
k: v if type(v) is str else str(v) | ||
for k, v in trigger.items() | ||
} | ||
return f'''<html> | ||
<body> | ||
<h3> | ||
The alarm {alarm_name!r} is in {body['NewStateValue']!r} state.<br />{body['NewStateReason']} | ||
</h3> | ||
{''.join(['<p><strong>' + h[0] + '</strong><br />' + h[1] + '</p>' | ||
for h in (('Alarm State', body['NewStateValue']), | ||
('Alarm Name', alarm_name), | ||
('Timestamp', body['StateChangeTime']), | ||
('Namespace', metric.get('Namespace')), | ||
('Metric Name', metric.get('MetricName')), | ||
*(trigger.items()), | ||
)])} | ||
</body> | ||
</html>''' |
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,31 @@ | ||
from azul import ( | ||
JSON, | ||
config, | ||
) | ||
from azul.deployment import ( | ||
aws, | ||
) | ||
|
||
|
||
class AzulEmailNotificationService: | ||
|
||
def notify_group(self, subject: str, html: str) -> None: | ||
aws.ses.send_email( | ||
Source=f'NOTIFY@{config.indexer_endpoint.host}', | ||
Destination={ | ||
'ToAddresses': [config.monitoring_email] | ||
}, | ||
Message=self._format_message(subject, html) | ||
) | ||
|
||
def _format_message(self, subject: str, html: str) -> JSON: | ||
return { | ||
'Subject': { | ||
'Data': subject | ||
}, | ||
'Body': { | ||
'Html': { | ||
'Data': html | ||
} | ||
} | ||
} |
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