-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathv8.js
312 lines (263 loc) · 9.67 KB
/
v8.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import {
AVAILABLE_OPERATIONS,
DEFAULT_OPTIONS,
extractOperation,
isPlainObject,
translateFiltersIntoMethods,
validateOption,
} from "./common.js";
export function supabaseIntegration(SupabaseClient, Sentry, userOptions = {}) {
if (!SupabaseClient) {
throw new Error("SupabaseClient class constructor is required");
}
// We want to allow passing either `SupabaseClient` constructor
// or an instance returned from `createClient()`.
SupabaseClient =
SupabaseClient.constructor === Function
? SupabaseClient
: SupabaseClient.constructor;
if (!isPlainObject(userOptions)) {
throw new TypeError(`SupabaseIntegration options should be an object`);
}
const options = { ...DEFAULT_OPTIONS };
const availableOptions = Object.keys(DEFAULT_OPTIONS);
for (const [key, value] of Object.entries(userOptions)) {
validateOption(availableOptions, key, value);
options[key] = value;
}
const instrumented = new Map();
// Used only for testing purposes, as in the real world, everything would be only wrapped once,
// with a predefined set of options, where in tests we need to test different variants in the same env.
function _restore() {
for (const [obj, methods] of instrumented.entries()) {
for (const [method, impl] of Object.entries(methods)) {
obj.prototype[method] = impl;
}
}
}
function instrumentSupabaseClient(SupabaseClient, getCurrentHub) {
if (instrumented.has(SupabaseClient)) {
return;
}
instrumented.set(SupabaseClient, {
from: SupabaseClient.prototype.from,
});
SupabaseClient.prototype.from = new Proxy(SupabaseClient.prototype.from, {
apply(target, thisArg, argumentsList) {
const rv = Reflect.apply(target, thisArg, argumentsList);
const PostgrestQueryBuilder = rv.constructor;
instrumentPostgrestQueryBuilder(PostgrestQueryBuilder, getCurrentHub);
return rv;
},
});
}
function instrumentPostgrestQueryBuilder(PostgrestQueryBuilder) {
if (instrumented.has(PostgrestQueryBuilder)) {
return;
}
// We need to wrap _all_ operations despite them sharing the same `PostgrestFilterBuilder`
// constructor, as we don't know which method will be called first, an we don't want to miss any calls.
for (const operation of AVAILABLE_OPERATIONS) {
instrumented.set(PostgrestQueryBuilder, {
[operation]: PostgrestQueryBuilder.prototype[operation],
});
PostgrestQueryBuilder.prototype[operation] = new Proxy(
PostgrestQueryBuilder.prototype[operation],
{
apply(target, thisArg, argumentsList) {
const rv = Reflect.apply(target, thisArg, argumentsList);
const PostgrestFilterBuilder = rv.constructor;
instrumentPostgrestFilterBuilder(PostgrestFilterBuilder);
return rv;
},
}
);
}
}
// This is the only "instrumented" part of the SDK. The rest of instrumentation
// methods are only used as a mean to get to the `PostgrestFilterBuilder` constructor itself.
function instrumentPostgrestFilterBuilder(PostgrestFilterBuilder) {
if (instrumented.has(PostgrestFilterBuilder)) {
return;
}
instrumented.set(PostgrestFilterBuilder, {
then: PostgrestFilterBuilder.prototype.then,
});
PostgrestFilterBuilder.prototype.then = new Proxy(
PostgrestFilterBuilder.prototype.then,
{
apply(target, thisArg, argumentsList) {
const operations = options.operations;
const operation = extractOperation(thisArg.method, thisArg.headers);
if (!operations.includes(operation)) {
return Reflect.apply(target, thisArg, argumentsList);
}
const table = thisArg.url.pathname.split("/").slice(-1)[0];
const description = `from(${table})`;
const query = [];
for (const [key, value] of thisArg.url.searchParams.entries()) {
// It's possible to have multiple entries for the same key, eg. `id=eq.7&id=eq.3`,
// so we need to use array instead of object to collect them.
query.push(translateFiltersIntoMethods(key, value));
}
const body = {};
if (isPlainObject(thisArg.body)) {
for (const [key, value] of Object.entries(thisArg.body)) {
body[key] =
typeof options.sanitizeBody === "function"
? options.sanitizeBody(table, key, value)
: value;
}
}
const shouldCreatePayload = {
method: thisArg.method,
url: thisArg.url,
headers: thisArg.headers,
schema: thisArg.schema,
body: thisArg.body,
};
const shouldCreateSpan =
typeof options.shouldCreateSpan === "function"
? options.shouldCreateSpan(shouldCreatePayload)
: true;
let span;
if (options.tracing && shouldCreateSpan) {
const attributes = {
"db.table": table,
"db.schema": thisArg.schema,
"db.url": thisArg.url.origin,
"db.sdk": thisArg.headers["X-Client-Info"],
[Sentry.SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: "auto.db.supabase",
[Sentry.SEMANTIC_ATTRIBUTE_SENTRY_OP]: `db.${operation}`,
};
if (query.length) {
attributes["db.query"] = query;
}
if (Object.keys(body).length) {
attributes["db.body"] = body;
}
span = Sentry.startInactiveSpan({
name: description,
attributes,
});
}
return Reflect.apply(target, thisArg, [])
.then(
(res) => {
if (span) {
setHttpStatus(span, res.status);
span.end();
}
if (options.errors && res.error) {
const err = new Error(res.error.message);
if (res.error.code) {
err.code = res.error.code;
}
if (res.error.details) {
err.details = res.error.details;
}
const supabaseContext = {};
if (query.length) {
supabaseContext["query"] = query;
}
if (Object.keys(body).length) {
supabaseContext["body"] = body;
}
Sentry.captureException(err, {
contexts: {
supabase: supabaseContext,
},
});
}
const shouldCreateBreadcrumb =
typeof options.shouldCreateBreadcrumb === "function"
? options.shouldCreateBreadcrumb(shouldCreatePayload)
: true;
if (options.breadcrumbs && shouldCreateBreadcrumb) {
const breadcrumb = {
type: "supabase",
category: `db.${operation}`,
message: description,
};
const data = {};
if (query.length) {
data["query"] = query;
}
if (Object.keys(body).length) {
data["body"] = body;
}
if (Object.keys(data).length) {
breadcrumb["data"] = data;
}
Sentry.addBreadcrumb(breadcrumb);
}
return res;
},
(err) => {
if (span) {
setHttpStatus(span, 500);
span.end();
}
throw err;
}
)
.then(...argumentsList);
},
}
);
}
return {
name: "SupabaseIntegration",
setupOnce() {
instrumentSupabaseClient(SupabaseClient);
},
_restore,
};
}
const SPAN_STATUS_OK = 1;
const SPAN_STATUS_ERROR = 2;
function getSpanStatusFromHttpCode(httpStatus) {
if (httpStatus < 400 && httpStatus >= 100) {
return { code: SPAN_STATUS_OK };
}
if (httpStatus >= 400 && httpStatus < 500) {
switch (httpStatus) {
case 401:
return { code: SPAN_STATUS_ERROR, message: "unauthenticated" };
case 403:
return { code: SPAN_STATUS_ERROR, message: "permission_denied" };
case 404:
return { code: SPAN_STATUS_ERROR, message: "not_found" };
case 409:
return { code: SPAN_STATUS_ERROR, message: "already_exists" };
case 413:
return { code: SPAN_STATUS_ERROR, message: "failed_precondition" };
case 429:
return { code: SPAN_STATUS_ERROR, message: "resource_exhausted" };
case 499:
return { code: SPAN_STATUS_ERROR, message: "cancelled" };
default:
return { code: SPAN_STATUS_ERROR, message: "invalid_argument" };
}
}
if (httpStatus >= 500 && httpStatus < 600) {
switch (httpStatus) {
case 501:
return { code: SPAN_STATUS_ERROR, message: "unimplemented" };
case 503:
return { code: SPAN_STATUS_ERROR, message: "unavailable" };
case 504:
return { code: SPAN_STATUS_ERROR, message: "deadline_exceeded" };
default:
return { code: SPAN_STATUS_ERROR, message: "internal_error" };
}
}
return { code: SPAN_STATUS_ERROR, message: "unknown_error" };
}
function setHttpStatus(span, httpStatus) {
span.setAttribute("http.response.status_code", httpStatus);
const spanStatus = getSpanStatusFromHttpCode(httpStatus);
if (spanStatus.message !== "unknown_error") {
span.setStatus(spanStatus);
}
}