Skip to content

Commit dd42e52

Browse files
committed
test: add middy options e2e
1 parent e456670 commit dd42e52

File tree

3 files changed

+80
-3
lines changed

3 files changed

+80
-3
lines changed

Diff for: packages/tracer/tests/e2e/allFeatures.middy.test.functionCode.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const refreshAWSSDKImport = (): void => {
3636
const tracer = new Tracer({ serviceName: serviceName });
3737
const dynamoDBv3 = tracer.captureAWSv3Client(new DynamoDBClient({}));
3838

39-
export const handler = middy(async (event: CustomEvent, _context: Context): Promise<void> => {
39+
const testHandler = async (event: CustomEvent, _context: Context): Promise<void> => {
4040
tracer.putAnnotation('invocation', event.invocation);
4141
tracer.putAnnotation(customAnnotationKey, customAnnotationValue);
4242
tracer.putMetadata(customMetadataKey, customMetadataValue);
@@ -63,4 +63,8 @@ export const handler = middy(async (event: CustomEvent, _context: Context): Prom
6363
} catch (err) {
6464
throw err;
6565
}
66-
}).use(captureLambdaHandler(tracer));
66+
};
67+
68+
export const handler = middy(testHandler).use(captureLambdaHandler(tracer));
69+
70+
export const handlerWithNoCaptureResponseViaMiddlewareOption = middy(testHandler).use(captureLambdaHandler(tracer, { captureResponse: false }));

Diff for: packages/tracer/tests/e2e/allFeatures.middy.test.ts

+72
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ const uuidFunction3 = v4();
7878
const functionNameWithTracerDisabled = generateUniqueName(RESOURCE_NAME_PREFIX, uuidFunction3, runtime, 'AllFeatures-Middy-TracerDisabled');
7979
const serviceNameWithTracerDisabled = functionNameWithNoCaptureErrorOrResponse;
8080

81+
/**
82+
* Function #4 doesn't capture response
83+
*/
84+
const uuidFunction4 = v4();
85+
const functionNameWithNoCaptureResponseViaMiddlewareOption = generateUniqueName(RESOURCE_NAME_PREFIX, uuidFunction4, runtime, 'AllFeatures-Middy-NoCaptureResponse2');
86+
const serviceNameWithNoCaptureResponseViaMiddlewareOption = functionNameWithNoCaptureResponseViaMiddlewareOption;
87+
8188
const xray = new AWS.XRay();
8289
const invocations = 3;
8390

@@ -149,13 +156,30 @@ describe(`Tracer E2E tests, all features with middy instantiation for runtime: $
149156
});
150157
ddbTable.grantWriteData(functionWithTracerDisabled);
151158

159+
const functionThatDoesNotCaptureResponseViaMiddlewareOption = createTracerTestFunction({
160+
stack,
161+
functionName: functionNameWithNoCaptureResponseViaMiddlewareOption,
162+
entry,
163+
handler: 'handlerWithNoCaptureResponseViaMiddlewareOption',
164+
expectedServiceName: serviceNameWithNoCaptureResponseViaMiddlewareOption,
165+
environmentParams: {
166+
TEST_TABLE_NAME: ddbTableName,
167+
POWERTOOLS_TRACER_CAPTURE_RESPONSE: 'true',
168+
POWERTOOLS_TRACER_CAPTURE_ERROR: 'true',
169+
POWERTOOLS_TRACE_ENABLED: 'true',
170+
},
171+
runtime
172+
});
173+
ddbTable.grantWriteData(functionThatDoesNotCaptureResponseViaMiddlewareOption);
174+
152175
await deployStack(integTestApp, stack);
153176

154177
// Act
155178
await Promise.all([
156179
invokeAllTestCases(functionNameWithAllFlagsEnabled),
157180
invokeAllTestCases(functionNameWithNoCaptureErrorOrResponse),
158181
invokeAllTestCases(functionNameWithTracerDisabled),
182+
invokeAllTestCases(functionNameWithNoCaptureResponseViaMiddlewareOption),
159183
]);
160184

161185
}, SETUP_TIMEOUT);
@@ -299,6 +323,54 @@ describe(`Tracer E2E tests, all features with middy instantiation for runtime: $
299323

300324
}, TEST_CASE_TIMEOUT);
301325

