-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack.py
109 lines (93 loc) · 3.77 KB
/
slack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import json
import logging
import requests
import boto3
import os
import re
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# Get credentials
secret_name = os.environ["SECRET_NAME"]
secretsmanager_client = boto3.client("secretsmanager", region_name="ap-northeast-1")
resp = secretsmanager_client.get_secret_value(SecretId=secret_name)
secret = json.loads(resp["SecretString"])
ALERT_HIGH_CHANNEL_WEBHOOK = secret["ALERT_HIGH_CHANNEL_WEBHOOK"]
ALERT_MIDDLE_CHANNEL_WEBHOOK = secret["ALERT_MIDDLE_CHANNEL_WEBHOOK"]
USERGROUP_ID = secret["USERGROUP_ID"]
def notify_error(event, context):
logger.info(event["Execution"])
logger.info(event["State"])
logger.info(event["StateMachine"])
# StepFunctions の 前のステップから情報取得
error = event["param"]["Error"]
cause = event["param"]["Cause"]
# StepFunctions の Context オブジェクトの情報取得
input = event["Execution"]["Input"]
execution_arn = event["Execution"]["Id"]
execution_arn_regex = "arn:aws:states:(.*):([0-9]{12}):execution:(.*):(.*)"
# Context オブジェクトの情報をパース
region = re.search(execution_arn_regex, execution_arn).group(1)
account_id = re.search(execution_arn_regex, execution_arn).group(2)
sfn_machine_name = re.search(execution_arn_regex, execution_arn).group(3)
execution_url = f"https://{region}.console.aws.amazon.com/states/home?region={region}#/executions/details/{execution_arn}"
# Slackに投稿するメッセージ作成
title = (
f"StepFunctions Alert | {sfn_machine_name} | {region} | Account:{account_id}"
)
title_link = f"{execution_url}"
from_time = event["Execution"]["StartTime"]
# エラー原因に応じて通知種類を変更
if "Task timed outss" in cause:
alert_middle(title, title_link, error, cause, from_time, input)
else:
alert_high(title, title_link, error, cause, from_time, input)
def alert_middle(title, title_link, error, cause, from_time, input):
title = ":warning: " + title
payload = {
"attachments": [
{
"fallback": "Error test-alert-sls",
"color": "warning",
"title": title,
"title_link": title_link,
"fields": [
{"title": "Error", "value": error, "short": False},
{"title": "Cause", "value": cause, "short": False},
{"title": "From Time", "value": from_time, "short": False},
{"title": "Input", "value": f"```{input}```", "short": False},
],
}
]
}
data = json.dumps(payload)
try:
requests.post(ALERT_MIDDLE_CHANNEL_WEBHOOK, data=data)
except Exception as e:
logger.exception("alert_middle {}".format(e))
raise
def alert_high(title, title_link, error, cause, from_time, input):
title = ":rotating_light: " + title
usergroup_id = USERGROUP_ID
payload = {
"attachments": [
{
"fallback": "Error test-alert-sls",
"pretext": f"<!subteam^{usergroup_id}>",
"color": "danger",
"title": title,
"title_link": title_link,
"fields": [
{"title": "Error", "value": error, "short": False},
{"title": "Cause", "value": cause, "short": False},
{"title": "From Time", "value": from_time, "short": False},
{"title": "Input", "value": f"```{input}```", "short": False},
],
}
]
}
data = json.dumps(payload)
try:
requests.post(ALERT_HIGH_CHANNEL_WEBHOOK, data=data)
except Exception as e:
logger.exception("alert_high {}".format(e))
raise