-
Notifications
You must be signed in to change notification settings - Fork 24
/
index.ts
734 lines (633 loc) · 21.4 KB
/
index.ts
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
/**
* Sentry client for Cloudflare Workers.
* Adheres to https://docs.sentry.io/development/sdk-dev/overview/
*/
import {
User,
Request,
Stacktrace,
StackFrame,
Extra,
Extras,
Primitive,
} from '@sentry/types';
import { Options, Event, Breadcrumb, Level, RewriteFrames } from './types';
import { API } from '@sentry/core';
import {
isError,
isPlainObject,
extractExceptionKeysForMessage,
normalizeToSize,
} from '@sentry/utils';
import { parse } from 'cookie';
import { fromError } from 'stacktrace-js';
import { Scope } from './scope';
import { hasTracingEnabled, isValidSampleRate } from './utils';
export default class Toucan {
/**
* Default maximum number of breadcrumbs added to an event. Can be overwritten
* with {@link Options.maxBreadcrumbs}.
*/
private readonly DEFAULT_BREADCRUMBS = 100;
/**
* Absolute maximum number of breadcrumbs added to an event. The
* `maxBreadcrumbs` option cannot be higher than this value.
*/
private readonly MAX_BREADCRUMBS = 100;
/**
* Is a {@link Scope}[]
*/
private scopes: Scope[];
/**
* If an empty DSN is passed, we should treat it as valid option which signifies disabling the SDK.
*/
private disabled: boolean;
/**
* Options passed to constructor. See Options type.
*/
private options: Options;
/**
* Full store endpoint with auth search params. Parsed from options.dsn.
*/
private url: string;
/**
* Sentry request object transformed from incoming event.request.
*/
private request: Request | undefined;
constructor(options: Options) {
this.scopes = [new Scope()];
this.options = options;
if (!options.dsn || options.dsn.length === 0) {
// If an empty DSN is passed, we should treat it as valid option which signifies disabling the SDK.
this.url = '';
this.disabled = true;
this.debug(() => this.log('dsn missing, SDK is disabled'));
} else {
this.url = new API(options.dsn).getStoreEndpointWithUrlEncodedAuth();
this.disabled = false;
this.debug(() =>
this.log(`dsn parsed, full store endpoint: ${this.url}`)
);
}
// This is to maintain backwards compatibility for 'event' option. When we remove it, all this complex logic can go away.
if ('context' in options && options.context.request) {
this.request = this.toSentryRequest(options.context.request);
} else if ('request' in options && options.request) {
this.request = this.toSentryRequest(options.request);
} else if ('event' in options && 'request' in options.event) {
this.request = this.toSentryRequest(options.event.request);
}
this.beforeSend = this.beforeSend.bind(this);
/**
* Wrap all class methods in a proxy that:
* 1. Wraps all code in try/catch to handle internal erros gracefully.
* 2. Prevents execution if disabled = true
*/
return new Proxy(this, {
get: (target, key: string, receiver) => {
return (...args: any) => {
if (this.disabled) return;
try {
return Reflect.get(target, key, receiver).apply(target, args);
} catch (err) {
this.debug(() => this.error(err));
}
};
},
});
}
/**
* Set key:value that will be sent as extra data with the event.
*
* @param key String key of extra
* @param extra Extra value of extra
*/
setExtra(key: string, extra: Extra) {
this.getScope().setExtra(key, extra);
}
/**
* Set an object that will be merged sent as extra data with the event.
*
* @param extras Extras context object to merge into current context.
*/
setExtras(extras: Extras) {
this.getScope().setExtras(extras);
}
/**
* Set key:value that will be sent as tags data with the event.
*
* @param key String key of tag
* @param value Primitive value of tag
*/
setTag(key: string, value: Primitive) {
this.getScope().setTag(key, value);
}
/**
* Set an object that will be merged sent as tags data with the event.
*
* @param tags Tags context object to merge into current context.
*/
setTags(tags: { [key: string]: Primitive }) {
this.getScope().setTags(tags);
}
/**
* Overrides the Sentry default grouping. See https://docs.sentry.io/data-management/event-grouping/sdk-fingerprinting/
*
* @param fingerprint Array of strings used to override the Sentry default grouping.
*/
setFingerprint(fingerprint: string[]) {
this.getScope().setFingerprint(fingerprint);
}
/**
* Records a new breadcrumb which will be attached to future events.
*
* Breadcrumbs will be added to subsequent events to provide more context on user's actions prior to an error or crash.
* @param breadcrumb The breadcrum to record.
*/
addBreadcrumb(breadcrumb: Breadcrumb) {
const maxBreadcrumbs =
this.options.maxBreadcrumbs ?? this.DEFAULT_BREADCRUMBS;
const numberOfBreadcrumbs = Math.min(maxBreadcrumbs, this.MAX_BREADCRUMBS);
if (numberOfBreadcrumbs <= 0) return;
if (!breadcrumb.timestamp) {
breadcrumb.timestamp = this.timestamp();
}
this.getScope().addBreadcrumb(breadcrumb, numberOfBreadcrumbs);
}
/**
* Captures an exception event and sends it to Sentry.
*
* @param exception An exception-like object.
* @returns The generated eventId, or undefined if event wasn't scheduled.
*/
captureException(exception: unknown) {
this.debug(() => this.log(`calling captureException`));
const event = this.buildEvent({});
if (!event) return;
// This is to maintain backwards compatibility for 'event' option. When we remove it, all this complex logic can go away.
if ('context' in this.options) {
this.options.context.waitUntil(this.reportException(event, exception));
} else if ('event' in this.options) {
this.options.event.waitUntil(this.reportException(event, exception));
} else {
// 'waitUntil' not provided -- this is probably called from within a Durable Object
this.reportException(event, exception);
}
return event.event_id;
}
/**
* Captures a message event and sends it to Sentry.
*
* @param message The message to send to Sentry.
* @param level Define the level of the message.
* @returns The generated eventId, or undefined if event wasn't scheduled.
*/
captureMessage(message: string, level: Level = 'info') {
this.debug(() => this.log(`calling captureMessage`));
const event = this.buildEvent({ level, message });
if (!event) return;
if ('context' in this.options) {
this.options.context.waitUntil(this.reportMessage(event));
} else if ('event' in this.options) {
this.options.event.waitUntil(this.reportMessage(event));
} else {
// 'waitUntil' not provided -- this is probably called from within a Durable Object
this.reportMessage(event);
}
return event.event_id;
}
/**
* Updates user context information for future events.
*
* @param user — User context object to be set in the current context. Pass null to unset the user.
*/
setUser(user: User | null) {
this.getScope().setUser(user);
}
/**
* In Cloudflare Workers it’s not possible to read event.request's body after having generated a response (if you attempt to, it throws an exception).
* Chances are that if you are interested in reporting request body to Sentry, you have already read the data (via request.json()/request.text()).
* Use this method to set it in Sentry context.
* @param body
*/
setRequestBody(body: any) {
if (this.request) {
this.request.data = body;
} else {
this.request = { data: body };
}
}
/**
* Creates a new scope with and executes the given operation within. The scope is automatically removed once the operation finishes or throws.
* This is essentially a convenience function for:
*
* @example
* pushScope();
* callback();
* popScope();
*/
withScope(callback: (scope: Scope) => void): void {
const scope = this.pushScope();
try {
callback(scope);
} finally {
this.popScope();
}
}
/**
* Send data to Sentry.
*
* @param data Event data
*/
private async postEvent(data: Event) {
// We are sending User-Agent for backwards compatibility with older Sentry
let headers: Record<string, string> = {
'Content-Type': 'application/json',
'User-Agent': '__name__/__version__',
};
// Build headers
if (this.options?.transportOptions?.headers) {
headers = {
...headers,
...this.options.transportOptions.headers,
};
}
// Build body string
const body = JSON.stringify(data);
// Log the outgoing request
this.debug(() => {
this.log(
`sending request to Sentry with headers: ${JSON.stringify(
headers
)} and body: ${body}`
);
});
// Send to Sentry and wait for Response
const response = await fetch(this.url, {
method: 'POST',
body,
headers,
});
// Log the response
await this.debug(() => {
return this.logResponse(response);
});
// Resolve with response
return response;
}
/**
* Builds event payload. Applies beforeSend.
*
* @param additionalData Additional data added to defaults.
* @returns Event
*/
private buildEvent(additionalData: Event): Event | undefined {
if (hasTracingEnabled(this.options)) {
// 1.0 === 100% events are sent
// 0.0 === 0% events are sent
const sampleRate =
typeof this.options.tracesSampler === 'function'
? this.options.tracesSampler({ request: this.request })
: typeof this.options.tracesSampleRate === 'number'
? this.options.tracesSampleRate
: typeof this.options.sampleRate === 'number'
? this.options.sampleRate
: null;
// Invalid sampling values result in skipped events, for parity with other Sentry SDKs.
// However, don't skip if
if (!isValidSampleRate(sampleRate)) {
this.debug(() =>
this.log(`skipping this event because of invalid sample rate.`)
);
return;
}
// Now we roll the dice. Math.random() is inclusive of 0, but not 1, so we need >= here.
// This is because When sampleRate is 0 and Math.random() returns 0, we must skip the event.
// When sampleRate is 1, the event will always be sent, because Math.random() cannot return 1.
if (Math.random() >= Number(sampleRate)) {
this.debug(() =>
this.log(`skipping this event (sampling rate ${Number(sampleRate)})`)
);
return;
}
}
const pkg = this.options.pkg;
// 'release' option takes precedence, if not present - try to derive from package.json
const release = this.options.release
? this.options.release
: pkg
? `${pkg.name}-${pkg.version}`
: undefined;
const scope = this.getScope();
// per https://docs.sentry.io/development/sdk-dev/event-payloads/#required-attributes
const payload: Event = {
event_id: crypto.randomUUID().replace(/-/g, ''), // dashes are not allowed
logger: 'EdgeWorker',
platform: 'node',
release,
environment: this.options.environment,
timestamp: this.timestamp(),
level: 'error',
modules: pkg
? {
...pkg.dependencies,
...pkg.devDependencies,
}
: undefined,
...additionalData,
request: this.request,
sdk: {
name: '__name__',
version: '__version__',
},
};
// Type-casting 'breadcrumb' to any because our level type is a union of literals, as opposed to Level enum.
scope.applyToEvent(payload);
const beforeSend = this.options.beforeSend ?? this.beforeSend;
return beforeSend(payload);
}
/**
* Converts data from fetch event's Request to Sentry Request used in Sentry Event
*
* @param request FetchEvent Request
* @returns Sentry Request
*/
private toSentryRequest(request: FetchEvent['request']): Request {
// Build cookies
const cookieString = request.headers.get('cookie');
let cookies: Record<string, string> | undefined = undefined;
if (cookieString) {
try {
cookies = parse(cookieString);
} catch (e) {}
}
const headers: Record<string, string> = {};
// Build headers (omit cookie header, because we used it in the previous step)
for (const [k, v] of (request.headers as any).entries()) {
if (k !== 'cookie') {
headers[k] = v;
}
}
const rv: Request = {
method: request.method,
cookies,
headers,
};
try {
const url = new URL(request.url);
rv.url = `${url.protocol}//${url.hostname}${url.pathname}`;
rv.query_string = url.search;
} catch (e) {
// `new URL` failed, let's try to split URL the primitive way
const qi = request.url.indexOf('?');
if (qi < 0) {
// no query string
rv.url = request.url;
} else {
rv.url = request.url.substr(0, qi);
rv.query_string = request.url.substr(qi + 1);
}
}
return rv;
}
/**
* This SDK's implementation of beforeSend. If 'beforeSend' is not provided in options, this implementation will be applied.
* This function is applied to all events before sending to Sentry.
*
* By default it:
* 1. Removes all request headers (unless opts.allowedHeaders is provided - in that case the allowlist is applied)
* 2. Removes all request cookies (unless opts.allowedCookies is provided- in that case the allowlist is applied)
* 3. Removes all search params (unless opts.allowedSearchParams is provided- in that case the allowlist is applied)
*
* @param event
* @returns Event
*/
private beforeSend(event: Event) {
const request = event.request;
if (request) {
// Let's try to remove sensitive data from incoming Request
const allowedHeaders = this.options.allowedHeaders;
const allowedCookies = this.options.allowedCookies;
const allowedSearchParams = this.options.allowedSearchParams;
if (allowedHeaders) {
request.headers = this.applyAllowlist(request.headers, allowedHeaders);
} else {
delete request.headers;
}
if (allowedCookies) {
request.cookies = this.applyAllowlist(request.cookies, allowedCookies);
} else {
delete request.cookies;
}
if (allowedSearchParams) {
const params = Object.fromEntries(
new URLSearchParams(request.query_string) as any
);
const allowedParams = new URLSearchParams();
Object.keys(this.applyAllowlist(params, allowedSearchParams)).forEach(
(allowedKey) => {
allowedParams.set(allowedKey, params[allowedKey]);
}
);
request.query_string = allowedParams.toString();
} else {
delete request.query_string;
}
}
event.request = request;
return event;
}
/**
* Helper function that applies 'allowlist' on 'obj' keys.
*
* @param obj
* @param allowlist
* @returns New object with allowed keys.
*/
private applyAllowlist(
obj: Record<string, any> = {},
allowlist: string[] | RegExp
) {
let predicate: (item: string) => boolean = (item) => false;
if (allowlist instanceof RegExp) {
predicate = (item: string) => allowlist.test(item);
} else if (Array.isArray(allowlist)) {
const allowlistLowercased = allowlist.map((item) => item.toLowerCase());
predicate = (item: string) => allowlistLowercased.includes(item);
} else {
this.debug(() =>
this.warn(
'allowlist must be an array of strings, or a regular expression.'
)
);
return {};
}
return Object.keys(obj)
.map((key) => key.toLowerCase())
.filter((key) => predicate(key))
.reduce<Record<string, string>>((allowed, key) => {
allowed[key] = obj[key];
return allowed;
}, {});
}
/**
* A number representing the seconds elapsed since the UNIX epoch.
*/
private timestamp() {
return Date.now() / 1000;
}
/**
* Builds Message as per https://develop.sentry.dev/sdk/event-payloads/message/, adds it to the event,
* and sends it to Sentry. Inspired by https://github.com/getsentry/sentry-javascript/blob/master/packages/node/src/backend.ts.
*
* @param event
*/
private async reportMessage(event: Event) {
return this.postEvent(event);
}
/**
* Builds Exception as per https://docs.sentry.io/development/sdk-dev/event-payloads/exception/, adds it to the event,
* and sends it to Sentry. Inspired by https://github.com/getsentry/sentry-javascript/blob/master/packages/node/src/backend.ts.
*
* @param event
* @param error
*/
private async reportException(event: Event, maybeError: unknown) {
let error: Error;
if (isError(maybeError)) {
error = maybeError as Error;
} else if (isPlainObject(maybeError)) {
// This will allow us to group events based on top-level keys
// which is much better than creating new group when any key/value change
const message = `Non-Error exception captured with keys: ${extractExceptionKeysForMessage(
maybeError
)}`;
this.setExtra('__serialized__', normalizeToSize(maybeError as {}));
error = new Error(message);
} else {
// This handles when someone does: `throw "something awesome";`
// We use synthesized Error here so we can extract a (rough) stack trace.
error = new Error(maybeError as string);
}
const stacktrace = await this.buildStackTrace(error);
event.exception = {
values: [{ type: error.name, value: error.message, stacktrace }],
};
return this.postEvent(event);
}
/**
* Builds Stacktrace as per https://docs.sentry.io/development/sdk-dev/event-payloads/stacktrace/
*
* @param error Error object.
* @returns Stacktrace
*/
private async buildStackTrace(error: Error): Promise<Stacktrace | undefined> {
if (this.options.attachStacktrace === false) {
return undefined;
}
try {
const stack = await fromError(error);
/**
* sentry-cli and webpack-sentry-plugin upload the source-maps named by their path with a ~/ prefix.
* Lets adhere to this behavior.
*/
const rewriteFrames: RewriteFrames = this.options.rewriteFrames ?? {
root: '~/',
iteratee: (frame) => frame,
};
return {
frames: stack
.map<StackFrame>((frame) => {
const filename = frame.fileName ?? '';
const stackFrame = {
colno: frame.columnNumber,
lineno: frame.lineNumber,
filename,
function: frame.functionName,
};
if (!!rewriteFrames.root) {
stackFrame.filename = `${rewriteFrames.root}${stackFrame.filename}`;
}
return !!rewriteFrames.iteratee
? rewriteFrames.iteratee(stackFrame)
: stackFrame;
})
.reverse(),
};
} catch (e) {
return undefined;
}
}
/**
* Runs a callback if debug === true.
* Use this to delay execution of debug logic, to ensure toucan doesn't burn I/O in non-debug mode.
*
* @param callback
*/
private debug<ReturnType>(callback: () => ReturnType) {
if (this.options.debug) {
return callback();
}
}
private log(message: string) {
console.log(`toucan-js: ${message}`);
}
private warn(message: string) {
console.warn(`toucan-js: ${message}`);
}
private error(message: string) {
console.error(`toucan-js: ${message}`);
}
/**
* Reads and logs Response object from Sentry. Warning: Reads the Response stream (.text()).
* Do not use without this.debug wrapper.
*
* @param response Response
*/
private async logResponse(response: Response) {
let responseText = '';
// Read response body, set to empty if fails
try {
responseText = await response.text();
} catch (e) {
responseText += '';
}
// Parse origin from response.url, but at least give some string if parsing fails.
let origin = 'Sentry';
try {
const originUrl = new URL(response.url);
origin = originUrl.origin;
} catch (e) {
origin = response.url ?? 'Sentry';
}
const msg = `${origin} responded with [${response.status} ${response.statusText}]: ${responseText}`;
if (response.ok) {
this.log(msg);
} else {
this.error(msg);
}
}
/** Returns the scope of the top stack. */
private getScope() {
return this.scopes[this.scopes.length - 1];
}
/**
* Create a new scope to store context information.
* The scope will be layered on top of the current one. It is isolated, i.e. all breadcrumbs and context information added to this scope will be removed once the scope ends. * Be sure to always remove this scope with {@link this.popScope} when the operation finishes or throws.
*/
private pushScope(): Scope {
// We want to clone the content of prev scope
const scope = Scope.clone(this.getScope());
this.scopes.push(scope);
return scope;
}
/**
* Removes a previously pushed scope from the stack.
* This restores the state before the scope was pushed. All breadcrumbs and context information added since the last call to {@link this.pushScope} are discarded.
*/
private popScope(): boolean {
if (this.scopes.length <= 1) return false;
return !!this.scopes.pop();
}
}
export type { Breadcrumb, Level, Options };