This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor.py
204 lines (174 loc) · 7.6 KB
/
processor.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
from sentry import utils
def normalize_issue(eventData, issueData):
payload = {}
if eventData is not None and len(eventData["entries"]) > 0:
entries = utils.filter_exception(eventData["entries"])
if len(entries) == 0:
return {
"error" : "Event does not have type 'exception' or 'stacktrace'"
}
payload["level"] = issueData["level"]
payload["platform"] = eventData["platform"]
payload["timestamp"] = eventData["dateCreated"]
payload["sdk"] = eventData["sdk"]
payload["tags"] = normalize_tags(eventData["tags"])
payload["tags"]["onprem_id"] = issueData["id"]
payload["tags"]["migration_id"] = issueData["migration_id"]
payload["tags"]["firstSeen"] = issueData['firstSeen']
payload["tags"]["firstRelease"] = issueData["release"]["first"] if "first" in issueData["release"] else None
payload["tags"]["migrated"] = "true"
payload["contexts"] = eventData["contexts"]
payload["message"] = eventData["message"] if "message" in eventData else ""
timestamps = {
"firstSeen" : issueData["firstSeen"],
"lastSeen" : issueData["lastSeen"],
"realTimestamp" : eventData["dateCreated"]
}
payload["contexts"]["timestamps"] = timestamps
environment = None
release = None
for attr in eventData["tags"] :
if attr["key"] == "environment" :
environment = attr["value"]
if attr["key"] == "release":
release = attr["value"]
payload["environment"] = environment
payload["release"] = release
if "extra" in eventData:
payload["extra"] = eventData["extra"]
elif "context" in eventData:
payload["extra"] = eventData["context"]
for exception in entries:
if exception["type"] == "exception":
dataValues = exception["data"]["values"][0] or None
if len(eventData["entries"]) > 1:
if eventData["entries"][1]["type"] == "breadcrumbs":
breadcrumbs = eventData["entries"][1]["data"]
payload["breadcrumbs"] = breadcrumbs
elif exception["type"] == "stacktrace":
dataValues = exception["data"]["frames"] or None
elif exception["type"] == "threads":
dataValues = exception["data"]["values"] or None
elif exception["type"] == "message":
dataValues = exception["data"]["formatted"] or None
if dataValues is not None:
try:
if exception["type"] == "exception":
error = {
"type" : dataValues["type"],
"value" : dataValues["value"],
"stacktrace" : normalize_stacktrace(dataValues["stacktrace"], eventData["platform"]),
"mechanism" : dataValues["mechanism"]
}
payload["exception"] = { "values": [error] }
elif exception["type"] == "stacktrace":
stacktrace = normalize_stacktrace(dataValues, eventData["platform"])
payload["stacktrace"] = stacktrace
elif exception["type"] == "threads":
event = dataValues[0]
event["stacktrace"] = normalize_stacktrace(event["stacktrace"], eventData["platform"])
payload["threads"] = { "values": [event]}
except Exception as e:
return {
"error" : f'Could not normalize data - Reason: {str(e)} - Skipping...'
}
else:
return {
"error" : "Event object has no data values - Skipping..."
}
else:
return {
"error" : "Event request did not return any data - Skipping..."
}
return payload
def normalize_tags(tags):
obj = {}
if tags is not None and len(tags) > 0:
for tag in tags:
obj[tag["key"]] = tag["value"]
return obj
def normalize_stacktrace(stacktrace, platform):
payload = {
"frames" : []
}
if stacktrace is None:
return payload
frames = stacktrace
if "frames" in stacktrace:
frames = stacktrace["frames"]
for frame in frames:
obj = {}
obj["filename"] = frame["filename"]
obj["function"] = frame["function"]
obj["lineno"] = frame["lineNo"]
obj["colno"] = frame["colNo"]
if platform == "python":
context_all = get_all_context_attr(frame)
if context_all is not None:
obj["pre_context"] = context_all["pre_context"]
obj["context_line"] = context_all["context_line"]
obj["post_context"] = context_all["post_context"]
if "module" in frame:
obj["module"] = frame["module"]
if "package" in frame:
obj["package"] = frame["package"]
if "instructionAddr" in frame:
obj["instructionAddr"] = frame["instructionAddr"]
if "symbolAddr" in frame:
obj["symbolAddr"] = frame["symbolAddr"]
if "rawFunction" in frame:
obj["rawFunction"] = frame["rawFunction"]
if "symbol" in frame:
obj["symbol"] = frame["symbol"]
if "vars" in frame:
obj["vars"] = frame["vars"]
if "errors" in frame:
obj["errors"] = frame["errors"]
if "trust" in frame:
obj["trust"] = frame["trust"]
if "inApp" in frame:
obj["in_app"] = frame["inApp"]
if "in_app" in frame and "inApp" not in frame:
obj["in_app"] = frame["in_app"]
payload["frames"].append(obj)
return payload
def get_all_context_attr(frame):
pre_context = []
context = None
post_context = []
properties = ["pre_context", "post_context", "context_line"]
if all(prop in frame for prop in properties):
return {
"pre_context" : frame["pre_context"],
"context_line" : frame["context_line"],
"post_context" : frame["post_context"]
}
if check_context_attrs(frame):
if ("e" in frame["vars"]) or ("err" in frame["vars"]):
error_line = frame["vars"]["e"] if "e" in frame["vars"] else frame["vars"]["err"]
context_line = utils.replace_all(error_line, [" ", "'", '"']).lower()
for line in frame["context"]:
formatted_str = utils.replace_all(line[1], [" ", "'", '"']).lower()
if context_line in formatted_str:
context = line[1]
else:
if context is None:
pre_context.append(line[1])
else:
post_context.append(line[1])
return {
"pre_context" : pre_context,
"context_line" : context,
"post_context" : post_context
}
else:
#try to guess context line
length = len(frame["context"])
return {
"pre_context": frame["context"][:round(length / 2) -1],
"context_line": frame["context"][round(length/2) -1],
"post_context": frame["context"][round(length / 2):]
}
return None
def check_context_attrs(frame):
return ("context" in frame and frame["context"] is not None) and ("vars" in frame and frame["vars"] is not None)