Skip to content

feat(node): Capture exceptions from worker_threads #15105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const __dirname = new URL('.', import.meta.url).pathname;
Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
release: '1.0',
integrations: [Sentry.childProcessIntegration({ captureWorkerErrors: false })],
transport: loggingTransport,
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
setTimeout(() => {
throw new Error('Test error');
}, 1000);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
setTimeout(() => {
throw new Error('Test error');
}, 1000);
18 changes: 18 additions & 0 deletions dev-packages/node-integration-tests/suites/child-process/fork.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const Sentry = require('@sentry/node');
const { loggingTransport } = require('@sentry-internal/node-integration-tests');
const path = require('path');
const { fork } = require('child_process');

Sentry.init({
debug: true,
dsn: 'https://public@dsn.ingest.sentry.io/1337',
release: '1.0',
transport: loggingTransport,
});

// eslint-disable-next-line no-unused-vars
const _child = fork(path.join(__dirname, 'child.mjs'));

setTimeout(() => {
throw new Error('Exiting main process');
}, 3000);
19 changes: 19 additions & 0 deletions dev-packages/node-integration-tests/suites/child-process/fork.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { fork } from 'child_process';
import * as path from 'path';
import { loggingTransport } from '@sentry-internal/node-integration-tests';
import * as Sentry from '@sentry/node';

const __dirname = new URL('.', import.meta.url).pathname;

Sentry.init({
debug: true,
dsn: 'https://public@dsn.ingest.sentry.io/1337',
release: '1.0',
transport: loggingTransport,
});

const _child = fork(path.join(__dirname, 'child.mjs'));

setTimeout(() => {
throw new Error('Exiting main process');
}, 3000);
65 changes: 65 additions & 0 deletions dev-packages/node-integration-tests/suites/child-process/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import type { Event } from '@sentry/core';
import { conditionalTest } from '../../utils';
import { cleanupChildProcesses, createRunner } from '../../utils/runner';

const WORKER_EVENT: Event = {
exception: {
values: [
{
type: 'Error',
value: 'Test error',
mechanism: {
type: 'instrument',
handled: false,
data: {
threadId: expect.any(String),
},
},
},
],
},
};

const CHILD_EVENT: Event = {
exception: {
values: [
{
type: 'Error',
value: 'Exiting main process',
},
],
},
breadcrumbs: [
{
category: 'child_process',
message: "Child process exited with code '1'",
level: 'warning',
},
],
};

describe('should capture child process events', () => {
afterAll(() => {
cleanupChildProcesses();
});

conditionalTest({ min: 20 })('worker', () => {
test('ESM', done => {
createRunner(__dirname, 'worker.mjs').expect({ event: WORKER_EVENT }).start(done);
});

test('CJS', done => {
createRunner(__dirname, 'worker.js').expect({ event: WORKER_EVENT }).start(done);
});
});

conditionalTest({ min: 20 })('fork', () => {
test('ESM', done => {
createRunner(__dirname, 'fork.mjs').expect({ event: CHILD_EVENT }).start(done);
});

test('CJS', done => {
createRunner(__dirname, 'fork.js').expect({ event: CHILD_EVENT }).start(done);
});
});
});
18 changes: 18 additions & 0 deletions dev-packages/node-integration-tests/suites/child-process/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const Sentry = require('@sentry/node');
const { loggingTransport } = require('@sentry-internal/node-integration-tests');
const path = require('path');
const { Worker } = require('worker_threads');

Sentry.init({
debug: true,
dsn: 'https://public@dsn.ingest.sentry.io/1337',
release: '1.0',
transport: loggingTransport,
});

// eslint-disable-next-line no-unused-vars
const _worker = new Worker(path.join(__dirname, 'child.js'));

setTimeout(() => {
process.exit();
}, 3000);
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as path from 'path';
import { loggingTransport } from '@sentry-internal/node-integration-tests';
import * as Sentry from '@sentry/node';
import { Worker } from 'worker_threads';

const __dirname = new URL('.', import.meta.url).pathname;

Sentry.init({
debug: true,
dsn: 'https://public@dsn.ingest.sentry.io/1337',
release: '1.0',
transport: loggingTransport,
});

const _worker = new Worker(path.join(__dirname, 'child.mjs'));

setTimeout(() => {
process.exit();
}, 3000);
37 changes: 25 additions & 12 deletions packages/node/src/integrations/childProcess.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ChildProcess } from 'node:child_process';
import * as diagnosticsChannel from 'node:diagnostics_channel';
import type { Worker } from 'node:worker_threads';
import { addBreadcrumb, defineIntegration } from '@sentry/core';
import { addBreadcrumb, captureException, defineIntegration } from '@sentry/core';

interface Options {
/**
Expand All @@ -10,17 +10,24 @@ interface Options {
* @default false
*/
includeChildProcessArgs?: boolean;

/**
* Whether to capture errors from worker threads.
*
* @default true
*/
captureWorkerErrors?: boolean;
}

const INTEGRATION_NAME = 'ChildProcess';

/**
* Capture breadcrumbs for child processes and worker threads.
* Capture breadcrumbs and events for child processes and worker threads.
*/
export const childProcessIntegration = defineIntegration((options: Options = {}) => {
return {
name: INTEGRATION_NAME,
setup(_client) {
setup() {
diagnosticsChannel.channel('child_process').subscribe((event: unknown) => {
if (event && typeof event === 'object' && 'process' in event) {
captureChildProcessEvents(event.process as ChildProcess, options);
Expand All @@ -29,7 +36,7 @@ export const childProcessIntegration = defineIntegration((options: Options = {})

diagnosticsChannel.channel('worker_threads').subscribe((event: unknown) => {
if (event && typeof event === 'object' && 'worker' in event) {
captureWorkerThreadEvents(event.worker as Worker);
captureWorkerThreadEvents(event.worker as Worker, options);
}
});
},
Expand Down Expand Up @@ -62,7 +69,7 @@ function captureChildProcessEvents(child: ChildProcess, options: Options): void
addBreadcrumb({
category: 'child_process',
message: `Child process exited with code '${code}'`,
level: 'warning',
level: code === 0 ? 'info' : 'warning',
data,
});
}
Expand All @@ -82,19 +89,25 @@ function captureChildProcessEvents(child: ChildProcess, options: Options): void
});
}

function captureWorkerThreadEvents(worker: Worker): void {
function captureWorkerThreadEvents(worker: Worker, options: Options): void {
let threadId: number | undefined;

worker
.on('online', () => {
threadId = worker.threadId;
})
.on('error', error => {
addBreadcrumb({
category: 'worker_thread',
message: `Worker thread errored with '${error.message}'`,
level: 'error',
data: { threadId },
});
if (options.captureWorkerErrors !== false) {
captureException(error, {
mechanism: { type: 'instrument', handled: false, data: { threadId: String(threadId) } },
});
} else {
addBreadcrumb({
category: 'worker_thread',
message: `Worker thread errored with '${error.message}'`,
level: 'error',
data: { threadId },
});
}
});
}
Loading