-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.ts
More file actions
243 lines (224 loc) · 7.94 KB
/
index.ts
File metadata and controls
243 lines (224 loc) · 7.94 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
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
import type { AsyncLocalStorage } from 'node:async_hooks';
import { arch as _arch, platform as _platform } from 'node:os';
import { join, resolve } from 'node:path';
import { env, versions } from 'node:process';
import { threadId } from 'node:worker_threads';
import { familySync } from 'detect-libc';
import { getAbi } from 'node-abi';
const stdlib = familySync();
const platform = process.env['BUILD_PLATFORM'] || _platform();
const arch = process.env['BUILD_ARCH'] || _arch();
const abi = getAbi(versions.node, 'node');
const identifier = [platform, arch, stdlib, abi].filter(c => c !== undefined && c !== null).join('-');
type AsyncStorageArgs = {
/** The AsyncLocalStorage instance used to fetch the store */
asyncLocalStorage: AsyncLocalStorage<unknown>;
/**
* Optional array of keys to fetch a specific property from the store
* Key will be traversed in order through Objects/Maps to reach the desired property.
*
* This is useful if you want to capture Open Telemetry context values as state.
*
* To get this value:
* context.getValue(my_unique_symbol_ref)
*
* You would set:
* stateLookup: ['_currentContext', my_unique_symbol_ref]
*/
stateLookup?: Array<string | symbol>;
}
type Thread<A = unknown, P = unknown> = {
frames: StackFrame[];
/** State captured from the AsyncLocalStorage, if provided */
asyncState?: A;
/** Optional state provided when calling threadPoll */
pollState?: P;
}
type StackFrame = {
function: string;
filename: string;
lineno: number;
colno: number;
};
interface Native {
registerThread(threadName: string): void;
registerThread(storage: AsyncStorageArgs, threadName: string): void;
threadPoll(enableLastSeen?: boolean, pollState?: object): void;
captureStackTrace<A = unknown, P = unknown>(): Record<string, Thread<A, P>>;
getThreadsLastSeen(): Record<string, number>;
}
// eslint-disable-next-line complexity
function getNativeModule(): Native {
// If a binary path is specified, use that.
if (env['SENTRY_STACK_TRACE_BINARY_PATH']) {
const envPath = env['SENTRY_STACK_TRACE_BINARY_PATH'];
return require(envPath);
}
// If a user specifies a different binary dir, they are in control of the binaries being moved there
if (env['SENTRY_STACK_TRACE_BINARY_DIR']) {
const binaryPath = join(resolve(env['SENTRY_STACK_TRACE_BINARY_DIR']), `stack-trace-${identifier}.node`);
return require(binaryPath);
}
if (process.versions.electron) {
try {
return require('../build/Release/stack-trace.node');
} catch (e) {
// eslint-disable-next-line no-console
console.warn('The \'@sentry-internal/node-native-stacktrace\' binary could not be found. Use \'@electron/rebuild\' to ensure the native module is built for Electron.');
throw e;
}
}
// We need the fallthrough so that in the end, we can fallback to the dynamic require.
// This is for cases where precompiled binaries were not provided, but may have been compiled from source.
if (platform === 'darwin') {
if (arch === 'x64') {
if (abi === '108') {
return require('./stack-trace-darwin-x64-108.node');
}
if (abi === '115') {
return require('./stack-trace-darwin-x64-115.node');
}
if (abi === '127') {
return require('./stack-trace-darwin-x64-127.node');
}
if (abi === '137') {
return require('./stack-trace-darwin-x64-137.node');
}
}
if (arch === 'arm64') {
if (abi === '108') {
return require('./stack-trace-darwin-arm64-108.node');
}
if (abi === '115') {
return require('./stack-trace-darwin-arm64-115.node');
}
if (abi === '127') {
return require('./stack-trace-darwin-arm64-127.node');
}
if (abi === '137') {
return require('./stack-trace-darwin-arm64-137.node');
}
}
}
if (platform === 'win32') {
if (arch === 'x64') {
if (abi === '108') {
return require('./stack-trace-win32-x64-108.node');
}
if (abi === '115') {
return require('./stack-trace-win32-x64-115.node');
}
if (abi === '127') {
return require('./stack-trace-win32-x64-127.node');
}
if (abi === '137') {
return require('./stack-trace-win32-x64-137.node');
}
}
}
if (platform === 'linux') {
if (arch === 'x64') {
if (stdlib === 'musl') {
if (abi === '108') {
return require('./stack-trace-linux-x64-musl-108.node');
}
if (abi === '115') {
return require('./stack-trace-linux-x64-musl-115.node');
}
if (abi === '127') {
return require('./stack-trace-linux-x64-musl-127.node');
}
if (abi === '137') {
return require('./stack-trace-linux-x64-musl-137.node');
}
}
if (stdlib === 'glibc') {
if (abi === '108') {
return require('./stack-trace-linux-x64-glibc-108.node');
}
if (abi === '115') {
return require('./stack-trace-linux-x64-glibc-115.node');
}
if (abi === '127') {
return require('./stack-trace-linux-x64-glibc-127.node');
}
if (abi === '137') {
return require('./stack-trace-linux-x64-glibc-137.node');
}
}
}
if (arch === 'arm64') {
if (stdlib === 'musl') {
if (abi === '108') {
return require('./stack-trace-linux-arm64-musl-108.node');
}
if (abi === '115') {
return require('./stack-trace-linux-arm64-musl-115.node');
}
if (abi === '127') {
return require('./stack-trace-linux-arm64-musl-127.node');
}
if (abi === '137') {
return require('./stack-trace-linux-arm64-musl-137.node');
}
}
if (stdlib === 'glibc') {
if (abi === '108') {
return require('./stack-trace-linux-arm64-glibc-108.node');
}
if (abi === '115') {
return require('./stack-trace-linux-arm64-glibc-115.node');
}
if (abi === '127') {
return require('./stack-trace-linux-arm64-glibc-127.node');
}
if (abi === '137') {
return require('./stack-trace-linux-arm64-glibc-137.node');
}
}
}
}
return require(`./stack-trace-${identifier}.node`);
}
const native = getNativeModule();
export function registerThread(threadName?: string): void;
export function registerThread(storageOrThread: AsyncStorageArgs | string, threadName?: string): void;
/**
* Registers the current thread with the native module.
*
* This should be called on every thread that you want to capture stack traces from.
*
* @param storageOrThreadName Either the name of the thread, or an object containing an AsyncLocalStorage instance and optional storage key.
* @param threadName The name of the thread, if the first argument is an object.
*
* threadName defaults to the `threadId` if not provided.
*/
export function registerThread(storageOrThreadName?: AsyncStorageArgs | string, threadName?: string): void {
if (typeof storageOrThreadName === 'object') {
native.registerThread(storageOrThreadName, threadName || String(threadId));
} else {
native.registerThread(storageOrThreadName || String(threadId));
}
}
/**
* Tells the native module that the thread is still running and updates the state.
*
* @param enableLastSeen If true, enables the last seen tracking for this thread.
*/
export function threadPoll(enableLastSeen: boolean = true, pollState?: object): void {
native.threadPoll(enableLastSeen, pollState);
}
/**
* Captures stack traces for all registered threads.
*/
export function captureStackTrace<A = unknown, P = unknown>(): Record<string, Thread<A, P>> {
return native.captureStackTrace<A, P>();
}
/**
* Returns the number of milliseconds since the last time each thread was seen.
*
* This is useful for determining if a threads event loop has been blocked for a long time.
*/
export function getThreadsLastSeen(): Record<string, number> {
return native.getThreadsLastSeen();
}