Skip to content

Commit 966c1dd

Browse files
timfishmsonnb
authored andcommitted
feat(node-native): Add option to disable event loop blocked detection (#16919)
This PR updates `@sentry-internal/node-native-stacktrace` to v0.2.0 which supports disabling last time tracking per thread. The integration needed quite a few changes and I split it into more functions but all the tests still pass! This PR then adds `pauseEventLoopBlockDetection()`, `restartEventLoopBlockDetection()` which can be used to pause and restart detection and `disableBlockDetectionForCallback` which can be used like this: ```ts import { disableBlockDetectionForCallback } from '@sentry/node-native'; disableBlockDetectionForCallback(() => { someLongBlockingFn(); }); ```
1 parent d13a4c4 commit 966c1dd

File tree

8 files changed

+252
-90
lines changed

8 files changed

+252
-90
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import * as Sentry from '@sentry/node';
2+
import { disableBlockDetectionForCallback, eventLoopBlockIntegration } from '@sentry/node-native';
3+
import { longWork, longWorkOther } from './long-work.js';
4+
5+
global._sentryDebugIds = { [new Error().stack]: 'aaaaaaaa-aaaa-4aaa-aaaa-aaaaaaaaaa' };
6+
7+
setTimeout(() => {
8+
process.exit();
9+
}, 15000);
10+
11+
Sentry.init({
12+
debug: true,
13+
dsn: process.env.SENTRY_DSN,
14+
release: '1.0',
15+
integrations: [eventLoopBlockIntegration()],
16+
});
17+
18+
setTimeout(() => {
19+
disableBlockDetectionForCallback(() => {
20+
// This wont be captured
21+
longWork();
22+
});
23+
24+
setTimeout(() => {
25+
// But this will be captured
26+
longWorkOther();
27+
}, 2000);
28+
}, 2000);
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
const crypto = require('crypto');
22
const assert = require('assert');
33

4-
function longWork() {
4+
function longWork(count = 100) {
5+
for (let i = 0; i < count; i++) {
6+
const salt = crypto.randomBytes(128).toString('base64');
7+
const hash = crypto.pbkdf2Sync('myPassword', salt, 10000, 512, 'sha512');
8+
assert.ok(hash);
9+
}
10+
}
11+
12+
function longWorkOther() {
513
for (let i = 0; i < 200; i++) {
614
const salt = crypto.randomBytes(128).toString('base64');
715
const hash = crypto.pbkdf2Sync('myPassword', salt, 10000, 512, 'sha512');
@@ -10,3 +18,4 @@ function longWork() {
1018
}
1119

1220
exports.longWork = longWork;
21+
exports.longWorkOther = longWorkOther;

dev-packages/node-integration-tests/suites/thread-blocked-native/test.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { Event } from '@sentry/core';
33
import { afterAll, describe, expect, test } from 'vitest';
44
import { cleanupChildProcesses, createRunner } from '../../utils/runner';
55

6-
function EXCEPTION(thread_id = '0') {
6+
function EXCEPTION(thread_id = '0', fn = 'longWork') {
77
return {
88
values: [
99
{
@@ -24,7 +24,7 @@ function EXCEPTION(thread_id = '0') {
2424
colno: expect.any(Number),
2525
lineno: expect.any(Number),
2626
filename: expect.any(String),
27-
function: 'longWork',
27+
function: fn,
2828
in_app: true,
2929
}),
3030
]),
@@ -155,6 +155,19 @@ describe('Thread Blocked Native', { timeout: 30_000 }, () => {
155155
expect(runner.childHasExited()).toBe(true);
156156
});
157157

158+
test('can be disabled with disableBlockDetectionForCallback', async () => {
159+
await createRunner(__dirname, 'basic-disabled.mjs')
160+
.withMockSentryServer()
161+
.expect({
162+
event: {
163+
...ANR_EVENT,
164+
exception: EXCEPTION('0', 'longWorkOther'),
165+
},
166+
})
167+
.start()
168+
.completed();
169+
});
170+
158171
test('worker thread', async () => {
159172
const instrument = join(__dirname, 'instrument.mjs');
160173
await createRunner(__dirname, 'worker-main.mjs')

packages/node-native/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"build:tarball": "npm pack"
6464
},
6565
"dependencies": {
66-
"@sentry-internal/node-native-stacktrace": "^0.1.0",
66+
"@sentry-internal/node-native-stacktrace": "^0.2.0",
6767
"@sentry/core": "9.38.0",
6868
"@sentry/node": "9.38.0"
6969
},

0 commit comments

Comments
 (0)