-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentry-background.js
186 lines (173 loc) · 6.13 KB
/
sentry-background.js
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
try {
browser;
cookiesGet = (details, promise) => browser.cookies.get(details).then(promise);
localget = (keys, promise) => browser.storage.local.get(keys).then(promise);
// Not supported in chrome
browser.browserAction.setBadgeTextColor({color: "#ffffff"});
} catch (ex) {
browser = chrome;
cookiesGet = (details, promise) => browser.cookies.get(details, promise);
localget = (keys, promise) => browser.storage.local.get(keys, promise);
}
Sentry.init({
dsn: 'https://[email protected]/5498617',
integrations: [
new Sentry.Integrations.BrowserTracing(),
],
tracesSampleRate: 1.0,
release: "santry-trace-extension@" + browser.runtime.getManifest().version,
});
const DEFAULT_COLOUR = "#524a7e"
browser.browserAction.setBadgeBackgroundColor({color: DEFAULT_COLOUR});
// Specifically for dev
const DEV_REGEX = /.*dev\.getsentry\.net.*/;
let traceId = null;
let errorId = null;
let transactions = [];
function updateBadge(length, firstLoad) {
if (length == -1) {
fetchAndUpdateBadge(firstLoad);
} else if (length > 0) {
browser.browserAction.setBadgeText({text: `${length}`});
if (!firstLoad) {
browser.browserAction.setBadgeBackgroundColor({color: "#fa4747"});
window.setTimeout(
() => browser.browserAction.setBadgeBackgroundColor({color: DEFAULT_COLOUR}),
1000,
);
} else {
}
} else {
browser.browserAction.setBadgeText({text: ""});
}
}
function getProjects() {
cookiesGet({name: "session", url: "https://sentry.io"}, res => {
document.cookie = `session=${res.value}`;
let organizationMap = {};
let projectMap = {};
fetch("https://sentry.io/api/0/projects/").then(res => {res.json().then(data => {
data.forEach(project => {
organizationMap[project.id] = project.organization.slug.toLowerCase();
projectMap[project.id] = project.slug.toLowerCase();
});
browser.storage.local.set({"organizationMap": organizationMap});
browser.storage.local.set({"projectMap": projectMap});
browser.runtime.sendMessage({"projectLoaded": true});
})});
})
}
getProjects();
function fetchAndUpdateBadge(firstLoad) {
localget("transactions", function(data) {
if (data.transactions === undefined) {
data.transactions = [];
}
transactions = data.transactions;
updateBadge(data.transactions.length, firstLoad)
browser.storage.local.set(data);
});
}
fetchAndUpdateBadge(true);
browser.runtime.onMessage.addListener((data) => {
if (data.hasOwnProperty("length")) updateBadge(data.length, false);
if (data.hasOwnProperty("refresh")) getProjects();
transactions = [];
})
function isValidUrl(url, data) {
return (data.prodRegex ? url.match(new RegExp(data.prodRegex)) : false) || (data.stagingRegex ? url.match(new RegExp(data.stagingRegex)) : false);
}
function getProjectFromURL(event) {
const projects = new URL(event.url).pathname.match(/(\d+)/g);
if (projects.length != 1) {
// error
return -1
}
const project = projects[0];
if (project === "5498617") {
// Don't listen to this extension's events
return -1
}
return project
}
function decodeBody(event) {
const decoder = new TextDecoder("utf-8");
return decoder.decode(new Uint8Array(event.requestBody.raw[0].bytes));
}
function transactionListener(event) {
const project = getProjectFromURL(event);
if (project === -1) {
return
}
const sentryEvent = JSON.parse(decodeBody(event).split("\n", 3)[2]);
let newTraceId = sentryEvent?.contexts?.trace?.trace_id;
localget(["organizationMap"], function(data) {
const url = sentryEvent?.request?.url || "";
const isValid = data.organizationMap[project] && newTraceId;
const isLocal = url.match(new RegExp(DEV_REGEX));
if (isValid || isLocal) {
if (newTraceId !== traceId) {
const transactionEvent = {
environment: sentryEvent.environment,
event_id: sentryEvent.event_id,
isValid: isValid,
isLocal: isLocal,
timestamp: sentryEvent.timestamp,
trace_id: newTraceId,
transaction: sentryEvent.transaction,
organization: data.organizationMap[project],
}
traceId = newTraceId;
transactions.unshift(transactionEvent)
browser.storage.local.set({"transactions": transactions, "recentTransactions": transactions.slice(0, 10)});
browser.runtime.sendMessage({"length": transactions.length});
// messages aren't sent back to the current frame
updateBadge(transactions.length, false);
}
}
});
}
function errorListener(event) {
const project = getProjectFromURL(event);
if (project === -1) {
return
}
const sentryEvent = JSON.parse(decodeBody(event));
let newEventId = sentryEvent?.event_id;
localget(["organizationMap", "projectMap"], function(data) {
const url = sentryEvent?.request?.url || "";
const isValid = data.organizationMap[project] && newEventId;
const isLocal = url.match(new RegExp(DEV_REGEX));
if (isValid || isLocal) {
if (newEventId !== errorId) {
const errorEvent = {
environment: sentryEvent.environment,
event_id: newEventId,
isLocal: isLocal,
isValid: isValid,
timestamp: sentryEvent.timestamp,
exception: sentryEvent.exception,
type: sentryEvent?.exception?.values[0]?.type,
organization: data.organizationMap[project],
project: data.projectMap[project],
}
errorId = newEventId;
transactions.unshift(errorEvent)
browser.storage.local.set({"transactions": transactions, "recentTransactions": transactions.slice(0, 10)});
browser.runtime.sendMessage({"length": transactions.length});
// messages aren't sent back to the current frame
updateBadge(transactions.length, false);
}
}
});
}
browser.webRequest.onBeforeRequest.addListener(
transactionListener,
{urls: ["*://*.sentry.io/*/envelope/*", "*://dev.getsentry.net/*/envelope/*"]},
["requestBody"]
);
browser.webRequest.onBeforeRequest.addListener(
errorListener,
{urls: ["*://*.sentry.io/*/store/*", "*://dev.getsentry.net/*/store/*"]},
["requestBody"]
);