1
- import type { Handler } from 'aws-lambda' ;
2
1
import { Utility } from '@aws-lambda-powertools/commons' ;
3
2
import type {
4
3
AsyncHandler ,
5
- SyncHandler ,
6
4
HandlerMethodDecorator ,
5
+ SyncHandler ,
7
6
} from '@aws-lambda-powertools/commons/types' ;
7
+ import type { Handler } from 'aws-lambda' ;
8
+ import type { Segment , Subsegment } from 'aws-xray-sdk-core' ;
9
+ import xraySdk from 'aws-xray-sdk-core' ;
8
10
import { EnvironmentVariablesService } from './config/EnvironmentVariablesService.js' ;
11
+ import { ProviderService } from './provider/ProviderService.js' ;
9
12
import type { ConfigServiceInterface } from './types/ConfigServiceInterface.js' ;
13
+ import type { ProviderServiceInterface } from './types/ProviderService.js' ;
10
14
import type {
11
- TracerInterface ,
12
- TracerOptions ,
13
15
AnyClass ,
14
- MethodDecorator ,
15
16
CaptureLambdaHandlerOptions ,
16
17
CaptureMethodOptions ,
18
+ MethodDecorator ,
19
+ TracerInterface ,
20
+ TracerOptions ,
17
21
} from './types/Tracer.js' ;
18
- import { ProviderService } from './provider/ProviderService.js' ;
19
- import type { ProviderServiceInterface } from './types/ProviderService.js' ;
20
- import type { Segment , Subsegment } from 'aws-xray-sdk-core' ;
21
- import xraySdk from 'aws-xray-sdk-core' ;
22
22
const { Subsegment : XraySubsegment } = xraySdk ;
23
23
24
24
/**
@@ -372,22 +372,16 @@ class Tracer extends Utility implements TracerInterface {
372
372
options ?: CaptureLambdaHandlerOptions
373
373
) : HandlerMethodDecorator {
374
374
return ( _target , _propertyKey , descriptor ) => {
375
- /**
376
- * The descriptor.value is the method this decorator decorates, it cannot be undefined.
377
- */
378
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
375
+ // biome-ignore lint/style/noNonNullAssertion: The descriptor.value is the method this decorator decorates, it cannot be undefined.
379
376
const originalMethod = descriptor . value ! ;
380
377
381
378
// eslint-disable-next-line @typescript-eslint/no-this-alias
382
379
const tracerRef = this ;
383
380
// Use a function() {} instead of an () => {} arrow function so that we can
384
381
// access `myClass` as `this` in a decorated `myClass.myMethod()`.
385
382
descriptor . value = function ( this : Handler , event , context , callback ) {
386
- // eslint-disable-next-line @typescript-eslint/no-this-alias
387
- const handlerRef : Handler = this ;
388
-
389
383
if ( ! tracerRef . isTracingEnabled ( ) ) {
390
- return originalMethod . apply ( handlerRef , [ event , context , callback ] ) ;
384
+ return originalMethod . apply ( this , [ event , context , callback ] ) ;
391
385
}
392
386
393
387
return tracerRef . provider . captureAsyncFunc (
@@ -397,7 +391,7 @@ class Tracer extends Utility implements TracerInterface {
397
391
tracerRef . addServiceNameAnnotation ( ) ;
398
392
let result : unknown ;
399
393
try {
400
- result = await originalMethod . apply ( handlerRef , [
394
+ result = await originalMethod . apply ( this , [
401
395
event ,
402
396
context ,
403
397
callback ,
@@ -413,7 +407,7 @@ class Tracer extends Utility implements TracerInterface {
413
407
subsegment ?. close ( ) ;
414
408
} catch ( error ) {
415
409
console . warn (
416
- ` Failed to close or serialize segment %s. We are catching the error but data might be lost.` ,
410
+ ' Failed to close or serialize segment %s. We are catching the error but data might be lost.' ,
417
411
subsegment ?. name ,
418
412
error
419
413
) ;
@@ -469,8 +463,7 @@ class Tracer extends Utility implements TracerInterface {
469
463
options ?: CaptureMethodOptions
470
464
) : MethodDecorator < T > {
471
465
return ( _target , propertyKey , descriptor ) => {
472
- // The descriptor.value is the method this decorator decorates, it cannot be undefined.
473
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
466
+ // biome-ignore lint/style/noNonNullAssertion: The descriptor.value is the method this decorator decorates, it cannot be undefined.
474
467
const originalMethod = descriptor . value ! ;
475
468
476
469
// eslint-disable-next-line @typescript-eslint/no-this-alias
@@ -490,7 +483,8 @@ class Tracer extends Utility implements TracerInterface {
490
483
return tracerRef . provider . captureAsyncFunc (
491
484
subsegmentName ,
492
485
async ( subsegment ) => {
493
- let result ;
486
+ // biome-ignore lint/suspicious/noExplicitAny: we don't know the type of the result because we're decorating arbitrary functions
487
+ let result : any ;
494
488
try {
495
489
result = await originalMethod . apply ( this , [ ...args ] ) ;
496
490
if ( options ?. captureResponse ?? true ) {
@@ -505,7 +499,7 @@ class Tracer extends Utility implements TracerInterface {
505
499
subsegment ?. close ( ) ;
506
500
} catch ( error ) {
507
501
console . warn (
508
- ` Failed to close or serialize segment %s. We are catching the error but data might be lost.` ,
502
+ ' Failed to close or serialize segment %s. We are catching the error but data might be lost.' ,
509
503
subsegment ?. name ,
510
504
error
511
505
) ;
@@ -702,7 +696,7 @@ class Tracer extends Utility implements TracerInterface {
702
696
public setSegment ( segment : Segment | Subsegment ) : void {
703
697
if ( ! this . isTracingEnabled ( ) ) return ;
704
698
705
- return this . provider . setSegment ( segment ) ;
699
+ this . provider . setSegment ( segment ) ;
706
700
}
707
701
708
702
/**
0 commit comments