@@ -12,8 +12,7 @@ import { Table, AttributeType, BillingMode } from 'aws-cdk-lib/aws-dynamodb';
12
12
import { App , Duration , Stack , RemovalPolicy } from 'aws-cdk-lib' ;
13
13
import { deployStack , destroyStack } from '../../../commons/tests/utils/cdk-cli' ;
14
14
import * as AWS from 'aws-sdk' ;
15
- import { getTraces , getInvocationSubsegment } from '../helpers/tracesUtils' ;
16
- import type { ParsedDocument } from '../helpers/tracesUtils' ;
15
+ import { getTraces , getInvocationSubsegment , splitSegmentsByName } from '../helpers/tracesUtils' ;
17
16
18
17
const xray = new AWS . XRay ( ) ;
19
18
const lambdaClient = new AWS . Lambda ( ) ;
@@ -140,39 +139,26 @@ describe('Tracer integration tests', () => {
140
139
const sortedTraces = await getTraces ( xray , startTime , resourceArn , invocations , 5 ) ;
141
140
142
141
for ( let i = 0 ; i < invocations ; i ++ ) {
143
- // Assert that the trace has the expected amount of segments
144
142
expect ( sortedTraces [ i ] . Segments . length ) . toBe ( 5 ) ;
145
143
146
144
const invocationSubsegment = getInvocationSubsegment ( sortedTraces [ i ] ) ;
147
145
148
146
if ( invocationSubsegment ?. subsegments !== undefined ) {
149
147
expect ( invocationSubsegment ?. subsegments ?. length ) . toBe ( 1 ) ;
148
+
150
149
const handlerSubsegment = invocationSubsegment ?. subsegments [ 0 ] ;
151
- // Assert that the subsegment name is the expected one
152
150
expect ( handlerSubsegment . name ) . toBe ( '## index.handler' ) ;
151
+
153
152
if ( handlerSubsegment ?. subsegments !== undefined ) {
154
- // Assert that there are three subsegments
155
153
expect ( handlerSubsegment ?. subsegments ?. length ) . toBe ( 3 ) ;
156
154
157
- // Sort the subsegments by name
158
- const dynamoDBSubsegments : ParsedDocument [ ] = [ ] ;
159
- const httpSubsegment : ParsedDocument [ ] = [ ] ;
160
- const otherSegments : ParsedDocument [ ] = [ ] ;
161
- handlerSubsegment ?. subsegments . forEach ( subsegment => {
162
- if ( subsegment . name === 'DynamoDB' ) {
163
- dynamoDBSubsegments . push ( subsegment ) ;
164
- } else if ( subsegment . name === 'httpbin.org' ) {
165
- httpSubsegment . push ( subsegment ) ;
166
- } else {
167
- otherSegments . push ( subsegment ) ;
168
- }
169
- } ) ;
155
+ const subsegments = splitSegmentsByName ( handlerSubsegment . subsegments , [ 'DynamoDB' , 'httpbin.org' ] ) ;
170
156
// Assert that there are exactly two subsegment with the name 'DynamoDB'
171
- expect ( dynamoDBSubsegments . length ) . toBe ( 2 ) ;
157
+ expect ( subsegments . get ( 'DynamoDB' ) ? .length ) . toBe ( 2 ) ;
172
158
// Assert that there is exactly one subsegment with the name 'httpbin.org'
173
- expect ( httpSubsegment . length ) . toBe ( 1 ) ;
159
+ expect ( subsegments . get ( 'httpbin.org' ) ? .length ) . toBe ( 1 ) ;
174
160
// Assert that there are exactly zero other subsegments
175
- expect ( otherSegments . length ) . toBe ( 0 ) ;
161
+ expect ( subsegments . get ( 'other' ) ? .length ) . toBe ( 0 ) ;
176
162
177
163
const { annotations, metadata } = handlerSubsegment ;
178
164
@@ -232,32 +218,20 @@ describe('Tracer integration tests', () => {
232
218
233
219
if ( invocationSubsegment ?. subsegments !== undefined ) {
234
220
expect ( invocationSubsegment ?. subsegments ?. length ) . toBe ( 1 ) ;
221
+
235
222
const handlerSubsegment = invocationSubsegment ?. subsegments [ 0 ] ;
236
- // Assert that the subsegment name is the expected one
237
223
expect ( handlerSubsegment . name ) . toBe ( '## index.handler' ) ;
224
+
238
225
if ( handlerSubsegment ?. subsegments !== undefined ) {
239
- // Assert that there're two subsegments
240
226
expect ( handlerSubsegment ?. subsegments ?. length ) . toBe ( 3 ) ;
241
227
242
- // Sort the subsegments by name
243
- const dynamoDBSubsegments : ParsedDocument [ ] = [ ] ;
244
- const httpSubsegment : ParsedDocument [ ] = [ ] ;
245
- const otherSegments : ParsedDocument [ ] = [ ] ;
246
- handlerSubsegment ?. subsegments . forEach ( subsegment => {
247
- if ( subsegment . name === 'DynamoDB' ) {
248
- dynamoDBSubsegments . push ( subsegment ) ;
249
- } else if ( subsegment . name === 'httpbin.org' ) {
250
- httpSubsegment . push ( subsegment ) ;
251
- } else {
252
- otherSegments . push ( subsegment ) ;
253
- }
254
- } ) ;
228
+ const subsegments = splitSegmentsByName ( handlerSubsegment . subsegments , [ 'DynamoDB' , 'httpbin.org' ] ) ;
255
229
// Assert that there are exactly two subsegment with the name 'DynamoDB'
256
- expect ( dynamoDBSubsegments . length ) . toBe ( 2 ) ;
230
+ expect ( subsegments . get ( 'DynamoDB' ) ? .length ) . toBe ( 2 ) ;
257
231
// Assert that there is exactly one subsegment with the name 'httpbin.org'
258
- expect ( httpSubsegment . length ) . toBe ( 1 ) ;
232
+ expect ( subsegments . get ( 'httpbin.org' ) ? .length ) . toBe ( 1 ) ;
259
233
// Assert that there are exactly zero other subsegments
260
- expect ( otherSegments . length ) . toBe ( 0 ) ;
234
+ expect ( subsegments . get ( 'other' ) ? .length ) . toBe ( 0 ) ;
261
235
262
236
const { annotations, metadata } = handlerSubsegment ;
263
237
@@ -317,32 +291,20 @@ describe('Tracer integration tests', () => {
317
291
318
292
if ( invocationSubsegment ?. subsegments !== undefined ) {
319
293
expect ( invocationSubsegment ?. subsegments ?. length ) . toBe ( 1 ) ;
294
+
320
295
const handlerSubsegment = invocationSubsegment ?. subsegments [ 0 ] ;
321
- // Assert that the subsegment name is the expected one
322
296
expect ( handlerSubsegment . name ) . toBe ( '## index.handler' ) ;
297
+
323
298
if ( handlerSubsegment ?. subsegments !== undefined ) {
324
- // Assert that there're two subsegments
325
299
expect ( handlerSubsegment ?. subsegments ?. length ) . toBe ( 3 ) ;
326
300
327
- // Sort the subsegments by name
328
- const dynamoDBSubsegments : ParsedDocument [ ] = [ ] ;
329
- const httpSubsegment : ParsedDocument [ ] = [ ] ;
330
- const otherSegments : ParsedDocument [ ] = [ ] ;
331
- handlerSubsegment ?. subsegments . forEach ( subsegment => {
332
- if ( subsegment . name === 'DynamoDB' ) {
333
- dynamoDBSubsegments . push ( subsegment ) ;
334
- } else if ( subsegment . name === 'httpbin.org' ) {
335
- httpSubsegment . push ( subsegment ) ;
336
- } else {
337
- otherSegments . push ( subsegment ) ;
338
- }
339
- } ) ;
301
+ const subsegments = splitSegmentsByName ( handlerSubsegment . subsegments , [ 'DynamoDB' , 'httpbin.org' ] ) ;
340
302
// Assert that there are exactly two subsegment with the name 'DynamoDB'
341
- expect ( dynamoDBSubsegments . length ) . toBe ( 2 ) ;
303
+ expect ( subsegments . get ( 'DynamoDB' ) ? .length ) . toBe ( 2 ) ;
342
304
// Assert that there is exactly one subsegment with the name 'httpbin.org'
343
- expect ( httpSubsegment . length ) . toBe ( 1 ) ;
305
+ expect ( subsegments . get ( 'httpbin.org' ) ? .length ) . toBe ( 1 ) ;
344
306
// Assert that there are exactly zero other subsegments
345
- expect ( otherSegments . length ) . toBe ( 0 ) ;
307
+ expect ( subsegments . get ( 'other' ) ? .length ) . toBe ( 0 ) ;
346
308
347
309
const { annotations, metadata } = handlerSubsegment ;
348
310
@@ -425,38 +387,24 @@ describe('Tracer integration tests', () => {
425
387
426
388
if ( invocationSubsegment ?. subsegments !== undefined ) {
427
389
expect ( invocationSubsegment ?. subsegments ?. length ) . toBe ( 1 ) ;
390
+
428
391
const handlerSubsegment = invocationSubsegment ?. subsegments [ 0 ] ;
429
- // Assert that the subsegment name is the expected one
430
392
expect ( handlerSubsegment . name ) . toBe ( '## index.handler' ) ;
393
+
431
394
if ( handlerSubsegment ?. subsegments !== undefined ) {
432
- // Assert that there are four subsegments
433
395
expect ( handlerSubsegment ?. subsegments ?. length ) . toBe ( 4 ) ;
434
396
435
- // Sort the subsegments by name
436
- const dynamoDBSubsegments : ParsedDocument [ ] = [ ] ;
437
- const methodSubsegment : ParsedDocument [ ] = [ ] ;
438
- const httpSubsegment : ParsedDocument [ ] = [ ] ;
439
- const otherSegments : ParsedDocument [ ] = [ ] ;
440
- handlerSubsegment ?. subsegments . forEach ( subsegment => {
441
- if ( subsegment . name === 'DynamoDB' ) {
442
- dynamoDBSubsegments . push ( subsegment ) ;
443
- } else if ( subsegment . name === '### myMethod' ) {
444
- methodSubsegment . push ( subsegment ) ;
445
- } else if ( subsegment . name === 'httpbin.org' ) {
446
- httpSubsegment . push ( subsegment ) ;
447
- } else {
448
- otherSegments . push ( subsegment ) ;
449
- }
450
- } ) ;
397
+ const subsegments = splitSegmentsByName ( handlerSubsegment . subsegments , [ 'DynamoDB' , 'httpbin.org' , '### myMethod' ] ) ;
451
398
// Assert that there are exactly two subsegment with the name 'DynamoDB'
452
- expect ( dynamoDBSubsegments . length ) . toBe ( 2 ) ;
453
- // Assert that there is exactly one subsegment with the name '### myMethod'
454
- expect ( methodSubsegment . length ) . toBe ( 1 ) ;
399
+ expect ( subsegments . get ( 'DynamoDB' ) ?. length ) . toBe ( 2 ) ;
455
400
// Assert that there is exactly one subsegment with the name 'httpbin.org'
456
- expect ( httpSubsegment . length ) . toBe ( 1 ) ;
401
+ expect ( subsegments . get ( 'httpbin.org' ) ?. length ) . toBe ( 1 ) ;
402
+ // Assert that there is exactly one subsegment with the name '### myMethod'
403
+ expect ( subsegments . get ( '### myMethod' ) ?. length ) . toBe ( 1 ) ;
457
404
// Assert that there are exactly zero other subsegments
458
- expect ( otherSegments . length ) . toBe ( 0 ) ;
405
+ expect ( subsegments . get ( 'other' ) ? .length ) . toBe ( 0 ) ;
459
406
407
+ const methodSubsegment = subsegments . get ( '### myMethod' ) || [ ] ;
460
408
const { metadata } = methodSubsegment [ 0 ] ;
461
409
462
410
if ( metadata !== undefined ) {
@@ -527,38 +475,24 @@ describe('Tracer integration tests', () => {
527
475
528
476
if ( invocationSubsegment ?. subsegments !== undefined ) {
529
477
expect ( invocationSubsegment ?. subsegments ?. length ) . toBe ( 1 ) ;
478
+
530
479
const handlerSubsegment = invocationSubsegment ?. subsegments [ 0 ] ;
531
- // Assert that the subsegment name is the expected one
532
480
expect ( handlerSubsegment . name ) . toBe ( '## index.handler' ) ;
481
+
533
482
if ( handlerSubsegment ?. subsegments !== undefined ) {
534
- // Assert that there are four subsegments
535
483
expect ( handlerSubsegment ?. subsegments ?. length ) . toBe ( 4 ) ;
536
484
537
- // Sort the subsegments by name
538
- const dynamoDBSubsegments : ParsedDocument [ ] = [ ] ;
539
- const methodSubsegment : ParsedDocument [ ] = [ ] ;
540
- const httpSubsegment : ParsedDocument [ ] = [ ] ;
541
- const otherSegments : ParsedDocument [ ] = [ ] ;
542
- handlerSubsegment ?. subsegments . forEach ( subsegment => {
543
- if ( subsegment . name === 'DynamoDB' ) {
544
- dynamoDBSubsegments . push ( subsegment ) ;
545
- } else if ( subsegment . name === '### myMethod' ) {
546
- methodSubsegment . push ( subsegment ) ;
547
- } else if ( subsegment . name === 'httpbin.org' ) {
548
- httpSubsegment . push ( subsegment ) ;
549
- } else {
550
- otherSegments . push ( subsegment ) ;
551
- }
552
- } ) ;
485
+ const subsegments = splitSegmentsByName ( handlerSubsegment . subsegments , [ 'DynamoDB' , 'httpbin.org' , '### myMethod' ] ) ;
553
486
// Assert that there are exactly two subsegment with the name 'DynamoDB'
554
- expect ( dynamoDBSubsegments . length ) . toBe ( 2 ) ;
555
- // Assert that there is exactly one subsegment with the name '### myMethod'
556
- expect ( methodSubsegment . length ) . toBe ( 1 ) ;
487
+ expect ( subsegments . get ( 'DynamoDB' ) ?. length ) . toBe ( 2 ) ;
557
488
// Assert that there is exactly one subsegment with the name 'httpbin.org'
558
- expect ( httpSubsegment . length ) . toBe ( 1 ) ;
489
+ expect ( subsegments . get ( 'httpbin.org' ) ?. length ) . toBe ( 1 ) ;
490
+ // Assert that there is exactly one subsegment with the name '### myMethod'
491
+ expect ( subsegments . get ( '### myMethod' ) ?. length ) . toBe ( 1 ) ;
559
492
// Assert that there are exactly zero other subsegments
560
- expect ( otherSegments . length ) . toBe ( 0 ) ;
493
+ expect ( subsegments . get ( 'other' ) ? .length ) . toBe ( 0 ) ;
561
494
495
+ const methodSubsegment = subsegments . get ( '### myMethod' ) || [ ] ;
562
496
const { metadata } = methodSubsegment [ 0 ] ;
563
497
564
498
if ( metadata !== undefined ) {
@@ -629,38 +563,25 @@ describe('Tracer integration tests', () => {
629
563
630
564
if ( invocationSubsegment ?. subsegments !== undefined ) {
631
565
expect ( invocationSubsegment ?. subsegments ?. length ) . toBe ( 1 ) ;
566
+
632
567
const handlerSubsegment = invocationSubsegment ?. subsegments [ 0 ] ;
633
- // Assert that the subsegment name is the expected one
634
568
expect ( handlerSubsegment . name ) . toBe ( '## index.handler' ) ;
569
+
635
570
if ( handlerSubsegment ?. subsegments !== undefined ) {
636
- // Assert that there are four subsegments
637
571
expect ( handlerSubsegment ?. subsegments ?. length ) . toBe ( 4 ) ;
638
572
639
- // Sort the subsegments by name
640
- const dynamoDBSubsegments : ParsedDocument [ ] = [ ] ;
641
- const methodSubsegment : ParsedDocument [ ] = [ ] ;
642
- const httpSubsegment : ParsedDocument [ ] = [ ] ;
643
- const otherSegments : ParsedDocument [ ] = [ ] ;
644
- handlerSubsegment ?. subsegments . forEach ( subsegment => {
645
- if ( subsegment . name === 'DynamoDB' ) {
646
- dynamoDBSubsegments . push ( subsegment ) ;
647
- } else if ( subsegment . name === '### myMethod' ) {
648
- methodSubsegment . push ( subsegment ) ;
649
- } else if ( subsegment . name === 'httpbin.org' ) {
650
- httpSubsegment . push ( subsegment ) ;
651
- } else {
652
- otherSegments . push ( subsegment ) ;
653
- }
654
- } ) ;
573
+ const subsegments = splitSegmentsByName ( handlerSubsegment . subsegments , [ 'DynamoDB' , 'httpbin.org' , '### myMethod' ] ) ;
655
574
// Assert that there are exactly two subsegment with the name 'DynamoDB'
656
- expect ( dynamoDBSubsegments . length ) . toBe ( 2 ) ;
657
- // Assert that there is exactly one subsegment with the name '### myMethod'
658
- expect ( methodSubsegment . length ) . toBe ( 1 ) ;
575
+ expect ( subsegments . get ( 'DynamoDB' ) ?. length ) . toBe ( 2 ) ;
659
576
// Assert that there is exactly one subsegment with the name 'httpbin.org'
660
- expect ( httpSubsegment . length ) . toBe ( 1 ) ;
577
+ expect ( subsegments . get ( 'httpbin.org' ) ?. length ) . toBe ( 1 ) ;
578
+ // Assert that there is exactly one subsegment with the name '### myMethod'
579
+ expect ( subsegments . get ( '### myMethod' ) ?. length ) . toBe ( 1 ) ;
661
580
// Assert that there are exactly zero other subsegments
662
- expect ( otherSegments . length ) . toBe ( 0 ) ;
581
+ expect ( subsegments . get ( 'other' ) ?. length ) . toBe ( 0 ) ;
582
+
663
583
// Assert that no response was captured on the subsegment
584
+ const methodSubsegment = subsegments . get ( '### myMethod' ) || [ ] ;
664
585
expect ( methodSubsegment [ 0 ] . hasOwnProperty ( 'metadata' ) ) . toBe ( false ) ;
665
586
} else {
666
587
// Make test fail if the handlerSubsegment subsegment doesn't have any subsebment
0 commit comments