-
Notifications
You must be signed in to change notification settings - Fork 146
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
feat(metrics): rename method purgeStoredMetrics to publishStoredMetrics #377
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,9 @@ | |
// SPDX-License-Identifier: MIT-0 | ||
|
||
/** | ||
* Test metrics decorator | ||
* Test metrics standard functions | ||
* | ||
* @group e2e/metrics/decorator | ||
* @group e2e/metrics/standardFunctions | ||
*/ | ||
|
||
import { randomUUID } from 'crypto'; | ||
|
@@ -19,43 +19,44 @@ const cloudwatchClient = new AWS.CloudWatch(); | |
const lambdaClient = new AWS.Lambda(); | ||
|
||
const integTestApp = new App(); | ||
const stack = new Stack(integTestApp, 'ExampleIntegTest'); | ||
const stack = new Stack(integTestApp, 'MetricsE2EStandardFunctionsStack'); | ||
|
||
describe('coldstart', () => { | ||
it('can be deploy succcessfully', async () => { | ||
// GIVEN | ||
const startTime = new Date(); | ||
const expectedNamespace = randomUUID(); // to easily find metrics back at assert phase | ||
const expectedServiceName = 'MyFunctionWithStandardHandler'; | ||
const expectedMetricName = 'MyMetric'; | ||
const expectedMetricUnit = MetricUnits.Count; | ||
const expectedMetricValue = '1'; | ||
const expectedDefaultDimensions = { MyDimension: 'MyValue' }; | ||
const expectedExtraDimension = { MyExtraDimension: 'MyExtraValue' }; | ||
const expectedSingleMetricDimension = { MySingleMetricDim: 'MySingleValue' }; | ||
const expectedSingleMetricName = 'MySingleMetric'; | ||
const expectedSingleMetricUnit = MetricUnits.Percent; | ||
const expectedSingleMetricValue = '2'; | ||
const functionName = 'MyFunctionWithStandardHandler'; | ||
new lambda.NodejsFunction(stack, 'MyFunction', { | ||
functionName: functionName, | ||
environment: { | ||
EXPECTED_NAMESPACE: expectedNamespace, | ||
EXPECTED_SERVICE_NAME: expectedServiceName, | ||
EXPECTED_METRIC_NAME: expectedMetricName, | ||
EXPECTED_METRIC_UNIT: expectedMetricUnit, | ||
EXPECTED_METRIC_VALUE: expectedMetricValue, | ||
EXPECTED_DEFAULT_DIMENSIONS: JSON.stringify(expectedDefaultDimensions), | ||
EXPECTED_EXTRA_DIMENSION: JSON.stringify(expectedExtraDimension), | ||
EXPECTED_SINGLE_METRIC_DIMENSION: JSON.stringify(expectedSingleMetricDimension), | ||
EXPECTED_SINGLE_METRIC_NAME: expectedSingleMetricName, | ||
EXPECTED_SINGLE_METRIC_UNIT: expectedSingleMetricUnit, | ||
EXPECTED_SINGLE_METRIC_VALUE: expectedSingleMetricValue, | ||
}, | ||
}); | ||
// GIVEN | ||
const startTime = new Date(); | ||
const expectedNamespace = randomUUID(); // to easily find metrics back at assert phase | ||
const expectedServiceName = 'MyFunctionWithStandardHandler'; | ||
const expectedMetricName = 'MyMetric'; | ||
const expectedMetricUnit = MetricUnits.Count; | ||
const expectedMetricValue = '1'; | ||
const expectedDefaultDimensions = { MyDimension: 'MyValue' }; | ||
const expectedExtraDimension = { MyExtraDimension: 'MyExtraValue' }; | ||
const expectedSingleMetricDimension = { MySingleMetricDim: 'MySingleValue' }; | ||
const expectedSingleMetricName = 'MySingleMetric'; | ||
const expectedSingleMetricUnit = MetricUnits.Percent; | ||
const expectedSingleMetricValue = '2'; | ||
const functionName = 'MyFunctionWithStandardHandler'; | ||
new lambda.NodejsFunction(stack, 'MyFunction', { | ||
functionName: functionName, | ||
tracing: Tracing.ACTIVE, | ||
environment: { | ||
EXPECTED_NAMESPACE: expectedNamespace, | ||
EXPECTED_SERVICE_NAME: expectedServiceName, | ||
EXPECTED_METRIC_NAME: expectedMetricName, | ||
EXPECTED_METRIC_UNIT: expectedMetricUnit, | ||
EXPECTED_METRIC_VALUE: expectedMetricValue, | ||
EXPECTED_DEFAULT_DIMENSIONS: JSON.stringify(expectedDefaultDimensions), | ||
EXPECTED_EXTRA_DIMENSION: JSON.stringify(expectedExtraDimension), | ||
EXPECTED_SINGLE_METRIC_DIMENSION: JSON.stringify(expectedSingleMetricDimension), | ||
EXPECTED_SINGLE_METRIC_NAME: expectedSingleMetricName, | ||
EXPECTED_SINGLE_METRIC_UNIT: expectedSingleMetricUnit, | ||
EXPECTED_SINGLE_METRIC_VALUE: expectedSingleMetricValue, | ||
}, | ||
}); | ||
|
||
const stackArtifact = integTestApp.synth().getStackByName(stack.stackName); | ||
const stackArtifact = integTestApp.synth().getStackByName(stack.stackName); | ||
|
||
describe('happy cases', () => { | ||
beforeAll(async () => { | ||
const sdkProvider = await SdkProvider.withAwsCliCompatibleDefaults({ | ||
profile: process.env.AWS_PROFILE, | ||
}); | ||
|
@@ -66,7 +67,11 @@ describe('coldstart', () => { | |
await cloudFormation.deployStack({ | ||
stack: stackArtifact, | ||
}); | ||
// and invoked | ||
}, 200000); | ||
|
||
it('capture ColdStart Metric', async () => { | ||
// WHEN | ||
// invoked | ||
await lambdaClient | ||
.invoke({ | ||
FunctionName: functionName, | ||
|
@@ -106,27 +111,82 @@ describe('coldstart', () => { | |
MetricName: 'ColdStart', | ||
Statistics: ['Sum'], | ||
}, | ||
undefined, | ||
undefined | ||
) | ||
.promise(); | ||
|
||
// Despite lambda has been called twice, coldstart metric sum should only be 1 | ||
const singleDataPoint = coldStartMetricStat.Datapoints ? coldStartMetricStat.Datapoints[0] : {}; | ||
expect(singleDataPoint.Sum).toBe(1); | ||
}, 9000000); | ||
}); | ||
}, 15000); | ||
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. (minor) timeout is in ms. Will 15 sec be sufficient? 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. Yes the test itself is quite fast. but since we sleep for 10s to get metrics ingested it still takes around 11sec |
||
|
||
afterAll(async () => { | ||
if (!process.env.DISABLE_TEARDOWN) { | ||
const stackArtifact = integTestApp.synth().getStackByName(stack.stackName); | ||
it('produce added Metric with the default and extra one dimensions', async () => { | ||
// GIVEN | ||
const invocationCount = 2; | ||
|
||
const sdkProvider = await SdkProvider.withAwsCliCompatibleDefaults({ | ||
profile: process.env.AWS_PROFILE, | ||
}); | ||
const cloudFormation = new CloudFormationDeployments({ sdkProvider }); | ||
// WHEN | ||
// invoked | ||
for (let i = 0; i < invocationCount; i++) { | ||
await lambdaClient | ||
.invoke({ | ||
FunctionName: functionName, | ||
}) | ||
.promise(); | ||
} | ||
|
||
await cloudFormation.destroyStack({ | ||
stack: stackArtifact, | ||
}); | ||
} | ||
}, 9000000); | ||
// THEN | ||
// sleep to allow metrics to be collected | ||
await new Promise((resolve) => setTimeout(resolve, 10000)); | ||
|
||
// Check metric dimensions | ||
const metrics = await cloudwatchClient | ||
.listMetrics({ | ||
Namespace: expectedNamespace, | ||
MetricName: expectedMetricName, | ||
}) | ||
.promise(); | ||
expect(metrics.Metrics?.length).toBe(1); | ||
const metric = metrics.Metrics?.[0]; | ||
const expectedDimensions = [ | ||
{ Name: 'service', Value: expectedServiceName }, | ||
{ Name: Object.keys(expectedDefaultDimensions)[0], Value: expectedDefaultDimensions.MyDimension }, | ||
{ Name: Object.keys(expectedExtraDimension)[0], Value: expectedExtraDimension.MyExtraDimension }, | ||
]; | ||
expect(metric?.Dimensions).toStrictEqual(expectedDimensions); | ||
|
||
// Check coldstart metric value | ||
const metricStat = await cloudwatchClient | ||
.getMetricStatistics( | ||
{ | ||
Namespace: expectedNamespace, | ||
StartTime: new Date(startTime.getTime() - 60 * 1000), // minus 1 minute, | ||
Dimensions: expectedDimensions, | ||
EndTime: new Date(new Date().getTime() + 60 * 1000), | ||
Period: 60, | ||
MetricName: expectedMetricName, | ||
Statistics: ['Sum'], | ||
}, | ||
undefined | ||
) | ||
.promise(); | ||
|
||
// Since lambda has been called twice in this test and potentially more in others, metric sum should be at least of expectedMetricValue * invocationCount | ||
const singleDataPoint = metricStat.Datapoints ? metricStat.Datapoints[0] : {}; | ||
expect(singleDataPoint.Sum).toBeGreaterThanOrEqual(parseInt(expectedMetricValue) * invocationCount); | ||
}, 15000); | ||
|
||
afterAll(async () => { | ||
if (!process.env.DISABLE_TEARDOWN) { | ||
const stackArtifact = integTestApp.synth().getStackByName(stack.stackName); | ||
|
||
const sdkProvider = await SdkProvider.withAwsCliCompatibleDefaults({ | ||
profile: process.env.AWS_PROFILE, | ||
}); | ||
const cloudFormation = new CloudFormationDeployments({ sdkProvider }); | ||
|
||
await cloudFormation.destroyStack({ | ||
stack: stackArtifact, | ||
}); | ||
} | ||
}, 200000); | ||
}); |
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.
A following PR will focus on refactoring e2e test to reuse tracing one