1
+ import { Tracer } from '../../src' ;
2
+ import { Callback , Context } from 'aws-lambda' ;
3
+ import { STSClient , GetCallerIdentityCommand } from '@aws-sdk/client-sts' ;
4
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
5
+ let AWS = require ( 'aws-sdk' ) ;
6
+
7
+ const serviceName = process . env . EXPECTED_SERVICE_NAME ?? 'MyFunctionWithStandardHandler' ;
8
+ const customAnnotationKey = process . env . EXPECTED_CUSTOM_ANNOTATION_KEY ?? 'myAnnotation' ;
9
+ const customAnnotationValue = process . env . EXPECTED_CUSTOM_ANNOTATION_VALUE ?? 'myValue' ;
10
+ const customMetadataKey = process . env . EXPECTED_CUSTOM_METADATA_KEY ?? 'myMetadata' ;
11
+ const customMetadataValue = JSON . parse ( process . env . EXPECTED_CUSTOM_METADATA_VALUE ) ?? { bar : 'baz' } ;
12
+ const customResponseValue = JSON . parse ( process . env . EXPECTED_CUSTOM_RESPONSE_VALUE ) ?? { foo : 'bar' } ;
13
+ const customErrorMessage = process . env . EXPECTED_CUSTOM_ERROR_MESSAGE ?? 'An error has occurred' ;
14
+
15
+ interface CustomEvent {
16
+ throw : boolean
17
+ sdkV2 : string
18
+ invocation : number
19
+ }
20
+
21
+ // Function that refreshes imports to ensure that we are instrumenting only one version of the AWS SDK v2 at a time.
22
+ const refreshAWSSDKImport = ( ) : void => {
23
+ // Clean up the require cache to ensure we're using a newly imported version of the AWS SDK v2
24
+ for ( const key in require . cache ) {
25
+ if ( key . indexOf ( '/aws-sdk/' ) !== - 1 ) {
26
+ delete require . cache [ key ] ;
27
+ }
28
+ }
29
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
30
+ AWS = require ( 'aws-sdk' ) ;
31
+ } ;
32
+
33
+ // Disable capture errors & responses for this test
34
+ process . env . POWERTOOLS_TRACER_CAPTURE_RESPONSE = 'false' ;
35
+ process . env . POWERTOOLS_TRACER_CAPTURE_ERROR = 'false' ;
36
+ const tracer = new Tracer ( { serviceName : serviceName } ) ;
37
+ const stsv3 = tracer . captureAWSv3Client ( new STSClient ( { } ) ) ;
38
+
39
+ export class MyFunctionWithDecorator {
40
+ @tracer . captureLambdaHandler ( )
41
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
42
+ // @ts -ignore
43
+ public handler ( event : CustomEvent , _context : Context , _callback : Callback < unknown > ) : void | Promise < unknown > {
44
+ tracer . putAnnotation ( customAnnotationKey , customAnnotationValue ) ;
45
+ tracer . putMetadata ( customMetadataKey , customMetadataValue ) ;
46
+
47
+ let stsv2 ;
48
+ refreshAWSSDKImport ( ) ;
49
+ if ( event . sdkV2 === 'client' ) {
50
+ stsv2 = tracer . captureAWSClient ( new AWS . STS ( ) ) ;
51
+ } else if ( event . sdkV2 === 'all' ) {
52
+ AWS = tracer . captureAWS ( AWS ) ;
53
+ stsv2 = new AWS . STS ( ) ;
54
+ }
55
+
56
+ return Promise . all ( [
57
+ stsv2 . getCallerIdentity ( ) . promise ( ) ,
58
+ stsv3 . send ( new GetCallerIdentityCommand ( { } ) ) ,
59
+ new Promise ( ( resolve , reject ) => {
60
+ setTimeout ( ( ) => {
61
+ const res = this . myMethod ( ) ;
62
+ if ( event . throw ) {
63
+ reject ( new Error ( customErrorMessage ) ) ;
64
+ } else {
65
+ resolve ( res ) ;
66
+ }
67
+ } , 2000 ) ; // We need to wait for to make sure previous calls are finished
68
+ } )
69
+ ] )
70
+ . then ( ( [ _stsv2Res , _stsv3Res , promiseRes ] ) => promiseRes )
71
+ . catch ( ( err ) => {
72
+ throw err ;
73
+ } ) ;
74
+ }
75
+
76
+ @tracer . captureMethod ( )
77
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
78
+ // @ts -ignore
79
+ public myMethod ( ) : string {
80
+ return customResponseValue ;
81
+ }
82
+ }
83
+
84
+ export const handlerClass = new MyFunctionWithDecorator ( ) ;
85
+ export const handler = handlerClass . handler ;
0 commit comments