11import type { ChildProcess } from 'node:child_process' ;
22import * as diagnosticsChannel from 'node:diagnostics_channel' ;
33import type { Worker } from 'node:worker_threads' ;
4- import { addBreadcrumb , defineIntegration } from '@sentry/core' ;
4+ import { addBreadcrumb , captureException , defineIntegration } from '@sentry/core' ;
55
66interface Options {
77 /**
@@ -10,6 +10,13 @@ interface Options {
1010 * @default false
1111 */
1212 includeChildProcessArgs ?: boolean ;
13+
14+ /**
15+ * Whether to capture errors from worker threads.
16+ *
17+ * @default true
18+ */
19+ captureWorkerErrors ?: boolean ;
1320}
1421
1522const INTEGRATION_NAME = 'ChildProcess' ;
@@ -29,7 +36,7 @@ export const childProcessIntegration = defineIntegration((options: Options = {})
2936
3037 diagnosticsChannel . channel ( 'worker_threads' ) . subscribe ( ( event : unknown ) => {
3138 if ( event && typeof event === 'object' && 'worker' in event ) {
32- captureWorkerThreadEvents ( event . worker as Worker ) ;
39+ captureWorkerThreadEvents ( event . worker as Worker , options ) ;
3340 }
3441 } ) ;
3542 } ,
@@ -82,19 +89,25 @@ function captureChildProcessEvents(child: ChildProcess, options: Options): void
8289 } ) ;
8390}
8491
85- function captureWorkerThreadEvents ( worker : Worker ) : void {
92+ function captureWorkerThreadEvents ( worker : Worker , options : Options ) : void {
8693 let threadId : number | undefined ;
8794
8895 worker
8996 . on ( 'online' , ( ) => {
9097 threadId = worker . threadId ;
9198 } )
9299 . on ( 'error' , error => {
93- addBreadcrumb ( {
94- category : 'worker_thread' ,
95- message : `Worker thread errored with '${ error . message } '` ,
96- level : 'error' ,
97- data : { threadId } ,
98- } ) ;
100+ if ( options . captureWorkerErrors !== false ) {
101+ captureException ( error , {
102+ mechanism : { type : 'instrument' , 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+ }
99112 } ) ;
100113}
0 commit comments