@@ -209,7 +209,16 @@ export class HttpPlugin extends BasePlugin<Http> {
209
209
}
210
210
211
211
if ( this . _config . applyCustomAttributesOnSpan ) {
212
- this . _config . applyCustomAttributesOnSpan ( span , request , response ) ;
212
+ this . _safeExecute (
213
+ span ,
214
+ ( ) =>
215
+ this . _config . applyCustomAttributesOnSpan ! (
216
+ span ,
217
+ request ,
218
+ response
219
+ ) ,
220
+ false
221
+ ) ;
213
222
}
214
223
215
224
span . end ( ) ;
@@ -248,7 +257,14 @@ export class HttpPlugin extends BasePlugin<Http> {
248
257
249
258
plugin . _logger . debug ( '%s plugin incomingRequest' , plugin . moduleName ) ;
250
259
251
- if ( Utils . isIgnored ( pathname , plugin . _config . ignoreIncomingPaths ) ) {
260
+ if (
261
+ Utils . isIgnored (
262
+ pathname ,
263
+ plugin . _config . ignoreIncomingPaths ,
264
+ ( e : Error ) =>
265
+ plugin . _logger . error ( 'caught ignoreIncomingPaths error: ' , e )
266
+ )
267
+ ) {
252
268
return original . apply ( this , [ event , ...args ] ) ;
253
269
}
254
270
@@ -279,9 +295,11 @@ export class HttpPlugin extends BasePlugin<Http> {
279
295
response . end = originalEnd ;
280
296
// Cannot pass args of type ResponseEndArgs,
281
297
// tslint complains "Expected 1-2 arguments, but got 1 or more.", it does not make sense to me
282
- // tslint:disable-next-line:no-any
283
- const returned = plugin . _safeExecute ( span , ( ) =>
284
- response . end . apply ( this , arguments as any )
298
+ const returned = plugin . _safeExecute (
299
+ span ,
300
+ // tslint:disable-next-line:no-any
301
+ ( ) => response . end . apply ( this , arguments as any ) ,
302
+ true
285
303
) ;
286
304
const requestUrl = request . url ? url . parse ( request . url ) : null ;
287
305
const hostname = headers . host
@@ -315,15 +333,26 @@ export class HttpPlugin extends BasePlugin<Http> {
315
333
. setStatus ( Utils . parseResponseStatus ( response . statusCode ) ) ;
316
334
317
335
if ( plugin . _config . applyCustomAttributesOnSpan ) {
318
- plugin . _config . applyCustomAttributesOnSpan ( span , request , response ) ;
336
+ plugin . _safeExecute (
337
+ span ,
338
+ ( ) =>
339
+ plugin . _config . applyCustomAttributesOnSpan ! (
340
+ span ,
341
+ request ,
342
+ response
343
+ ) ,
344
+ false
345
+ ) ;
319
346
}
320
347
321
348
span . end ( ) ;
322
349
return returned ;
323
350
} ;
324
351
325
- return plugin . _safeExecute ( span , ( ) =>
326
- original . apply ( this , [ event , ...args ] )
352
+ return plugin . _safeExecute (
353
+ span ,
354
+ ( ) => original . apply ( this , [ event , ...args ] ) ,
355
+ true
327
356
) ;
328
357
} ) ;
329
358
} ;
@@ -353,7 +382,12 @@ export class HttpPlugin extends BasePlugin<Http> {
353
382
354
383
if (
355
384
Utils . isOpenTelemetryRequest ( options ) ||
356
- Utils . isIgnored ( origin + pathname , plugin . _config . ignoreOutgoingUrls )
385
+ Utils . isIgnored (
386
+ origin + pathname ,
387
+ plugin . _config . ignoreOutgoingUrls ,
388
+ ( e : Error ) =>
389
+ plugin . _logger . error ( 'caught ignoreOutgoingUrls error: ' , e )
390
+ )
357
391
) {
358
392
return original . apply ( this , [ options , ...args ] ) ;
359
393
}
@@ -370,8 +404,10 @@ export class HttpPlugin extends BasePlugin<Http> {
370
404
. getHttpTextFormat ( )
371
405
. inject ( span . context ( ) , Format . HTTP , options . headers ) ;
372
406
373
- const request : ClientRequest = plugin . _safeExecute ( span , ( ) =>
374
- original . apply ( this , [ options , ...args ] )
407
+ const request : ClientRequest = plugin . _safeExecute (
408
+ span ,
409
+ ( ) => original . apply ( this , [ options , ...args ] ) ,
410
+ true
375
411
) ;
376
412
377
413
plugin . _logger . debug ( '%s plugin outgoingRequest' , plugin . moduleName ) ;
@@ -399,16 +435,28 @@ export class HttpPlugin extends BasePlugin<Http> {
399
435
. startSpan ( name , options )
400
436
. setAttribute ( AttributeNames . COMPONENT , HttpPlugin . component ) ;
401
437
}
402
-
438
+ private _safeExecute <
439
+ T extends ( ...args : unknown [ ] ) => ReturnType < T > ,
440
+ K extends boolean
441
+ > (
442
+ span : Span ,
443
+ execute : T ,
444
+ rethrow : K
445
+ ) : K extends true ? ReturnType < T > : ( ReturnType < T > | void ) ;
403
446
private _safeExecute < T extends ( ...args : unknown [ ] ) => ReturnType < T > > (
404
447
span : Span ,
405
- execute : T
406
- ) : ReturnType < T > {
448
+ execute : T ,
449
+ rethrow : boolean
450
+ ) : ReturnType < T > | void {
407
451
try {
408
452
return execute ( ) ;
409
453
} catch ( error ) {
410
- Utils . setSpanWithError ( span , error ) ;
411
- throw error ;
454
+ if ( rethrow ) {
455
+ Utils . setSpanWithError ( span , error ) ;
456
+ span . end ( ) ;
457
+ throw error ;
458
+ }
459
+ this . _logger . error ( 'caught error ' , error ) ;
412
460
}
413
461
}
414
462
}
0 commit comments