Skip to content

Conversation

timfish
Copy link
Collaborator

@timfish timfish commented Jun 23, 2025

This PR:

  • Adds eventLoopBlockIntegration which uses @sentry-internal/node-native-stacktrace to detect and capture blocked event loops
  • Adds suite of integration tests to test all the functionality
  • Bundling
    • Unlike the existing ANR integration, we can't bundle the worker code into a base64 string because there is a native module and all threads need to load the same native module
    • The worker is loaded via new Worker(new URL('./thread-blocked-watchdog.js', import.meta.url)) which will be bundled correctly by latest Vite and Webpack 5
    • For other bundlers you can add an extra entry point pointing at @sentry/node-native/thread-blocked-watchdog which should output tothread-blocked-watchdog.js next to your bundle.

Usage

If you instrument your application via the Node.js --import flag, this instrumentation will be automatically applied to all worker threads.

instrument.mjs

import * as Sentry from '@sentry/node';
import { eventLoopBlockIntegration } from '@sentry/node-native';

Sentry.init({
  dsn: '__YOUR_DSN__',
  // Capture stack traces when the event loop is blocked for more than 500ms
  integrations: [eventLoopBlockIntegration({ 
    threshold: 500, // defaults to 1000 ms 
    maxEventsPerHour: 5,  // defaults to 1
  })],
});

app.mjs

import { Worker } from 'worker_threads';

const worker = new Worker(new URL('./worker.mjs', import.meta.url));

// This main thread will be monitored for blocked event loops

worker.mjs

// This worker thread will also be monitored for blocked event loops too

setTimeout(() => {
  longWork();
}, 2_000);

Start your application:

node --import instrument.mjs app.mjs

Blocked events are captured with stack traces for all threads:

image

@timfish timfish force-pushed the timfish/feat/new-anr branch from 1419bd9 to b49f3f5 Compare June 23, 2025 21:13
@timfish timfish force-pushed the timfish/feat/new-anr branch from f337741 to 0745a20 Compare June 24, 2025 11:49
@timfish timfish marked this pull request as ready for review June 24, 2025 12:34
@timfish timfish requested a review from AbhiPrasad June 24, 2025 12:40
Copy link
Member

@AbhiPrasad AbhiPrasad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's split this PR up

  1. Adding just the @sentry/node-native package with no implementation. Same approach as #16652
  2. the integration tests + main implementation (this PR)

AbhiPrasad pushed a commit that referenced this pull request Jun 24, 2025
- Ref
#16709 (review)

Adds an empty `@sentry/node-native` package
@AbhiPrasad
Copy link
Member

@sentry review

@timfish timfish changed the title feat(node): Add @sentry/node-native with threadBlockedIntegration feat(node): Add eventLoopBlockIntegration Jun 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants