Skip to content

Commit 2496240

Browse files
author
prashantramangupta
committed
slack support for reporting error
1 parent ad97597 commit 2496240

File tree

6 files changed

+88
-13
lines changed

6 files changed

+88
-13
lines changed

constant.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,13 @@
88
}
99
}
1010
}
11+
SLACK_HOOK = {
12+
'hostname' : '',
13+
'port': 443,
14+
'path': '',
15+
'method': 'POST',
16+
'headers': {
17+
'Content-Type': 'application/json'
18+
}
19+
}
1120
METRICS_NETWORK_ID = 999

platform-usage-authorizer/lambda_handler.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,34 @@
22
import os
33

44
from authorizer import Token
5+
from utils import Utils
56

7+
obj_util = Utils()
68

79
def request_handler(event, context):
810
if 'path' not in event:
911
return get_response("400", {"status": "failed", "error": "Bad Request"})
1012
try:
11-
path = event['path'].lower()
1213
data = None
14+
payload_dict = None
15+
path = event['path'].lower()
1316
if "/event" == path:
1417
payload_dict = event['headers']
1518
print("Processing [" + str(path) + "] with body [" + str(payload_dict) + "]")
1619
token_instance = Token()
1720
data = token_instance.validate_token(daemon_id=payload_dict['x-daemonid'],
1821
token=payload_dict['x-token'])
19-
print(data)
2022
else:
2123
return get_response(500, "Invalid URL path.")
22-
response = get_lambda_authorizer_response_format(event=event, allow=data.get('validated', False))
23-
return response
24+
return get_lambda_authorizer_response_format(event=event, allow=data.get('validated', False))
2425
except Exception as e:
25-
print(repr(e))
26-
return get_lambda_authorizer_response_format(event=event, allow=False)
26+
err_msg = {"status": "failed",
27+
"error": repr(e),
28+
"api": event['path'],
29+
"payload": payload_dict,
30+
"type": "authorize"}
31+
obj_util.report_slack(1, str(err_msg))
32+
return get_response(500, err_msg)
2733

2834

2935
# Generate response JSON that API gateway expects from the lambda function
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pymysql==0.9.2
2+
requests==2.20.1

platform-usage-process/lambda_handler.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@
22

33
from metrics import Metrics
44
from register import Token
5+
from utils import Utils
6+
7+
obj_util = Utils()
58

69

710
def request_handler(event, context):
811
if 'path' not in event:
912
return get_response("400", {"status": "failed",
1013
"error": "Bad Request"})
1114
try:
12-
path = event['path'].lower()
1315
data = None
16+
payload_dict = None
17+
path = event['path'].lower()
1418
payload_dict = payload_check(payload=event['body'], path=path)
1519
if "/register" == path:
1620
token_instance = Token()
@@ -25,7 +29,6 @@ def request_handler(event, context):
2529
else:
2630
return get_response(500, "Invalid URL path.")
2731

28-
2932
if data is None:
3033
response = get_response("400", {"status": "failed",
3134
"error": "Bad Request",
@@ -41,11 +44,13 @@ def request_handler(event, context):
4144

4245
return response
4346
except Exception as e:
44-
response = get_response(500, {"status": "failed",
45-
"error": repr(e),
46-
"api": event['path'],
47-
"payload": payload_dict})
48-
return response
47+
err_msg = {"status": "failed",
48+
"error": repr(e),
49+
"api": event['path'],
50+
"payload": payload_dict,
51+
"type": "process"}
52+
obj_util.report_slack(1, str(err_msg))
53+
return get_response(500, err_msg)
4954

5055

5156
def payload_check(payload, path):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pymysql==0.9.2
2+
requests==2.20.1

utils.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import json
2+
import datetime
3+
import decimal
4+
import requests
5+
6+
from constant import SLACK_HOOK
7+
IGNORED_LIST = ['row_id', 'row_created', 'row_updated']
8+
9+
class Utils:
10+
def __init__(self):
11+
self.msg_type = {
12+
0 : 'info:: ',
13+
1 : 'err:: '
14+
}
15+
16+
def report_slack(self, type, slack_msg):
17+
url = SLACK_HOOK['hostname'] + SLACK_HOOK['path']
18+
prefix = self.msg_type.get(type, "")
19+
print(url)
20+
payload = {"channel": "#contract-index-alerts",
21+
"username": "webhookbot",
22+
"text": prefix + slack_msg,
23+
"icon_emoji": ":ghost:"
24+
}
25+
26+
resp = requests.post(url=url, data=json.dumps(payload))
27+
print(resp.status_code, resp.text)
28+
29+
def clean(self, value_list):
30+
for value in value_list:
31+
self.clean_row(value)
32+
33+
def clean_row(self, row):
34+
for item in IGNORED_LIST:
35+
del row[item]
36+
37+
for key in row:
38+
if isinstance(row[key], decimal.Decimal) or isinstance(row[key], datetime.datetime):
39+
row[key] = str(row[key])
40+
elif isinstance(row[key], bytes):
41+
if row[key] == b'\x01':
42+
row[key] = 1
43+
elif row[key] == b'\x00':
44+
row[key] = 0
45+
else:
46+
raise Exception("Unsupported bytes object. Key " + str(key) + " value " + str(row[key]))
47+
48+
return row
49+
50+
def remove_http_https_prefix(self, url):
51+
url = url.replace("https://","")
52+
url = url.replace("http://","")
53+
return url

0 commit comments

Comments
 (0)