Skip to content

Commit 92a8a25

Browse files
committed
feat: split childProcess integration into childProcess and worker integrations
The childProcess integration handled both child process and worker thread events under one integration. This splits it into two: - childProcessIntegration: handles child process events only - workerIntegration: handles worker thread events only (new) The old childProcessIntegration's captureWorkerErrors option is deprecated. Users should use workerIntegration() instead. Fixes #18698 Signed-off-by: Atharv Pandey <atharvpandey245@gmail.com>
1 parent 61b7d52 commit 92a8a25

8 files changed

Lines changed: 65 additions & 41 deletions

File tree

dev-packages/node-integration-tests/suites/breadcrumbs/process-thread/app.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Sentry.init({
1010
traceLifecycle: 'static',
1111
dsn: 'https://public@dsn.ingest.sentry.io/1337',
1212
release: '1.0',
13-
integrations: [Sentry.childProcessIntegration({ captureWorkerErrors: false })],
13+
integrations: [Sentry.childProcessIntegration(), Sentry.workerIntegration({ captureWorkerErrors: false })],
1414
transport: loggingTransport,
1515
});
1616

dev-packages/node-integration-tests/suites/child-process/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const WORKER_EVENT: Event = {
1010
type: 'Error',
1111
value: 'Test error',
1212
mechanism: {
13-
type: 'auto.child_process.worker_thread',
13+
type: 'auto.worker_thread',
1414
handled: false,
1515
data: {
1616
threadId: expect.any(String),

packages/aws-serverless/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ export {
120120
processSessionIntegration,
121121
prismaIntegration,
122122
childProcessIntegration,
123+
workerIntegration,
123124
createSentryWinstonTransport,
124125
hapiIntegration,
125126
setupHapiErrorHandler,

packages/google-cloud-serverless/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ export {
142142
anthropicAIIntegration,
143143
googleGenAIIntegration,
144144
childProcessIntegration,
145+
workerIntegration,
145146
createSentryWinstonTransport,
146147
vercelAIIntegration,
147148
logger,

packages/node/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ export { metrics, withStreamedSpan } from '@sentry/core';
178178
export * as logger from './logs/exports';
179179

180180
export { childProcessIntegration } from './integrations/childProcess';
181+
export { workerIntegration } from './integrations/workerIntegration';
181182
export { consoleIntegration } from './integrations/console';
182183
export { nodeContextIntegration } from './integrations/context';
183184
export { contextLinesIntegration } from './integrations/contextlines';
Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { ChildProcess } from 'node:child_process';
22
import * as diagnosticsChannel from 'node:diagnostics_channel';
3-
import type { Worker } from 'node:worker_threads';
4-
import { addBreadcrumb, captureException, defineIntegration, isObjectLike } from '@sentry/core';
3+
import { addBreadcrumb, defineIntegration, isObjectLike } from '@sentry/core';
54

65
interface Options {
76
/**
@@ -10,19 +9,14 @@ interface Options {
109
* @default false
1110
*/
1211
includeChildProcessArgs?: boolean;
13-
14-
/**
15-
* Whether to capture errors from worker threads.
16-
*
17-
* @default true
18-
*/
19-
captureWorkerErrors?: boolean;
2012
}
2113

2214
const INTEGRATION_NAME = 'ChildProcess' as const;
2315

2416
/**
25-
* Capture breadcrumbs and events for child processes and worker threads.
17+
* Capture breadcrumbs and events for child processes.
18+
*
19+
* For worker thread events, use `workerIntegration()` instead.
2620
*/
2721
export const childProcessIntegration = defineIntegration((options: Options = {}) => {
2822
return {
@@ -33,12 +27,6 @@ export const childProcessIntegration = defineIntegration((options: Options = {})
3327
captureChildProcessEvents(event.process as ChildProcess, options);
3428
}
3529
});
36-
37-
diagnosticsChannel.channel('worker_threads').subscribe((event: unknown) => {
38-
if (isObjectLike(event) && 'worker' in event) {
39-
captureWorkerThreadEvents(event.worker as Worker, options);
40-
}
41-
});
4230
},
4331
};
4432
});
@@ -69,7 +57,7 @@ function captureChildProcessEvents(child: ChildProcess, options: Options): void
6957
addBreadcrumb({
7058
category: 'child_process',
7159
message: `Child process exited with code '${code}'`,
72-
level: code === 0 ? 'info' : 'warning',
60+
level: 'warning',
7361
data,
7462
});
7563
}
@@ -89,25 +77,3 @@ function captureChildProcessEvents(child: ChildProcess, options: Options): void
8977
});
9078
}
9179

