7
7
TestStack ,
8
8
TestDynamodbTable ,
9
9
} from '@aws-lambda-powertools/testing-utils' ;
10
- import { XRayClient } from '@aws-sdk/client-xray' ;
11
10
import { join } from 'node:path' ;
12
11
import { TracerTestNodejsFunction } from '../helpers/resources' ;
13
12
import {
@@ -16,7 +15,6 @@ import {
16
15
} from '../helpers/traceAssertions' ;
17
16
import {
18
17
getFirstSubsegment ,
19
- getFunctionArn ,
20
18
getInvocationSubsegment ,
21
19
getTraces ,
22
20
invokeAllTestCases ,
@@ -141,7 +139,6 @@ describe(`Tracer E2E tests, all features with decorator instantiation`, () => {
141
139
) ;
142
140
testTable . grantWriteData ( fnCaptureResponseOff ) ;
143
141
144
- const xrayClient = new XRayClient ( { } ) ;
145
142
const invocationCount = 3 ;
146
143
147
144
beforeAll ( async ( ) => {
@@ -178,28 +175,23 @@ describe(`Tracer E2E tests, all features with decorator instantiation`, () => {
178
175
const { EXPECTED_CUSTOM_ERROR_MESSAGE : expectedCustomErrorMessage } =
179
176
commonEnvironmentVars ;
180
177
181
- const tracesWhenAllFlagsEnabled = await getTraces (
182
- xrayClient ,
178
+ /**
179
+ * The trace should have 4 segments:
180
+ * 1. Lambda Context (AWS::Lambda)
181
+ * 2. Lambda Function (AWS::Lambda::Function)
182
+ * 4. DynamoDB (AWS::DynamoDB)
183
+ * 4. Remote call (docs.powertools.aws.dev)
184
+ */
185
+ const tracesWhenAllFlagsEnabled = await getTraces ( {
183
186
startTime,
184
- await getFunctionArn ( fnNameAllFlagsEnabled ) ,
185
- invocationCount ,
186
- 4
187
- ) ;
188
-
189
- expect ( tracesWhenAllFlagsEnabled . length ) . toBe ( invocationCount ) ;
187
+ resourceName : fnNameAllFlagsEnabled ,
188
+ expectedTracesCount : invocationCount ,
189
+ expectedSegmentsCount : 4 ,
190
+ } ) ;
190
191
191
192
// Assess
192
193
for ( let i = 0 ; i < invocationCount ; i ++ ) {
193
194
const trace = tracesWhenAllFlagsEnabled [ i ] ;
194
-
195
- /**
196
- * Expect the trace to have 4 segments:
197
- * 1. Lambda Context (AWS::Lambda)
198
- * 2. Lambda Function (AWS::Lambda::Function)
199
- * 4. DynamoDB (AWS::DynamoDB)
200
- * 4. Remote call (docs.powertools.aws.dev)
201
- */
202
- expect ( trace . Segments . length ) . toBe ( 4 ) ;
203
195
const invocationSubsegment = getInvocationSubsegment ( trace ) ;
204
196
205
197
/**
@@ -246,13 +238,12 @@ describe(`Tracer E2E tests, all features with decorator instantiation`, () => {
246
238
EXPECTED_CUSTOM_RESPONSE_VALUE : expectedCustomResponseValue ,
247
239
} = commonEnvironmentVars ;
248
240
249
- const tracesWhenAllFlagsEnabled = await getTraces (
250
- xrayClient ,
241
+ const tracesWhenAllFlagsEnabled = await getTraces ( {
251
242
startTime,
252
- await getFunctionArn ( fnNameAllFlagsEnabled ) ,
253
- invocationCount ,
254
- 4
255
- ) ;
243
+ resourceName : fnNameAllFlagsEnabled ,
244
+ expectedTracesCount : invocationCount ,
245
+ expectedSegmentsCount : 4 ,
246
+ } ) ;
256
247
257
248
for ( let i = 0 ; i < invocationCount ; i ++ ) {
258
249
const trace = tracesWhenAllFlagsEnabled [ i ] ;
@@ -291,30 +282,24 @@ describe(`Tracer E2E tests, all features with decorator instantiation`, () => {
291
282
it (
292
283
'should not capture error nor response when the flags are false' ,
293
284
async ( ) => {
294
- const tracesWithNoCaptureErrorOrResponse = await getTraces (
295
- xrayClient ,
285
+ /**
286
+ * Expect the trace to have 4 segments:
287
+ * 1. Lambda Context (AWS::Lambda)
288
+ * 2. Lambda Function (AWS::Lambda::Function)
289
+ * 3. DynamoDB (AWS::DynamoDB)
290
+ * 4. Remote call (docs.powertools.aws.dev)
291
+ */
292
+ const tracesWithNoCaptureErrorOrResponse = await getTraces ( {
296
293
startTime,
297
- await getFunctionArn ( fnNameNoCaptureErrorOrResponse ) ,
298
- invocationCount ,
299
- 4
300
- ) ;
301
-
302
- expect ( tracesWithNoCaptureErrorOrResponse . length ) . toBe ( invocationCount ) ;
294
+ resourceName : fnNameNoCaptureErrorOrResponse ,
295
+ expectedTracesCount : invocationCount ,
296
+ expectedSegmentsCount : 4 ,
297
+ } ) ;
303
298
304
299
// Assess
305
300
for ( let i = 0 ; i < invocationCount ; i ++ ) {
306
301
const trace = tracesWithNoCaptureErrorOrResponse [ i ] ;
307
-
308
- /**
309
- * Expect the trace to have 4 segments:
310
- * 1. Lambda Context (AWS::Lambda)
311
- * 2. Lambda Function (AWS::Lambda::Function)
312
- * 3. DynamoDB (AWS::DynamoDB)
313
- * 4. Remote call (docs.powertools.aws.dev)
314
- */
315
- expect ( trace . Segments . length ) . toBe ( 4 ) ;
316
302
const invocationSubsegment = getInvocationSubsegment ( trace ) ;
317
-
318
303
/**
319
304
* Invocation subsegment should have a subsegment '## index.handler' (default behavior for Tracer)
320
305
* '## index.handler' subsegment should have 3 subsegments
@@ -358,30 +343,24 @@ describe(`Tracer E2E tests, all features with decorator instantiation`, () => {
358
343
const { EXPECTED_CUSTOM_ERROR_MESSAGE : expectedCustomErrorMessage } =
359
344
commonEnvironmentVars ;
360
345
361
- const tracesWithCaptureResponseFalse = await getTraces (
362
- xrayClient ,
346
+ /**
347
+ * Expect the trace to have 4 segments:
348
+ * 1. Lambda Context (AWS::Lambda)
349
+ * 2. Lambda Function (AWS::Lambda::Function)
350
+ * 3. DynamoDB (AWS::DynamoDB)
351
+ * 4. Remote call (docs.powertools.aws.dev)
352
+ */
353
+ const tracesWithCaptureResponseFalse = await getTraces ( {
363
354
startTime,
364
- await getFunctionArn ( fnNameCaptureResponseOff ) ,
365
- invocationCount ,
366
- 4
367
- ) ;
368
-
369
- expect ( tracesWithCaptureResponseFalse . length ) . toBe ( invocationCount ) ;
355
+ resourceName : fnNameCaptureResponseOff ,
356
+ expectedTracesCount : invocationCount ,
357
+ expectedSegmentsCount : 4 ,
358
+ } ) ;
370
359
371
360
// Assess
372
361
for ( let i = 0 ; i < invocationCount ; i ++ ) {
373
362
const trace = tracesWithCaptureResponseFalse [ i ] ;
374
-
375
- /**
376
- * Expect the trace to have 4 segments:
377
- * 1. Lambda Context (AWS::Lambda)
378
- * 2. Lambda Function (AWS::Lambda::Function)
379
- * 3. DynamoDB (AWS::DynamoDB)
380
- * 4. Remote call (docs.powertools.aws.dev)
381
- */
382
- expect ( trace . Segments . length ) . toBe ( 4 ) ;
383
363
const invocationSubsegment = getInvocationSubsegment ( trace ) ;
384
-
385
364
/**
386
365
* Invocation subsegment should have a subsegment '## index.handler' (default behavior for Tracer)
387
366
* '## index.handler' subsegment should have 3 subsegments
@@ -428,22 +407,16 @@ describe(`Tracer E2E tests, all features with decorator instantiation`, () => {
428
407
it (
429
408
'should not capture any custom traces when disabled' ,
430
409
async ( ) => {
431
- const expectedNoOfTraces = 2 ;
432
- const tracesWithTracerDisabled = await getTraces (
433
- xrayClient ,
410
+ const tracesWithTracerDisabled = await getTraces ( {
434
411
startTime,
435
- await getFunctionArn ( fnNameTracerDisabled ) ,
436
- invocationCount ,
437
- expectedNoOfTraces
438
- ) ;
439
-
440
- expect ( tracesWithTracerDisabled . length ) . toBe ( invocationCount ) ;
412
+ resourceName : fnNameTracerDisabled ,
413
+ expectedTracesCount : invocationCount ,
414
+ expectedSegmentsCount : 2 ,
415
+ } ) ;
441
416
442
417
// Assess
443
418
for ( let i = 0 ; i < invocationCount ; i ++ ) {
444
419
const trace = tracesWithTracerDisabled [ i ] ;
445
- expect ( trace . Segments . length ) . toBe ( 2 ) ;
446
-
447
420
/**
448
421
* Expect no subsegment in the invocation
449
422
*/
0 commit comments