326+
it('should not capture response when the middleware\'s captureResponse is set to false', async () => {
327+
328+
const tracesWithNoCaptureResponse = await getTraces(xray, startTime, await getFunctionArn(functionNameWithNoCaptureResponseViaMiddlewareOption), invocations, 5);
329+
330+
expect(tracesWithNoCaptureResponse.length).toBe(invocations);
331+
332+
// Assess
333+
for (let i = 0; i < invocations; i++) {
334+
const trace = tracesWithNoCaptureResponse[i];
335+
336+
/**
337+
* Expect the trace to have 5 segments:
338+
* 1. Lambda Context (AWS::Lambda)
339+
* 2. Lambda Function (AWS::Lambda::Function)
340+
* 3. DynamoDB (AWS::DynamoDB)
341+
* 4. DynamoDB Table (AWS::DynamoDB::Table)
342+
* 5. Remote call (httpbin.org)
343+
*/
344+
expect(trace.Segments.length).toBe(5);
345+
const invocationSubsegment = getInvocationSubsegment(trace);
346+
347+
/**
348+
* Invocation subsegment should have a subsegment '## index.handlerWithNoCaptureResponseViaMiddlewareOption' (default behavior for PowerTool tracer)
349+
* '## index.handlerWithNoCaptureResponseViaMiddlewareOption' subsegment should have 3 subsegments
350+
* 1. DynamoDB (PutItem on the table)
351+
* 2. DynamoDB (PutItem overhead)
352+
* 3. httpbin.org (Remote call)
353+
*/
354+
const handlerSubsegment = getFirstSubsegment(invocationSubsegment);
355+
expect(handlerSubsegment.name).toBe('## index.handlerWithNoCaptureResponseViaMiddlewareOption');
356+
expect(handlerSubsegment?.subsegments).toHaveLength(3);
357+
358+
if (!handlerSubsegment.subsegments) {
359+
fail('"## index.handlerWithNoCaptureResponseViaMiddlewareOption" subsegment should have subsegments');
360+
}
361+
const subsegments = splitSegmentsByName(handlerSubsegment.subsegments, [ 'DynamoDB', 'httpbin.org' ]);
362+
expect(subsegments.get('DynamoDB')?.length).toBe(2);
363+
expect(subsegments.get('httpbin.org')?.length).toBe(1);
364+
expect(subsegments.get('other')?.length).toBe(0);
365+
366+
const shouldThrowAnError = (i === (invocations - 1));
367+
if (shouldThrowAnError) {
368+
assertErrorAndFault(invocationSubsegment, expectedCustomErrorMessage);
369+
}
370+
}
371+
372+
}, TEST_CASE_TIMEOUT);
373+
302374
it('should not capture any custom traces when disabled', async () => {
303375
const expectedNoOfTraces = 2;
304376
const tracesWithTracerDisabled = await getTraces(xray, startTime, await getFunctionArn(functionNameWithTracerDisabled), invocations, expectedNoOfTraces);

Diff for: packages/tracer/tests/helpers/tracesUtils.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export interface ParsedTrace {
7979
interface TracerTestFunctionParams {
8080
stack: Stack
8181
functionName: string
82+
handler?: string
8283
entry: string
8384
expectedServiceName: string
8485
environmentParams: { [key: string]: string }
@@ -237,7 +238,7 @@ const createTracerTestFunction = (params: TracerTestFunctionParams): NodejsFunct
237238
const func = new NodejsFunction(stack, functionName, {
238239
entry: entry,
239240
functionName: functionName,
240-
handler: 'handler',
241+
handler: params.handler ?? 'handler',
241242
tracing: Tracing.ACTIVE,
242243
architecture: Architecture.X86_64,
243244
memorySize: 256, // Default value (128) will take too long to process

0 commit comments

Comments
 (0)