-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
257 lines (210 loc) · 9.66 KB
/
main.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import base64
import requests
import json
from datetime import timedelta
from datetime import datetime
from jinja2 import Template
from google.cloud import storage
import re
# this CF takes fivetran logs pub/sub and transform to messages for Slack webhook ingestion
def inject_to_slack(event, context):
"""Triggered from a message on a Cloud Pub/Sub topic.
Args:
event (dict): Event payload.
context (google.cloud.functions.Context): Metadata for the event.
"""
# define header for slack incoming webhook
headers = {
'Content-type': 'application/json',
}
# TO DO: get your webhook url from Slack
slack_url = f'https://hooks.slack.com/services/{slack_credentials}'
# decode event paylod
pubsub_message = base64.b64decode(event['data']).decode('utf-8')
pubsub_message = json.loads(pubsub_message)
# convert timezone to PST
ts = datetime.strptime(pubsub_message['receiveTimestamp'][:-4], "%Y-%m-%dT%H:%M:%S.%f")
new_ts = datetime.strftime(ts + timedelta(hours=-7), "%Y-%m-%dT%H:%M:%S.%f")
# set up alert message format; this is where you would like to customize your alert message:
alert_dict = {
'log_id': pubsub_message['insertId'],
'connector_id': pubsub_message['jsonPayload']['connector_id'],
'connector_type': pubsub_message['jsonPayload']['connector_type'],
'severity': pubsub_message['severity'],
'logName': pubsub_message['logName'],
'receiveTimestamp': new_ts
}
if 'data' in pubsub_message['jsonPayload'].keys():
alert_dict['jsonPayload_data'] = pubsub_message['jsonPayload']['data']
# only push ERROR or WARNING message to Slack
if alert_dict['severity'] == "ERROR": # or alert_dict['severity'] == "WARNING"
# WARNING: payload changes for every severity type. Need to change the keys of alert_dict['jsonPayload_data]
# set up jinja template
id_array = alert_dict['logName'].split("/")[-1].split("-")
pretty_msg = """
:red_circle: Connector Failed.
*Project*: {proj}
*Connector Type*: {connType}
*Connector Schema*: {connSchema}
*Alert Reason*: {reason}
*Alert Status*: {status}
*StackDriver Log ID*: {logId}
*Received Timestamp*: {receivedAtTimestamp}
*Severity*: {severity}
*fivetran Dash URL*: {dash_url}
""".format(
proj=id_array[1],
connType=alert_dict['connector_type'],
connSchema=alert_dict['connector_id'],
reason=alert_dict['jsonPayload_data']['reason'],
status=alert_dict['jsonPayload_data']['status'],
logId=alert_dict['log_id'],
receivedAtTimestamp=alert_dict['receiveTimestamp'],
severity=alert_dict['severity'],
dash_url=f"https://fivetran.com/dashboard/connectors/{id_array[2]}/{alert_dict['connector_id']}"
)
t = Template('{"text": "{{pretty_msg}}"}')
pretty_payload = t.render(pretty_msg=pretty_msg)
response = requests.post(slack_url, headers=headers, data=pretty_payload)
print(response.text)
else:
return None
# this CF takes airflow logs that are being stored in Cloud Storage and send to StackDriver only the traceback errors
def airflow_handler(data, context):
"""Triggered by a change to a Cloud Storage bucket.
Args:
event (dict): Event payload.
context (google.cloud.functions.Context): Metadata for the event.
"""
# define header for slack incoming webhook:
headers = {
'Content-type': 'application/json',
}
slack_url = f'https://hooks.slack.com/services/{slack_credentials}'
# getting blob metadata:
client = storage.Client()
bucket = client.get_bucket(format(data['bucket']))
blob = bucket.get_blob(format(data['name']))
blob_str = blob.download_as_string()
new_blob_str = blob_str.decode('utf-8')
new_blob_list = new_blob_str.split('\n')
blob_name = blob.name
log_array = blob_name.split('/')
# a helper that catches the beginning line number of Traceback and the ending line number of the same Traceback. Catches all.
def begin_end_trace(msg_list):
counter = 0
counter_tuple = []
for c, l in enumerate(msg_list):
if 'Traceback' in l and 'INFO' in l:
b_counter = c
counter = c
for sub_c, sub_l in enumerate(msg_list[counter:]):
if 'ERROR' in sub_l:
e_counter = sub_c + counter
counter_tuple.append((b_counter, e_counter))
return (counter_tuple)
# a helper that extracts trace errors
def get_trace(msg_list):
trace_dict = {}
for err_line in msg_list:
# first get the attempt number:
if 'Starting attempt' in err_line:
trace_dict['Attempts'] = err_line.split('-')[-1]
# only send final failed attempt
if err_line.split('-')[-1].split(' ')[-3] == err_line.split('-')[-1].split(' ')[-1]:
for begin_end_pos in begin_end_trace(msg_list):
begin_pos = begin_end_pos[0]
end_pos = begin_end_pos[1]
trace_dict['Traceback'] = msg_list[begin_pos: end_pos - 2]
return trace_dict
# a helper that trims down traceback:
def trimmer(long_list):
slim_list = []
for line in long_list:
if 'INFO -' in line:
slim_list.append(line.split("INFO -")[1])
return slim_list
# checking the logging_mixin output (last line of log before the empty line)
if 'Task exited with return code 1' in new_blob_list[-2]:
trace_dict = get_trace(msg_list = new_blob_list)
ts = datetime.strptime(new_blob_list[0][1:20], "%Y-%m-%d %H:%M:%S")
new_ts = datetime.strftime(ts + timedelta(hours=-7), "%Y-%m-%dT%H:%M:%S")
if 'Traceback' in trace_dict:
# set up alert message format; this is where you would like to customize your alert message:
alert_dict = {
'dag_id': log_array[0],
'task_id': log_array[1],
'attempts': trace_dict['Attempts'],
'traceback': trimmer(trace_dict['Traceback']),
'exec_ts': new_ts
}
new_alert_dict = '\n '.join(alert_dict['traceback'])
newer_alert_dict = new_alert_dict.replace("\"", "'")
conv_alert_dict = newer_alert_dict.replace('{}', '{{}}')
pretty_msg = """
:red_circle: Airflow DAG Failed.
*DAG ID*: {dagId}
*Task ID*: {taskId}
*Attempts of Retries*: {attempts}
*Execution Timestamp*: {execTimestamp}
*Traceback Details*:
{traceback}
*Severity*: {severity}
*Airflow DAG Status URL*: {url}
""".format(
dagId=alert_dict['dag_id'],
taskId=alert_dict['task_id'],
attempts=alert_dict['attempts'],
execTimestamp=alert_dict['exec_ts'],
traceback=conv_alert_dict,
severity='ERROR',
# pass your airflow domain plus port
url = f"http://{airflow_domain_port}/admin/airflow/log?task_id={alert_dict['task_id']}&dag_id={alert_dict['dag_id']}&execution_date={log_array[2].replace(':', '%3A').replace('+', '%2B')}&format=json")
t = Template('{"text": "{{pretty_msg}}"}')
pretty_payload = t.render(pretty_msg=pretty_msg)
response = requests.post(slack_url, headers=headers, data=pretty_payload)
print(response.text)
# this CF takes airflow server error logs around gs handler 404, and transform to messages for Slack
def sys_error(event, context):
"""Triggered from a message on a Cloud Pub/Sub topic.
Args:
event (dict): Event payload.
context (google.cloud.functions.Context): Metadata for the event.
"""
# define header for slack incoming webhook
headers = {
'Content-type': 'application/json',
}
slack_url = f'https://hooks.slack.com/services/{slack_credentials}'
# decode event paylod
pubsub_message = base64.b64decode(event['data']).decode('utf-8')
pubsub_message = json.loads(pubsub_message)
# convert timezone to PST
ts = datetime.strptime(pubsub_message['receiveTimestamp'][:-4], "%Y-%m-%dT%H:%M:%S.%f")
new_ts = datetime.strftime(ts + timedelta(hours=-7), "%Y-%m-%dT%H:%M:%S.%f")
# set up alert message format; this is where you would like to customize your alert message:
alert_dict = {
'log_id': pubsub_message['insertId'],
'labels': pubsub_message['labels']['compute.googleapis.com/resource_name'],
'logName': pubsub_message['logName'],
'receiveTimestamp': new_ts,
'textPayload': pubsub_message['textPayload'].replace('{', '{{').replace('}', '}}').replace('"', "'").replace('\\', ' ')
}
pretty_msg = """
:red_circle: Airflow GCS Handler experienced HTTP 404 Error.
*Log ID*: {logId}
*Labels*: {labels}
*Log Name*: {logName}
*Received Timestamp*: {receivedAtTimestamp}
*Error Message*: {logPayload}
""".format(
logId=alert_dict['log_id'],
labels=alert_dict['labels'],
logName=alert_dict['logName'],
receivedAtTimestamp=alert_dict['receiveTimestamp'],
logPayload=alert_dict['textPayload']
)
t = Template('{"text": "{{pretty_msg}}"}')
pretty_payload = t.render(pretty_msg=pretty_msg)
response = requests.post(slack_url, headers=headers, data=pretty_payload)
print(response.text)