-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathindex.ts
More file actions
102 lines (85 loc) · 3.56 KB
/
index.ts
File metadata and controls
102 lines (85 loc) · 3.56 KB
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
import type { Event, IntegrationFn, StackFrame } from '@sentry/core';
import { defineIntegration } from '@sentry/core';
import { patchWebAssembly } from './patchWebAssembly';
import { getImage, getImages } from './registry';
const INTEGRATION_NAME = 'Wasm';
interface WasmIntegrationOptions {
/**
* Key to identify this application for third-party error filtering.
* This key should match one of the keys provided to the `filterKeys` option
* of the `thirdPartyErrorFilterIntegration`.
*/
applicationKey?: string;
}
const _wasmIntegration = ((options: WasmIntegrationOptions = {}) => {
return {
name: INTEGRATION_NAME,
setupOnce() {
patchWebAssembly();
},
processEvent(event: Event): Event {
let hasAtLeastOneWasmFrameWithImage = false;
if (event.exception?.values) {
event.exception.values.forEach(exception => {
if (exception.stacktrace?.frames) {
hasAtLeastOneWasmFrameWithImage =
hasAtLeastOneWasmFrameWithImage || patchFrames(exception.stacktrace.frames, options.applicationKey);
}
});
}
if (hasAtLeastOneWasmFrameWithImage) {
event.debug_meta = event.debug_meta || {};
event.debug_meta.images = [...(event.debug_meta.images || []), ...getImages()];
}
return event;
},
};
}) satisfies IntegrationFn;
export const wasmIntegration = defineIntegration(_wasmIntegration);
const PARSER_REGEX = /^(.*?):wasm-function\[\d+\]:(0x[a-fA-F0-9]+)$/;
// We use the same prefix as bundler plugins so that thirdPartyErrorFilterIntegration
// recognizes WASM frames as first-party code without needing modifications.
const BUNDLER_PLUGIN_APP_KEY_PREFIX = '_sentryBundlerPluginAppKey:';
/**
* Patches a list of stackframes with wasm data needed for server-side symbolication
* if applicable. Returns true if the provided list of stack frames had at least one
* matching registered image.
*/
// Only exported for tests
export function patchFrames(frames: Array<StackFrame>, applicationKey?: string): boolean {
let hasAtLeastOneWasmFrameWithImage = false;
frames.forEach(frame => {
if (!frame.filename) {
return;
}
const split = frame.filename.split('(');
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const lastSplit = split[split.length - 1]!;
// Let's call this first match a "messy match".
// The browser stacktrace parser spits out frames that have a filename like this: "int) const (http://localhost:8001/main.wasm:wasm-function[190]:0x5aeb"
// It contains some leftover mess because wasm stack frames are more complicated than our parser can handle: "at MyClass::bar(int) const (http://localhost:8001/main.wasm:wasm-function[190]:0x5aeb)"
// This first match simply tries to mitigate the mess up until the first opening parens.
// The match afterwards is a sensible fallback
let match = lastSplit.match(PARSER_REGEX) as null | [string, string, string];
if (!match) {
match = frame.filename.match(PARSER_REGEX) as null | [string, string, string];
}
if (match) {
const index = getImage(match[1]);
frame.instruction_addr = match[2];
frame.filename = match[1];
frame.platform = 'native';
if (applicationKey) {
frame.module_metadata = {
...frame.module_metadata,
[`${BUNDLER_PLUGIN_APP_KEY_PREFIX}${applicationKey}`]: true,
};
}
if (index >= 0) {
frame.addr_mode = `rel:${index}`;
hasAtLeastOneWasmFrameWithImage = true;
}
}
});
return hasAtLeastOneWasmFrameWithImage;
}