@@ -78,6 +78,13 @@ const uuidFunction3 = v4();
78
78
const functionNameWithTracerDisabled = generateUniqueName ( RESOURCE_NAME_PREFIX , uuidFunction3 , runtime , 'AllFeatures-Decorator-TracerDisabled' ) ;
79
79
const serviceNameWithTracerDisabled = functionNameWithNoCaptureErrorOrResponse ;
80
80
81
+ /**
82
+ * Function #4 disables tracer
83
+ */
84
+ const uuidFunction4 = v4 ( ) ;
85
+ const functionNameWithCaptureResponseFalse = generateUniqueName ( RESOURCE_NAME_PREFIX , uuidFunction4 , runtime , 'AllFeatures-Decorator-CaptureResponseFalse' ) ;
86
+ const serviceNameWithCaptureResponseFalse = functionNameWithCaptureResponseFalse ;
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 decorator instantiation for runtim
149
156
} ) ;
150
157
ddbTable . grantWriteData ( functionWithTracerDisabled ) ;
151
158
159
+ const functionWithCaptureResponseFalse = createTracerTestFunction ( {
160
+ stack,
161
+ functionName : functionNameWithCaptureResponseFalse ,
162
+ handler : 'handlerWithCaptureResponseFalse' ,
163
+ entry,
164
+ expectedServiceName : serviceNameWithCaptureResponseFalse ,
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 ( functionWithCaptureResponseFalse ) ;
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 ( functionNameWithCaptureResponseFalse ) ,
159
183
] ) ;
160
184
161
185
} , SETUP_TIMEOUT ) ;
@@ -303,6 +327,62 @@ describe(`Tracer E2E tests, all features with decorator instantiation for runtim
303
327
304
328
} , TEST_CASE_TIMEOUT ) ;
305
329
330
+ it ( 'should not capture response when the decorator\'s captureResponse is set to false' , async ( ) => {
331
+
332
+ const tracesWithCaptureResponseFalse = await getTraces ( xray , startTime , await getFunctionArn ( functionNameWithCaptureResponseFalse ) , invocations , 5 ) ;
333
+
334
+ expect ( tracesWithCaptureResponseFalse . length ) . toBe ( invocations ) ;
335
+
336
+ // Assess
337
+ for ( let i = 0 ; i < invocations ; i ++ ) {
338
+ const trace = tracesWithCaptureResponseFalse [ i ] ;
339
+
340
+ /**
341
+ * Expect the trace to have 5 segments:
342
+ * 1. Lambda Context (AWS::Lambda)
343
+ * 2. Lambda Function (AWS::Lambda::Function)
344
+ * 3. DynamoDB (AWS::DynamoDB)
345
+ * 4. DynamoDB Table (AWS::DynamoDB::Table)
346
+ * 5. Remote call (httpbin.org)
347
+ */
348
+ expect ( trace . Segments . length ) . toBe ( 5 ) ;
349
+ const invocationSubsegment = getInvocationSubsegment ( trace ) ;
350
+
351
+ /**
352
+ * Invocation subsegment should have a subsegment '## index.handler' (default behavior for PowerTool tracer)
353
+ * '## index.handler' subsegment should have 4 subsegments
354
+ * 1. DynamoDB (PutItem on the table)
355
+ * 2. DynamoDB (PutItem overhead)
356
+ * 3. httpbin.org (Remote call)
357
+ * 4. '### myMethod' (method decorator)
358
+ */
359
+ const handlerSubsegment = getFirstSubsegment ( invocationSubsegment ) ;
360
+ expect ( handlerSubsegment . name ) . toBe ( '## index.handlerWithCaptureResponseFalse' ) ;
361
+ expect ( handlerSubsegment ?. subsegments ) . toHaveLength ( 4 ) ;
362
+
363
+ if ( ! handlerSubsegment . subsegments ) {
364
+ fail ( '"## index.handlerWithCaptureResponseFalse" subsegment should have subsegments' ) ;
365
+ }
366
+ const subsegments = splitSegmentsByName ( handlerSubsegment . subsegments , [ 'DynamoDB' , 'httpbin.org' , '### myMethod' ] ) ;
367
+ expect ( subsegments . get ( 'DynamoDB' ) ?. length ) . toBe ( 2 ) ;
368
+ expect ( subsegments . get ( 'httpbin.org' ) ?. length ) . toBe ( 1 ) ;
369
+ expect ( subsegments . get ( '### myMethod' ) ?. length ) . toBe ( 1 ) ;
370
+ expect ( subsegments . get ( 'other' ) ?. length ) . toBe ( 0 ) ;
371
+
372
+ // No metadata because capturing the response was disabled and that's
373
+ // the only metadata that could be in the subsegment for the test.
374
+ const myMethodSegment = subsegments . get ( '### myMethod' ) ?. [ 0 ] ;
375
+ expect ( myMethodSegment ) . toBeDefined ( ) ;
376
+ expect ( myMethodSegment ) . not . toHaveProperty ( 'metadata' ) ;
377
+
378
+ const shouldThrowAnError = ( i === ( invocations - 1 ) ) ;
379
+ if ( shouldThrowAnError ) {
380
+ assertErrorAndFault ( invocationSubsegment , expectedCustomErrorMessage ) ;
381
+ }
382
+ }
383
+
384
+ } , TEST_CASE_TIMEOUT ) ;
385
+
306
386
it ( 'should not capture any custom traces when disabled' , async ( ) => {
307
387
const expectedNoOfTraces = 2 ;
308
388
const tracesWithTracerDisabled = await getTraces ( xray , startTime , await getFunctionArn ( functionNameWithTracerDisabled ) , invocations , expectedNoOfTraces ) ;
0 commit comments