1
1
'use strict'
2
2
3
- const {
4
- channel,
5
- addHook,
6
- AsyncResource
7
- } = require ( './helpers/instrument' )
3
+ const { channel, addHook } = require ( './helpers/instrument' )
8
4
const shimmer = require ( '../../datadog-shimmer' )
9
5
10
6
const startChannel = channel ( 'apm:fs:operation:start' )
@@ -191,25 +187,23 @@ function wrapCreateStream (original) {
191
187
return function ( path , options ) {
192
188
if ( ! startChannel . hasSubscribers ) return original . apply ( this , arguments )
193
189
194
- const innerResource = new AsyncResource ( 'bound-anonymous-fn' )
195
- const message = getMessage ( name , [ 'path' , 'options' ] , arguments )
196
-
197
- return innerResource . runInAsyncScope ( ( ) => {
198
- startChannel . publish ( message )
190
+ const ctx = getMessage ( name , [ 'path' , 'options' ] , arguments )
199
191
192
+ return startChannel . runStores ( ctx , ( ) => {
200
193
try {
201
194
const stream = original . apply ( this , arguments )
202
- const onError = innerResource . bind ( error => {
203
- errorChannel . publish ( error )
195
+ const onError = error => {
196
+ ctx . error = error
197
+ errorChannel . publish ( ctx )
204
198
onFinish ( )
205
- } )
206
- const onFinish = innerResource . bind ( ( ) => {
207
- finishChannel . publish ( )
199
+ }
200
+ const onFinish = ( ) => {
201
+ finishChannel . runStores ( ctx , ( ) => { } )
208
202
stream . off ( 'close' , onFinish )
209
203
stream . off ( 'end' , onFinish )
210
204
stream . off ( 'finish' , onFinish )
211
205
stream . off ( 'error' , onError )
212
- } )
206
+ }
213
207
214
208
stream . once ( 'close' , onFinish )
215
209
stream . once ( 'end' , onFinish )
@@ -218,8 +212,9 @@ function wrapCreateStream (original) {
218
212
219
213
return stream
220
214
} catch ( error ) {
221
- errorChannel . publish ( error )
222
- finishChannel . publish ( )
215
+ ctx . error = error
216
+ errorChannel . publish ( ctx )
217
+ finishChannel . runStores ( ctx , ( ) => { } )
223
218
}
224
219
} )
225
220
}
@@ -239,17 +234,16 @@ function createWatchWrapFunction (override = '') {
239
234
const operation = name
240
235
return function ( ) {
241
236
if ( ! startChannel . hasSubscribers ) return original . apply ( this , arguments )
242
- const message = getMessage ( method , watchMethods [ operation ] , arguments , this )
243
- const innerResource = new AsyncResource ( 'bound-anonymous-fn' )
244
- return innerResource . runInAsyncScope ( ( ) => {
245
- startChannel . publish ( message )
237
+ const ctx = getMessage ( method , watchMethods [ operation ] , arguments , this )
238
+ return startChannel . runStores ( ctx , ( ) => {
246
239
try {
247
240
const result = original . apply ( this , arguments )
248
- finishChannel . publish ( )
241
+ finishChannel . runStores ( ctx , ( ) => { } )
249
242
return result
250
243
} catch ( error ) {
251
- errorChannel . publish ( error )
252
- finishChannel . publish ( )
244
+ ctx . error = error
245
+ errorChannel . publish ( ctx )
246
+ finishChannel . runStores ( ctx , ( ) => { } )
253
247
throw error
254
248
}
255
249
} )
@@ -268,30 +262,25 @@ function createWrapFunction (prefix = '', override = '') {
268
262
269
263
const lastIndex = arguments . length - 1
270
264
const cb = typeof arguments [ lastIndex ] === 'function' && arguments [ lastIndex ]
271
- const innerResource = new AsyncResource ( 'bound-anonymous-fn' )
272
265
const params = getMethodParamsRelationByPrefix ( prefix ) [ operation ]
273
266
const abortController = new AbortController ( )
274
- const message = { ...getMessage ( method , params , arguments , this ) , abortController }
267
+ const ctx = { ...getMessage ( method , params , arguments , this ) , abortController }
275
268
276
- const finish = innerResource . bind ( function ( error ) {
269
+ const finish = function ( error , cb = ( ) => { } ) {
277
270
if ( error !== null && typeof error === 'object' ) { // fs.exists receives a boolean
278
- errorChannel . publish ( error )
271
+ ctx . error = error
272
+ errorChannel . publish ( ctx )
279
273
}
280
- finishChannel . publish ( )
281
- } )
274
+ return finishChannel . runStores ( ctx , cb )
275
+ }
282
276
283
277
if ( cb ) {
284
- const outerResource = new AsyncResource ( 'bound-anonymous-fn' )
285
-
286
- arguments [ lastIndex ] = shimmer . wrapFunction ( cb , cb => innerResource . bind ( function ( e ) {
287
- finish ( e )
288
- return outerResource . runInAsyncScope ( ( ) => cb . apply ( this , arguments ) )
289
- } ) )
278
+ arguments [ lastIndex ] = shimmer . wrapFunction ( cb , cb => function ( e ) {
279
+ return finish ( e , ( ) => cb . apply ( this , arguments ) )
280
+ } )
290
281
}
291
282
292
- return innerResource . runInAsyncScope ( ( ) => {
293
- startChannel . publish ( message )
294
-
283
+ return startChannel . runStores ( ctx , ( ) => {
295
284
if ( abortController . signal . aborted ) {
296
285
const error = abortController . signal . reason || new Error ( 'Aborted' )
297
286
@@ -318,23 +307,25 @@ function createWrapFunction (prefix = '', override = '') {
318
307
if ( isFirstMethodReturningFileHandle ( original ) ) {
319
308
wrapFileHandle ( value )
320
309
}
321
- finishChannel . publish ( )
310
+ finishChannel . runStores ( ctx , ( ) => { } )
322
311
return value
323
312
} ,
324
313
error => {
325
- errorChannel . publish ( error )
326
- finishChannel . publish ( )
314
+ ctx . error = error
315
+ errorChannel . publish ( ctx )
316
+ finishChannel . runStores ( ctx , ( ) => { } )
327
317
throw error
328
318
}
329
319
)
330
320
}
331
321
332
- finishChannel . publish ( )
322
+ finishChannel . runStores ( ctx , ( ) => { } )
333
323
334
324
return result
335
325
} catch ( error ) {
336
- errorChannel . publish ( error )
337
- finishChannel . publish ( )
326
+ ctx . error = error
327
+ errorChannel . publish ( ctx )
328
+ finishChannel . runStores ( ctx , ( ) => { } )
338
329
throw error
339
330
}
340
331
} )
0 commit comments