1
- import { types } from 'node:util' ;
2
1
import { Worker } from 'node:worker_threads' ;
3
- import type { Contexts , Event , EventHint , Integration , IntegrationFn } from '@sentry/core' ;
4
- import { defineIntegration , getClient , getFilenameToDebugIdMap , getIsolationScope , logger } from '@sentry/core' ;
2
+ import type { Contexts , Event , EventHint , IntegrationFn } from '@sentry/core' ;
3
+ import { defineIntegration , getFilenameToDebugIdMap , getIsolationScope , logger } from '@sentry/core' ;
5
4
import type { NodeClient } from '@sentry/node' ;
6
5
import { registerThread , threadPoll } from '@sentry-internal/node-native-stacktrace' ;
7
6
import type { ThreadBlockedIntegrationOptions , WorkerStartData } from './common' ;
8
7
import { POLL_RATIO } from './common' ;
9
8
10
- const { isPromise } = types ;
11
-
12
9
const DEFAULT_THRESHOLD_MS = 1_000 ;
13
10
14
11
function log ( message : string , ...args : unknown [ ] ) : void {
@@ -32,43 +29,18 @@ async function getContexts(client: NodeClient): Promise<Contexts> {
32
29
33
30
const INTEGRATION_NAME = 'ThreadBlocked' ;
34
31
35
- type ThreadBlockedInternal = { start : ( ) => void ; stop : ( ) => void } ;
36
-
37
32
const _eventLoopBlockIntegration = ( ( options : Partial < ThreadBlockedIntegrationOptions > = { } ) => {
38
- let worker : Promise < ( ) => void > | undefined ;
39
- let client : NodeClient | undefined ;
40
-
41
33
return {
42
34
name : INTEGRATION_NAME ,
43
- start : ( ) => {
44
- if ( worker ) {
45
- return ;
46
- }
47
-
48
- if ( client ) {
49
- worker = _startWorker ( client , options ) ;
50
- }
51
- } ,
52
- stop : ( ) => {
53
- if ( worker ) {
54
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
55
- worker . then ( stop => {
56
- stop ( ) ;
57
- worker = undefined ;
58
- } ) ;
59
- }
60
- } ,
61
- afterAllSetup ( initClient : NodeClient ) {
62
- client = initClient ;
63
-
35
+ afterAllSetup ( client : NodeClient ) {
64
36
registerThread ( ) ;
65
- this . start ( ) ;
37
+ _startWorker ( client , options ) . catch ( err => {
38
+ log ( 'Failed to start event loop block worker' , err ) ;
39
+ } ) ;
66
40
} ,
67
- } as Integration & ThreadBlockedInternal ;
41
+ } ;
68
42
} ) satisfies IntegrationFn ;
69
43
70
- type ThreadBlockedReturn = ( options ?: Partial < ThreadBlockedIntegrationOptions > ) => Integration & ThreadBlockedInternal ;
71
-
72
44
/**
73
45
* Monitors the Node.js event loop for blocking behavior and reports blocked events to Sentry.
74
46
*
@@ -98,7 +70,7 @@ type ThreadBlockedReturn = (options?: Partial<ThreadBlockedIntegrationOptions>)
98
70
* node --import instrument.mjs app.mjs
99
71
* ```
100
72
*/
101
- export const eventLoopBlockIntegration = defineIntegration ( _eventLoopBlockIntegration ) as ThreadBlockedReturn ;
73
+ export const eventLoopBlockIntegration = defineIntegration ( _eventLoopBlockIntegration ) ;
102
74
103
75
/**
104
76
* Starts the worker thread
@@ -193,29 +165,3 @@ async function _startWorker(
193
165
clearInterval ( timer ) ;
194
166
} ;
195
167
}
196
-
197
- export function disableBlockedDetectionForCallback < T > ( callback : ( ) => T ) : T ;
198
- export function disableBlockedDetectionForCallback < T > ( callback : ( ) => Promise < T > ) : Promise < T > ;
199
- /**
200
- * Disables blocked detection for the duration of the callback
201
- */
202
- export function disableBlockedDetectionForCallback < T > ( callback : ( ) => T | Promise < T > ) : T | Promise < T > {
203
- const integration = getClient ( ) ?. getIntegrationByName ( INTEGRATION_NAME ) as ThreadBlockedInternal | undefined ;
204
-
205
- if ( ! integration ) {
206
- return callback ( ) ;
207
- }
208
-
209
- integration . stop ( ) ;
210
-
211
- const result = callback ( ) ;
212
- if ( isPromise ( result ) ) {
213
- return result . finally ( ( ) => integration . start ( ) ) ;
214
- }
215
-
216
- try {
217
- return result ;
218
- } finally {
219
- integration . start ( ) ;
220
- }
221
- }
0 commit comments