@@ -78,6 +78,13 @@ const uuidFunction3 = v4();
78
78
const functionNameWithTracerDisabled = generateUniqueName ( RESOURCE_NAME_PREFIX , uuidFunction3 , runtime , 'AllFeatures-Middy-TracerDisabled' ) ;
79
79
const serviceNameWithTracerDisabled = functionNameWithNoCaptureErrorOrResponse ;
80
80
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
+
81
88
const xray = new AWS . XRay ( ) ;
82
89
const invocations = 3 ;
83
90
@@ -149,13 +156,30 @@ describe(`Tracer E2E tests, all features with middy instantiation for runtime: $
149
156
} ) ;
150
157
ddbTable . grantWriteData ( functionWithTracerDisabled ) ;
151
158
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
+
152
175
await deployStack ( integTestApp , stack ) ;
153
176
154
177
// Act
155
178
await Promise . all ( [
156
179
invokeAllTestCases ( functionNameWithAllFlagsEnabled ) ,
157
180
invokeAllTestCases ( functionNameWithNoCaptureErrorOrResponse ) ,
158
181
invokeAllTestCases ( functionNameWithTracerDisabled ) ,
182
+ invokeAllTestCases ( functionNameWithNoCaptureResponseViaMiddlewareOption ) ,
159
183
] ) ;
160
184
161
185
} , SETUP_TIMEOUT ) ;
@@ -299,6 +323,54 @@ describe(`Tracer E2E tests, all features with middy instantiation for runtime: $
299
323
300
324
} , TEST_CASE_TIMEOUT ) ;
301
325
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
+
302
374
it ( 'should not capture any custom traces when disabled' , async ( ) => {
303
375
const expectedNoOfTraces = 2 ;
304
376
const tracesWithTracerDisabled = await getTraces ( xray , startTime , await getFunctionArn ( functionNameWithTracerDisabled ) , invocations , expectedNoOfTraces ) ;
0 commit comments