Skip to content

Commit 332ffbb

Browse files
committed
test: updated e2e tracing
1 parent 828b414 commit 332ffbb

File tree

1 file changed

+98
-1
lines changed

1 file changed

+98
-1
lines changed

packages/tracing/tests/e2e/tracer.test.ts

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ describe('Tracer integration tests', () => {
109109
});
110110

111111
// sleep to allow for traces to be collected
112-
await new Promise((resolve) => setTimeout(resolve, 200000));
112+
await new Promise((resolve) => setTimeout(resolve, 180000));
113113

114114
}, 360000); // 6 minutes
115115

@@ -460,6 +460,103 @@ describe('Tracer integration tests', () => {
460460

461461
}, 120000); // 2 minutes
462462

463+
it('Verifies that a when Tracer is used as decorator on an async handler all custom traces are generated with correct annotations and metadata', async () => {
464+
465+
const resourceArn = invocationsMap['DecoratorWithAsyncHandler'].resourceArn;
466+
const expectedServiceName = invocationsMap['DecoratorWithAsyncHandler'].serviceName;
467+
468+
// Assess
469+
// Retrieve traces from X-Ray using Resource ARN as filter
470+
const sortedTraces = await getTraces(xray, startTime, resourceArn, invocations);
471+
472+
for (let i = 0; i < invocations; i++) {
473+
// Assert that the trace has the expected amount of segments
474+
expect(sortedTraces[i].Segments.length).toBe(4);
475+
476+
const invocationSubsegment = getInvocationSubsegment(sortedTraces[i]);
477+
478+
if (invocationSubsegment?.subsegments !== undefined) {
479+
expect(invocationSubsegment?.subsegments?.length).toBe(1);
480+
const handlerSubsegment = invocationSubsegment?.subsegments[0];
481+
// Assert that the subsegment name is the expected one
482+
expect(handlerSubsegment.name).toBe('## index.handler');
483+
if (handlerSubsegment?.subsegments !== undefined) {
484+
// Assert that there're three subsegments
485+
expect(handlerSubsegment?.subsegments?.length).toBe(3);
486+
487+
// Sort the subsegments by name
488+
const stsSubsegments: ParsedDocument[] = [];
489+
const methodSubsegment: ParsedDocument[] = [];
490+
const otherSegments: ParsedDocument[] = [];
491+
handlerSubsegment?.subsegments.forEach(subsegment => {
492+
if (subsegment.name === 'STS') {
493+
stsSubsegments.push(subsegment);
494+
} else if (subsegment.name === '### myMethod') {
495+
methodSubsegment.push(subsegment);
496+
} else {
497+
otherSegments.push(subsegment);
498+
}
499+
});
500+
// Assert that there are exactly two subsegment with the name 'STS'
501+
expect(stsSubsegments.length).toBe(2);
502+
// Assert that there is exactly one subsegment with the name '### myMethod'
503+
expect(methodSubsegment.length).toBe(1);
504+
// Assert that there are exactly zero other subsegments
505+
expect(otherSegments.length).toBe(0);
506+
507+
const { metadata } = methodSubsegment[0];
508+
509+
if (metadata !== undefined) {
510+
// Assert that the metadata object is as expected
511+
expect(metadata[expectedServiceName]['myMethod response'])
512+
.toEqual(expectedCustomResponseValue);
513+
} else {
514+
// Make test fail if there is no metadata
515+
expect('metadata !== undefined')
516+
.toBe('metadata === undefined');
517+
}
518+
} else {
519+
// Make test fail if the handlerSubsegment subsegment doesn't have any subsebment
520+
expect('handlerSubsegment?.subsegments !== undefined')
521+
.toBe('handlerSubsegment?.subsegments === undefined');
522+
}
523+
524+
const { annotations, metadata } = handlerSubsegment;
525+
526+
if (annotations !== undefined && metadata !== undefined) {
527+
// Assert that the annotations are as expected
528+
expect(annotations['ColdStart']).toEqual(true ? i === 0 : false);
529+
expect(annotations['Service']).toEqual(expectedServiceName);
530+
expect(annotations[expectedCustomAnnotationKey]).toEqual(expectedCustomAnnotationValue);
531+
// Assert that the metadata object is as expected
532+
expect(metadata[expectedServiceName][expectedCustomMetadataKey])
533+
.toEqual(expectedCustomMetadataValue);
534+
535+
if (i === invocations - 1) {
536+
// Assert that the subsegment has the expected fault
537+
expect(invocationSubsegment.error).toBe(true);
538+
expect(handlerSubsegment.fault).toBe(true);
539+
expect(handlerSubsegment.hasOwnProperty('cause')).toBe(true);
540+
expect(handlerSubsegment.cause?.exceptions[0].message).toBe(expectedCustomErrorMessage);
541+
} else {
542+
// Assert that the metadata object contains the response
543+
expect(metadata[expectedServiceName]['index.handler response'])
544+
.toEqual(expectedCustomResponseValue);
545+
}
546+
} else {
547+
// Make test fail if there are no annotations or metadata
548+
expect('annotations !== undefined && metadata !== undefined')
549+
.toBe('annotations === undefined && metadata === undefined');
550+
}
551+
} else {
552+
// Make test fail if the Invocation subsegment doesn't have an handler subsebment
553+
expect('invocationSubsegment?.subsegments !== undefined')
554+
.toBe('invocationSubsegment?.subsegments === undefined');
555+
}
556+
}
557+
558+
}, 120000); // 2 minutes
559+
463560
it('Verifies that a when Tracer is used as decorator, with errors & response capturing disabled, all custom traces are generated with correct annotations', async () => {
464561

465562
const resourceArn = invocationsMap['Decorator-NoCaptureErrorResponse'].resourceArn;

0 commit comments

Comments
 (0)