@@ -12,6 +12,7 @@ import { SdkProvider } from 'aws-cdk/lib/api/aws-auth';
12
12
import { CloudFormationDeployments } from 'aws-cdk/lib/api/cloudformation-deployments' ;
13
13
import * as AWS from 'aws-sdk' ;
14
14
import { getTraces , getInvocationSubsegment } from '../helpers/tracesUtils' ;
15
+ import type { ParsedDocument } from '../helpers/tracesUtils' ;
15
16
16
17
const xray = new AWS . XRay ( ) ;
17
18
const lambdaClient = new AWS . Lambda ( ) ;
@@ -88,7 +89,9 @@ describe('Tracer integration tests', () => {
88
89
await lambdaClient . invoke ( {
89
90
FunctionName : functionName ,
90
91
Payload : JSON . stringify ( {
91
- throw : i === invocations ? true : false , // only last invocation should throw
92
+ throw : i === invocations - 1 ? true : false , // only last invocation should throw
93
+ sdkV2 : i === 1 ? 'all' : 'client' , // only second invocation should use captureAll
94
+ invocation : i + 1 , // Pass invocation number for easier debugging
92
95
} ) ,
93
96
} ) . promise ( ) ;
94
97
}
@@ -197,7 +200,7 @@ describe('Tracer integration tests', () => {
197
200
198
201
for ( let i = 0 ; i < invocations ; i ++ ) {
199
202
// Assert that the trace has the expected amount of segments
200
- expect ( sortedTraces [ i ] . Segments . length ) . toBe ( 2 ) ;
203
+ expect ( sortedTraces [ i ] . Segments . length ) . toBe ( 4 ) ;
201
204
202
205
const invocationSubsegment = getInvocationSubsegment ( sortedTraces [ i ] ) ;
203
206
@@ -206,35 +209,46 @@ describe('Tracer integration tests', () => {
206
209
const handlerSubsegment = invocationSubsegment ?. subsegments [ 0 ] ;
207
210
// Assert that the subsegment name is the expected one
208
211
expect ( handlerSubsegment . name ) . toBe ( '## index.handler' ) ;
209
- // Assert that there're no subsegments
210
- expect ( handlerSubsegment . hasOwnProperty ( 'subsegments' ) ) . toBe ( false ) ;
211
-
212
- const { annotations, metadata } = handlerSubsegment ;
212
+ if ( handlerSubsegment ?. subsegments !== undefined ) {
213
+ // Assert that there're two subsegments
214
+ expect ( handlerSubsegment ?. subsegments ?. length ) . toBe ( 2 ) ;
213
215
214
- if ( annotations !== undefined && metadata !== undefined ) {
215
- // Assert that the annotations are as expected
216
- expect ( annotations [ 'ColdStart' ] ) . toEqual ( true ? i === 0 : false ) ;
217
- expect ( annotations [ 'Service' ] ) . toEqual ( expectedServiceName ) ;
218
- expect ( annotations [ expectedCustomAnnotationKey ] ) . toEqual ( expectedCustomAnnotationValue ) ;
219
- // Assert that the metadata object is as expected
220
- expect ( metadata [ expectedServiceName ] [ expectedCustomMetadataKey ] )
221
- . toEqual ( expectedCustomMetadataValue ) ;
216
+ const [ AWSSDKSubsegment1 , AWSSDKSubsegment2 ] = handlerSubsegment ?. subsegments ;
217
+ // Assert that the subsegment names is the expected ones
218
+ expect ( AWSSDKSubsegment1 . name ) . toBe ( 'STS' ) ;
219
+ expect ( AWSSDKSubsegment2 . name ) . toBe ( 'STS' ) ;
222
220
223
- if ( i === invocations - 1 ) {
224
- // Assert that the subsegment has the expected fault
225
- expect ( invocationSubsegment . error ) . toBe ( true ) ;
226
- expect ( handlerSubsegment . fault ) . toBe ( true ) ;
227
- expect ( handlerSubsegment . hasOwnProperty ( 'cause' ) ) . toBe ( true ) ;
228
- expect ( handlerSubsegment . cause ?. exceptions [ 0 ] . message ) . toBe ( expectedCustomErrorMessage ) ;
221
+ const { annotations, metadata } = handlerSubsegment ;
222
+
223
+ if ( annotations !== undefined && metadata !== undefined ) {
224
+ // Assert that the annotations are as expected
225
+ expect ( annotations [ 'ColdStart' ] ) . toEqual ( true ? i === 0 : false ) ;
226
+ expect ( annotations [ 'Service' ] ) . toEqual ( expectedServiceName ) ;
227
+ expect ( annotations [ expectedCustomAnnotationKey ] ) . toEqual ( expectedCustomAnnotationValue ) ;
228
+ // Assert that the metadata object is as expected
229
+ expect ( metadata [ expectedServiceName ] [ expectedCustomMetadataKey ] )
230
+ . toEqual ( expectedCustomMetadataValue ) ;
231
+
232
+ if ( i === invocations - 1 ) {
233
+ // Assert that the subsegment has the expected fault
234
+ expect ( invocationSubsegment . error ) . toBe ( true ) ;
235
+ expect ( handlerSubsegment . fault ) . toBe ( true ) ;
236
+ expect ( handlerSubsegment . hasOwnProperty ( 'cause' ) ) . toBe ( true ) ;
237
+ expect ( handlerSubsegment . cause ?. exceptions [ 0 ] . message ) . toBe ( expectedCustomErrorMessage ) ;
238
+ } else {
239
+ // Assert that the metadata object contains the response
240
+ expect ( metadata [ expectedServiceName ] [ 'index.handler response' ] )
241
+ . toEqual ( expectedCustomResponseValue ) ;
242
+ }
229
243
} else {
230
- // Assert that the metadata object contains the response
231
- expect ( metadata [ expectedServiceName ] [ 'index.handler response' ] )
232
- . toEqual ( expectedCustomResponseValue ) ;
244
+ // Make test fail if there are no annotations or metadata
245
+ expect ( 'annotations !== undefined && metadata !== undefined' )
246
+ . toBe ( 'annotations === undefined && metadata === undefined' ) ;
233
247
}
234
248
} else {
235
- // Make test fail if there are no annotations or metadata
236
- expect ( 'annotations !== undefined && metadata !== undefined' )
237
- . toBe ( 'annotations === undefined && metadata === undefined' ) ;
249
+ // Make test fail if the handlerSubsegment subsegment doesn't have any subsebment
250
+ expect ( 'handlerSubsegment?.subsegments !== undefined' )
251
+ . toBe ( 'handlerSubsegment?.subsegments === undefined' ) ;
238
252
}
239
253
} else {
240
254
// Make test fail if the Invocation subsegment doesn't have an handler subsebment
@@ -256,7 +270,7 @@ describe('Tracer integration tests', () => {
256
270
257
271
for ( let i = 0 ; i < invocations ; i ++ ) {
258
272
// Assert that the trace has the expected amount of segments
259
- expect ( sortedTraces [ i ] . Segments . length ) . toBe ( 2 ) ;
273
+ expect ( sortedTraces [ i ] . Segments . length ) . toBe ( 4 ) ;
260
274
261
275
const invocationSubsegment = getInvocationSubsegment ( sortedTraces [ i ] ) ;
262
276
@@ -266,13 +280,30 @@ describe('Tracer integration tests', () => {
266
280
// Assert that the subsegment name is the expected one
267
281
expect ( handlerSubsegment . name ) . toBe ( '## index.handler' ) ;
268
282
if ( handlerSubsegment ?. subsegments !== undefined ) {
269
- // Assert that there is one subsegment
270
- expect ( handlerSubsegment ?. subsegments ?. length ) . toBe ( 1 ) ;
271
- const methodSubsegment = handlerSubsegment ?. subsegments [ 0 ] ;
272
- // Assert that the subsegment name is the expected one
273
- expect ( methodSubsegment . name ) . toBe ( '### myMethod' ) ;
274
-
275
- const { metadata } = methodSubsegment ;
283
+ // Assert that there're three subsegments
284
+ expect ( handlerSubsegment ?. subsegments ?. length ) . toBe ( 3 ) ;
285
+
286
+ // Sort the subsegments by name
287
+ const stsSubsegments : ParsedDocument [ ] = [ ] ;
288
+ const methodSubsegment : ParsedDocument [ ] = [ ] ;
289
+ const otherSegments : ParsedDocument [ ] = [ ] ;
290
+ handlerSubsegment ?. subsegments . forEach ( subsegment => {
291
+ if ( subsegment . name === 'STS' ) {
292
+ stsSubsegments . push ( subsegment ) ;
293
+ } else if ( subsegment . name === '### myMethod' ) {
294
+ methodSubsegment . push ( subsegment ) ;
295
+ } else {
296
+ otherSegments . push ( subsegment ) ;
297
+ }
298
+ } ) ;
299
+ // Assert that there are exactly two subsegment with the name 'STS'
300
+ expect ( stsSubsegments . length ) . toBe ( 2 ) ;
301
+ // Assert that there is exactly one subsegment with the name '### myMethod'
302
+ expect ( methodSubsegment . length ) . toBe ( 1 ) ;
303
+ // Assert that there are exactly zero other subsegments
304
+ expect ( otherSegments . length ) . toBe ( 0 ) ;
305
+
306
+ const { metadata } = methodSubsegment [ 0 ] ;
276
307
277
308
if ( metadata !== undefined ) {
278
309
// Assert that the metadata object is as expected
0 commit comments