92-
function captureWorkerThreadEvents(worker: Worker, options: Options): void {
93-
let threadId: number | undefined;
94-
95-
worker
96-
.on('online', () => {
97-
threadId = worker.threadId;
98-
})
99-
.on('error', error => {
100-
if (options.captureWorkerErrors !== false) {
101-
captureException(error, {
102-
mechanism: { type: 'auto.child_process.worker_thread', handled: false, data: { threadId: String(threadId) } },
103-
});
104-
} else {
105-
addBreadcrumb({
106-
category: 'worker_thread',
107-
message: `Worker thread errored with '${error.message}'`,
108-
level: 'error',
109-
data: { threadId },
110-
});
111-
}
112-
});
113-
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import type { Worker } from 'node:worker_threads';
2+
import * as diagnosticsChannel from 'node:diagnostics_channel';
3+
import { addBreadcrumb, captureException, defineIntegration, isObjectLike } from '@sentry/core';
4+
5+
interface Options {
6+
/**
7+
* Whether to capture errors from worker threads.
8+
*
9+
* @default true
10+
*/
11+
captureWorkerErrors?: boolean;
12+
}
13+
14+
const INTEGRATION_NAME = 'Worker' as const;
15+
16+
/**
17+
* Capture breadcrumbs and events for worker threads.
18+
*/
19+
export const workerIntegration = defineIntegration((options: Options = {}) => {
20+
return {
21+
name: INTEGRATION_NAME,
22+
setup() {
23+
diagnosticsChannel.channel('worker_threads').subscribe((event: unknown) => {
24+
if (isObjectLike(event) && 'worker' in event) {
25+
captureWorkerThreadEvents(event.worker as Worker, options);
26+
}
27+
});
28+
},
29+
};
30+
});
31+
32+
function captureWorkerThreadEvents(worker: Worker, options: Options): void {
33+
let threadId: number | undefined;
34+
35+
worker
36+
.on('online', () => {
37+
threadId = worker.threadId;
38+
})
39+
.on('error', error => {
40+
if (options.captureWorkerErrors !== false) {
41+
captureException(error, {
42+
mechanism: { type: 'auto.worker_thread', handled: false, data: { threadId: threadId !== undefined ? String(threadId) : undefined } },
43+
});
44+
} else {
45+
addBreadcrumb({
46+
category: 'worker_thread',
47+
message: `Worker thread errored with '${error.message}'`,
48+
level: 'error',
49+
data: { threadId },
50+
});
51+
}
52+
});
53+
}

packages/node/src/sdk/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { detectOrchestrionSetup } from '@sentry/server-utils/orchestrion';
2121
import { registerDiagnosticsChannelInjection } from '@sentry/server-utils/orchestrion/register';
2222
import { DEBUG_BUILD } from '../debug-build';
2323
import { childProcessIntegration } from '../integrations/childProcess';
24+
import { workerIntegration } from '../integrations/workerIntegration';
2425
import { consoleIntegration } from '../integrations/console';
2526
import { nodeContextIntegration } from '../integrations/context';
2627
import { contextLinesIntegration } from '../integrations/contextlines';
@@ -68,6 +69,7 @@ function getBaseDefaultIntegrations(): Integration[] {
6869
localVariablesIntegration(),
6970
nodeContextIntegration(),
7071
childProcessIntegration(),
72+
workerIntegration(),
7173
processSessionIntegration(),
7274
modulesIntegration(),
7375
];

0 commit comments

Comments
 (0)