-
Notifications
You must be signed in to change notification settings - Fork 159
test(tracer): make e2e tests to follow the same convention in Logger and Metrics #788
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
849e890
test(tracer): refactor to pass runtime info + extract code to add a L…
ijemmy 0f1e56c
test(tracer): refactor to extract common used values out
ijemmy 33e8c70
test: refactor to take manual test out in its own separated file
ijemmy 1f5e8ea
test(tracer): refactor to move reusable code to functions
ijemmy 8e86295
test(tracer): add middy test + refactor to make it more readable
ijemmy db3ee54
test(tracer): Add two variant of middy case
ijemmy 90c455a
test(tracer): Add decorator cases
ijemmy 8516b88
test:(tracer): add async handler test cases
ijemmy 6f4e50e
test:(tracer) remove unused code
ijemmy 9f4391c
test(tracer): update comment + fix incorrect test case name
ijemmy 6e571a3
test(tracer): add targets to run test in both runtimes
ijemmy 73e8e87
test(tracer): make import relative and put relative imports after
ijemmy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
331 changes: 331 additions & 0 deletions
331
packages/tracing/tests/e2e/allFeatures.decorator.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,331 @@ | ||
/** | ||
* Test tracer in decorator setup | ||
* | ||
* @group e2e/tracer/decorator | ||
*/ | ||
|
||
import { randomUUID } from 'crypto'; | ||
import path from 'path'; | ||
import { Table, AttributeType, BillingMode } from 'aws-cdk-lib/aws-dynamodb'; | ||
import { App, Stack, RemovalPolicy } from 'aws-cdk-lib'; | ||
import * as AWS from 'aws-sdk'; | ||
import { deployStack, destroyStack } from '../../../commons/tests/utils/cdk-cli'; | ||
import { | ||
getTraces, | ||
getInvocationSubsegment, | ||
splitSegmentsByName, | ||
invokeAllTestCases, | ||
createTracerTestFunction, | ||
getFunctionArn, | ||
getFirstSubsegment, | ||
} from '../helpers/tracesUtils'; | ||
import { | ||
generateUniqueName, | ||
isValidRuntimeKey, | ||
} from '../../../commons/tests/utils/e2eUtils'; | ||
import { | ||
RESOURCE_NAME_PREFIX, | ||
SETUP_TIMEOUT, | ||
TEARDOWN_TIMEOUT, | ||
TEST_CASE_TIMEOUT, | ||
expectedCustomAnnotationKey, | ||
expectedCustomAnnotationValue, | ||
expectedCustomMetadataKey, | ||
expectedCustomMetadataValue, | ||
expectedCustomResponseValue, | ||
expectedCustomErrorMessage, | ||
} from './constants'; | ||
import { | ||
assertAnnotation, | ||
assertErrorAndFault, | ||
} from '../helpers/traceAssertions'; | ||
|
||
const runtime: string = process.env.RUNTIME || 'nodejs14x'; | ||
|
||
if (!isValidRuntimeKey(runtime)) { | ||
throw new Error(`Invalid runtime key value: ${runtime}`); | ||
} | ||
|
||
/** | ||
* We will create a stack with 3 Lambda functions: | ||
* 1. With all flags enabled (capture both response and error) | ||
* 2. Do not capture error or response | ||
* 3. Do not enable tracer | ||
* Each stack must use a unique `serviceName` as it's used to for retrieving the trace. | ||
* Using the same one will result in traces from different test cases mixing up. | ||
*/ | ||
const stackName = generateUniqueName(RESOURCE_NAME_PREFIX, randomUUID(), runtime, 'AllFeatures-Decorator'); | ||
const lambdaFunctionCodeFile = 'allFeatures.decorator.test.functionCode.ts'; | ||
let startTime: Date; | ||
|
||
/** | ||
* Function #1 is with all flags enabled. | ||
*/ | ||
const uuidFunction1 = randomUUID(); | ||
const functionNameWithAllFlagsEnabled = generateUniqueName(RESOURCE_NAME_PREFIX, uuidFunction1, runtime, 'AllFeatures-Decoratory-AllFlagsEnabled'); | ||
const serviceNameWithAllFlagsEnabled = functionNameWithAllFlagsEnabled; | ||
|
||
/** | ||
* Function #2 doesn't capture error or response | ||
*/ | ||
const uuidFunction2 = randomUUID(); | ||
const functionNameWithNoCaptureErrorOrResponse = generateUniqueName(RESOURCE_NAME_PREFIX, uuidFunction2, runtime, 'AllFeatures-Decorator-NoCaptureErrorOrResponse'); | ||
const serviceNameWithNoCaptureErrorOrResponse = functionNameWithNoCaptureErrorOrResponse; | ||
/** | ||
* Function #3 disables tracer | ||
*/ | ||
const uuidFunction3 = randomUUID(); | ||
const functionNameWithTracerDisabled = generateUniqueName(RESOURCE_NAME_PREFIX, uuidFunction3, runtime, 'AllFeatures-Decorator-TracerDisabled'); | ||
const serviceNameWithTracerDisabled = functionNameWithNoCaptureErrorOrResponse; | ||
|
||
const xray = new AWS.XRay(); | ||
const invocations = 3; | ||
|
||
const integTestApp = new App(); | ||
let stack: Stack; | ||
|
||
describe(`Tracer E2E tests, all features with decorator instantiation for runtime: ${runtime}`, () => { | ||
|
||
beforeAll(async () => { | ||
|
||
// Prepare | ||
startTime = new Date(); | ||
const ddbTableName = stackName + '-table'; | ||
stack = new Stack(integTestApp, stackName); | ||
|
||
const ddbTable = new Table(stack, 'Table', { | ||
tableName: ddbTableName, | ||
partitionKey: { | ||
name: 'id', | ||
type: AttributeType.STRING | ||
}, | ||
billingMode: BillingMode.PAY_PER_REQUEST, | ||
removalPolicy: RemovalPolicy.DESTROY | ||
}); | ||
|
||
const entry = path.join(__dirname, lambdaFunctionCodeFile); | ||
const functionWithAllFlagsEnabled = createTracerTestFunction({ | ||
stack, | ||
functionName: functionNameWithAllFlagsEnabled, | ||
entry, | ||
expectedServiceName: serviceNameWithAllFlagsEnabled, | ||
environmentParams: { | ||
TEST_TABLE_NAME: ddbTableName, | ||
POWERTOOLS_TRACER_CAPTURE_RESPONSE: 'true', | ||
POWERTOOLS_TRACER_CAPTURE_ERROR: 'true', | ||
POWERTOOLS_TRACE_ENABLED: 'true', | ||
}, | ||
runtime | ||
}); | ||
ddbTable.grantWriteData(functionWithAllFlagsEnabled); | ||
|
||
const functionThatDoesNotCapturesErrorAndResponse = createTracerTestFunction({ | ||
stack, | ||
functionName: functionNameWithNoCaptureErrorOrResponse, | ||
entry, | ||
expectedServiceName: serviceNameWithNoCaptureErrorOrResponse, | ||
environmentParams: { | ||
TEST_TABLE_NAME: ddbTableName, | ||
POWERTOOLS_TRACER_CAPTURE_RESPONSE: 'false', | ||
POWERTOOLS_TRACER_CAPTURE_ERROR: 'false', | ||
POWERTOOLS_TRACE_ENABLED: 'true', | ||
}, | ||
runtime | ||
}); | ||
ddbTable.grantWriteData(functionThatDoesNotCapturesErrorAndResponse); | ||
|
||
const functionWithTracerDisabled = createTracerTestFunction({ | ||
stack, | ||
functionName: functionNameWithTracerDisabled, | ||
entry, | ||
expectedServiceName: serviceNameWithTracerDisabled, | ||
environmentParams: { | ||
TEST_TABLE_NAME: ddbTableName, | ||
POWERTOOLS_TRACER_CAPTURE_RESPONSE: 'true', | ||
POWERTOOLS_TRACER_CAPTURE_ERROR: 'true', | ||
POWERTOOLS_TRACE_ENABLED: 'false', | ||
}, | ||
runtime | ||
}); | ||
ddbTable.grantWriteData(functionWithTracerDisabled); | ||
|
||
await deployStack(integTestApp, stack); | ||
|
||
// Act | ||
await Promise.all([ | ||
invokeAllTestCases(functionNameWithAllFlagsEnabled), | ||
invokeAllTestCases(functionNameWithNoCaptureErrorOrResponse), | ||
invokeAllTestCases(functionNameWithTracerDisabled), | ||
]); | ||
|
||
}, SETUP_TIMEOUT); | ||
|
||
afterAll(async () => { | ||
if (!process.env.DISABLE_TEARDOWN) { | ||
await destroyStack(integTestApp, stack); | ||
} | ||
}, TEARDOWN_TIMEOUT); | ||
|
||
it('should generate all custom traces', async () => { | ||
|
||
const tracesWhenAllFlagsEnabled = await getTraces(xray, startTime, await getFunctionArn(functionNameWithAllFlagsEnabled), invocations, 5); | ||
|
||
expect(tracesWhenAllFlagsEnabled.length).toBe(invocations); | ||
|
||
// Assess | ||
for (let i = 0; i < invocations; i++) { | ||
const trace = tracesWhenAllFlagsEnabled[i]; | ||
|
||
/** | ||
* Expect the trace to have 5 segments: | ||
* 1. Lambda Context (AWS::Lambda) | ||
* 2. Lambda Function (AWS::Lambda::Function) | ||
* 3. DynamoDB (AWS::DynamoDB) | ||
* 4. DynamoDB Table (AWS::DynamoDB::Table) | ||
* 5. Remote call (httpbin.org) | ||
*/ | ||
expect(trace.Segments.length).toBe(5); | ||
const invocationSubsegment = getInvocationSubsegment(trace); | ||
|
||
/** | ||
* Invocation subsegment should have a subsegment '## index.handler' (default behavior for PowerTool tracer) | ||
* '## index.handler' subsegment should have 4 subsegments | ||
* 1. DynamoDB (PutItem on the table) | ||
* 2. DynamoDB (PutItem overhead) | ||
* 3. httpbin.org (Remote call) | ||
* 4. '### myMethod' (method decorator) | ||
*/ | ||
const handlerSubsegment = getFirstSubsegment(invocationSubsegment); | ||
expect(handlerSubsegment.name).toBe('## index.handler'); | ||
expect(handlerSubsegment?.subsegments).toHaveLength(4); | ||
|
||
if (!handlerSubsegment.subsegments) { | ||
fail('"## index.handler" subsegment should have subsegments'); | ||
} | ||
const subsegments = splitSegmentsByName(handlerSubsegment.subsegments, [ 'DynamoDB', 'httpbin.org', '### myMethod' ]); | ||
expect(subsegments.get('DynamoDB')?.length).toBe(2); | ||
expect(subsegments.get('httpbin.org')?.length).toBe(1); | ||
expect(subsegments.get('### myMethod')?.length).toBe(1); | ||
expect(subsegments.get('other')?.length).toBe(0); | ||
|
||
const shouldThrowAnError = (i === (invocations - 1)); | ||
if (shouldThrowAnError) { | ||
assertErrorAndFault(invocationSubsegment, expectedCustomErrorMessage); | ||
} | ||
} | ||
|
||
}, TEST_CASE_TIMEOUT); | ||
|
||
it('should have correct annotations and metadata', async () => { | ||
const tracesWhenAllFlagsEnabled = await getTraces(xray, startTime, await getFunctionArn(functionNameWithAllFlagsEnabled), invocations, 5); | ||
|
||
for (let i = 0; i < invocations; i++) { | ||
const trace = tracesWhenAllFlagsEnabled[i]; | ||
const invocationSubsegment = getInvocationSubsegment(trace); | ||
const handlerSubsegment = getFirstSubsegment(invocationSubsegment); | ||
const { annotations, metadata } = handlerSubsegment; | ||
|
||
const isColdStart = (i === 0); | ||
assertAnnotation({ | ||
annotations, | ||
isColdStart, | ||
expectedServiceName: serviceNameWithAllFlagsEnabled, | ||
expectedCustomAnnotationKey, | ||
expectedCustomAnnotationValue, | ||
}); | ||
|
||
if (!metadata) { | ||
fail('metadata is missing'); | ||
} | ||
expect(metadata[serviceNameWithAllFlagsEnabled][expectedCustomMetadataKey]) | ||
.toEqual(expectedCustomMetadataValue); | ||
|
||
const shouldThrowAnError = (i === (invocations - 1)); | ||
if (!shouldThrowAnError) { | ||
// Assert that the metadata object contains the response | ||
expect(metadata[serviceNameWithAllFlagsEnabled]['index.handler response']) | ||
.toEqual(expectedCustomResponseValue); | ||
} | ||
} | ||
}, TEST_CASE_TIMEOUT); | ||
Comment on lines
+219
to
+250
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I break this part out of the existing test case to make it not too long. |
||
|
||
it('should not capture error nor response when the flags are false', async () => { | ||
|
||
const tracesWithNoCaptureErrorOrResponse = await getTraces(xray, startTime, await getFunctionArn(functionNameWithNoCaptureErrorOrResponse), invocations, 5); | ||
|
||
expect(tracesWithNoCaptureErrorOrResponse.length).toBe(invocations); | ||
|
||
// Assess | ||
for (let i = 0; i < invocations; i++) { | ||
const trace = tracesWithNoCaptureErrorOrResponse[i]; | ||
|
||
/** | ||
* Expect the trace to have 5 segments: | ||
* 1. Lambda Context (AWS::Lambda) | ||
* 2. Lambda Function (AWS::Lambda::Function) | ||
* 3. DynamoDB (AWS::DynamoDB) | ||
* 4. DynamoDB Table (AWS::DynamoDB::Table) | ||
* 5. Remote call (httpbin.org) | ||
*/ | ||
expect(trace.Segments.length).toBe(5); | ||
const invocationSubsegment = getInvocationSubsegment(trace); | ||
|
||
/** | ||
* Invocation subsegment should have a subsegment '## index.handler' (default behavior for PowerTool tracer) | ||
* '## index.handler' subsegment should have 4 subsegments | ||
* 1. DynamoDB (PutItem on the table) | ||
* 2. DynamoDB (PutItem overhead) | ||
* 3. httpbin.org (Remote call) | ||
* 4. '### myMethod' (method decorator) | ||
*/ | ||
const handlerSubsegment = getFirstSubsegment(invocationSubsegment); | ||
expect(handlerSubsegment.name).toBe('## index.handler'); | ||
expect(handlerSubsegment?.subsegments).toHaveLength(4); | ||
|
||
if (!handlerSubsegment.subsegments) { | ||
fail('"## index.handler" subsegment should have subsegments'); | ||
} | ||
const subsegments = splitSegmentsByName(handlerSubsegment.subsegments, [ 'DynamoDB', 'httpbin.org', '### myMethod' ]); | ||
expect(subsegments.get('DynamoDB')?.length).toBe(2); | ||
expect(subsegments.get('httpbin.org')?.length).toBe(1); | ||
expect(subsegments.get('### myMethod')?.length).toBe(1); | ||
expect(subsegments.get('other')?.length).toBe(0); | ||
|
||
const shouldThrowAnError = (i === (invocations - 1)); | ||
if (shouldThrowAnError) { | ||
// Assert that the subsegment has the expected fault | ||
expect(invocationSubsegment.error).toBe(true); | ||
expect(handlerSubsegment.error).toBe(true); | ||
// Assert that no error was captured on the subsegment | ||
expect(handlerSubsegment.hasOwnProperty('cause')).toBe(false); | ||
} | ||
Comment on lines
+294
to
+301
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the only unique part of this test case. |
||
} | ||
|
||
}, TEST_CASE_TIMEOUT); | ||
|
||
it('should not capture any custom traces when disabled', async () => { | ||
const expectedNoOfTraces = 2; | ||
const tracesWithTracerDisabled = await getTraces(xray, startTime, await getFunctionArn(functionNameWithTracerDisabled), invocations, expectedNoOfTraces); | ||
|
||
expect(tracesWithTracerDisabled.length).toBe(invocations); | ||
|
||
// Assess | ||
for (let i = 0; i < invocations; i++) { | ||
const trace = tracesWithTracerDisabled[i]; | ||
expect(trace.Segments.length).toBe(2); | ||
|
||
/** | ||
* Expect no subsegment in the invocation | ||
*/ | ||
const invocationSubsegment = getInvocationSubsegment(trace); | ||
expect(invocationSubsegment?.subsegments).toBeUndefined(); | ||
|
||
const shouldThrowAnError = (i === (invocations - 1)); | ||
if (shouldThrowAnError) { | ||
expect(invocationSubsegment.error).toBe(true); | ||
} | ||
} | ||
|
||
}, TEST_CASE_TIMEOUT); | ||
}); | ||
|
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of nested
if
. I use a helper function to ensure that the item isn't null. The check to please TypeScript compilers are moved to the helper method.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love this, thanks for simplyfing that indentation hell that I got us in :D