Skip to content

Commit 6d4ab75

Browse files
author
Dmitry Balabanov
authored
fix(tracer, metrics): use polling instead of fixed wait in e2e tests (#654)
1 parent 0c2fec6 commit 6d4ab75

File tree

8 files changed

+162
-99
lines changed

8 files changed

+162
-99
lines changed

Diff for: package-lock.json

+36-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: packages/metrics/package.json

+4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050
"main": "./lib/index.js",
5151
"types": "./lib/index.d.ts",
5252
"typedocMain": "src/index.ts",
53+
"devDependencies": {
54+
"@types/promise-retry": "^1.1.3",
55+
"promise-retry": "^2.0.1"
56+
},
5357
"files": [
5458
"lib"
5559
],

Diff for: packages/metrics/tests/e2e/decorator.test.ts

+15-23
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import { SdkProvider } from 'aws-cdk/lib/api/aws-auth';
1515
import { CloudFormationDeployments } from 'aws-cdk/lib/api/cloudformation-deployments';
1616
import * as AWS from 'aws-sdk';
1717
import { MetricUnits } from '../../src';
18+
import { getMetrics } from '../helpers/metricsUtils';
19+
20+
const ONE_MINUTE = 1000 * 60;
1821

1922
const cloudwatchClient = new AWS.CloudWatch();
2023
const lambdaClient = new AWS.Lambda();
@@ -80,10 +83,7 @@ describe('happy cases', () => {
8083
.promise();
8184
}
8285

83-
// THEN
84-
// sleep to allow metrics to be collected
85-
await new Promise((resolve) => setTimeout(resolve, 15000));
86-
}, 200000);
86+
}, ONE_MINUTE * 3);
8787

8888
it('capture ColdStart Metric', async () => {
8989
const expectedDimensions = [
@@ -92,12 +92,8 @@ describe('happy cases', () => {
9292
{ Name: Object.keys(expectedDefaultDimensions)[0], Value: expectedDefaultDimensions.MyDimension },
9393
];
9494
// Check coldstart metric dimensions
95-
const coldStartMetrics = await cloudwatchClient
96-
.listMetrics({
97-
Namespace: expectedNamespace,
98-
MetricName: 'ColdStart',
99-
})
100-
.promise();
95+
const coldStartMetrics = await getMetrics(cloudwatchClient, expectedNamespace, 'ColdStart', 1);
96+
10197
expect(coldStartMetrics.Metrics?.length).toBe(1);
10298
const coldStartMetric = coldStartMetrics.Metrics?.[0];
10399
expect(coldStartMetric?.Dimensions).toStrictEqual(expectedDimensions);
@@ -124,16 +120,12 @@ describe('happy cases', () => {
124120
// Despite lambda has been called twice, coldstart metric sum should only be 1
125121
const singleDataPoint = coldStartMetricStat.Datapoints ? coldStartMetricStat.Datapoints[0] : {};
126122
expect(singleDataPoint?.Sum).toBe(1);
127-
}, 15000);
123+
}, ONE_MINUTE * 3);
128124

129125
it('produce added Metric with the default and extra one dimensions', async () => {
130126
// Check metric dimensions
131-
const metrics = await cloudwatchClient
132-
.listMetrics({
133-
Namespace: expectedNamespace,
134-
MetricName: expectedMetricName,
135-
})
136-
.promise();
127+
const metrics = await getMetrics(cloudwatchClient, expectedNamespace, expectedMetricName, 1);
128+
137129
expect(metrics.Metrics?.length).toBe(1);
138130
const metric = metrics.Metrics?.[0];
139131
const expectedDimensions = [
@@ -144,16 +136,16 @@ describe('happy cases', () => {
144136
expect(metric?.Dimensions).toStrictEqual(expectedDimensions);
145137

146138
// Check coldstart metric value
147-
const adjustedStartTime = new Date(startTime.getTime() - 60 * 1000);
148-
const endTime = new Date(new Date().getTime() + 60 * 1000);
139+
const adjustedStartTime = new Date(startTime.getTime() - 3 * ONE_MINUTE);
140+
const endTime = new Date(new Date().getTime() + ONE_MINUTE);
149141
console.log(`Manual command: aws cloudwatch get-metric-statistics --namespace ${expectedNamespace} --metric-name ${expectedMetricName} --start-time ${Math.floor(adjustedStartTime.getTime()/1000)} --end-time ${Math.floor(endTime.getTime()/1000)} --statistics 'Sum' --period 60 --dimensions '${JSON.stringify(expectedDimensions)}'`);
150142
const metricStat = await cloudwatchClient
151143
.getMetricStatistics(
152144
{
153145
Namespace: expectedNamespace,
154-
StartTime: new Date(startTime.getTime() - 60 * 1000), // minus 1 minute,
146+
StartTime: adjustedStartTime,
155147
Dimensions: expectedDimensions,
156-
EndTime: new Date(new Date().getTime() + 60 * 1000),
148+
EndTime: endTime,
157149
Period: 60,
158150
MetricName: expectedMetricName,
159151
Statistics: ['Sum'],
@@ -165,7 +157,7 @@ describe('happy cases', () => {
165157
// Since lambda has been called twice in this test and potentially more in others, metric sum should be at least of expectedMetricValue * invocationCount
166158
const singleDataPoint = metricStat.Datapoints ? metricStat.Datapoints[0] : {};
167159
expect(singleDataPoint?.Sum).toBeGreaterThanOrEqual(parseInt(expectedMetricValue) * invocationCount);
168-
}, 15000);
160+
}, ONE_MINUTE * 3);
169161

170162
afterAll(async () => {
171163
if (!process.env.DISABLE_TEARDOWN) {
@@ -181,5 +173,5 @@ describe('happy cases', () => {
181173
quiet: true,
182174
});
183175
}
184-
}, 200000);
176+
}, ONE_MINUTE * 3);
185177
});

Diff for: packages/metrics/tests/e2e/standardFunctions.test.ts

+15-20
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import { SdkProvider } from 'aws-cdk/lib/api/aws-auth';
1515
import { CloudFormationDeployments } from 'aws-cdk/lib/api/cloudformation-deployments';
1616
import * as AWS from 'aws-sdk';
1717
import { MetricUnits } from '../../src';
18+
import { getMetrics } from '../helpers/metricsUtils';
19+
20+
const ONE_MINUTE = 1000 * 60;
1821

1922
const cloudwatchClient = new AWS.CloudWatch();
2023
const lambdaClient = new AWS.Lambda();
@@ -83,16 +86,12 @@ describe('happy cases', () => {
8386
// THEN
8487
// sleep to allow metrics to be collected
8588
await new Promise((resolve) => setTimeout(resolve, 15000));
86-
}, 200000);
89+
}, ONE_MINUTE * 3);
8790

8891
it('capture ColdStart Metric', async () => {
8992
// Check coldstart metric dimensions
90-
const coldStartMetrics = await cloudwatchClient
91-
.listMetrics({
92-
Namespace: expectedNamespace,
93-
MetricName: 'ColdStart',
94-
})
95-
.promise();
93+
const coldStartMetrics = await getMetrics(cloudwatchClient, expectedNamespace, 'ColdStart', 1);
94+
9695
expect(coldStartMetrics.Metrics?.length).toBe(1);
9796
const coldStartMetric = coldStartMetrics.Metrics?.[0];
9897
expect(coldStartMetric?.Dimensions).toStrictEqual([{ Name: 'service', Value: expectedServiceName }]);
@@ -119,16 +118,12 @@ describe('happy cases', () => {
119118
// Despite lambda has been called twice, coldstart metric sum should only be 1
120119
const singleDataPoint = coldStartMetricStat.Datapoints ? coldStartMetricStat.Datapoints[0] : {};
121120
expect(singleDataPoint?.Sum).toBe(1);
122-
}, 15000);
121+
}, ONE_MINUTE * 3);
123122

124123
it('produce added Metric with the default and extra one dimensions', async () => {
125124
// Check metric dimensions
126-
const metrics = await cloudwatchClient
127-
.listMetrics({
128-
Namespace: expectedNamespace,
129-
MetricName: expectedMetricName,
130-
})
131-
.promise();
125+
const metrics = await getMetrics(cloudwatchClient, expectedNamespace, expectedMetricName, 1);
126+
132127
expect(metrics.Metrics?.length).toBe(1);
133128
const metric = metrics.Metrics?.[0];
134129
const expectedDimensions = [
@@ -139,16 +134,16 @@ describe('happy cases', () => {
139134
expect(metric?.Dimensions).toStrictEqual(expectedDimensions);
140135

141136
// Check coldstart metric value
142-
const adjustedStartTime = new Date(startTime.getTime() - 60 * 1000);
143-
const endTime = new Date(new Date().getTime() + 60 * 1000);
137+
const adjustedStartTime = new Date(startTime.getTime() - 3 * ONE_MINUTE);
138+
const endTime = new Date(new Date().getTime() + ONE_MINUTE);
144139
console.log(`Manual command: aws cloudwatch get-metric-statistics --namespace ${expectedNamespace} --metric-name ${expectedMetricName} --start-time ${Math.floor(adjustedStartTime.getTime()/1000)} --end-time ${Math.floor(endTime.getTime()/1000)} --statistics 'Sum' --period 60 --dimensions '${JSON.stringify(expectedDimensions)}'`);
145140
const metricStat = await cloudwatchClient
146141
.getMetricStatistics(
147142
{
148143
Namespace: expectedNamespace,
149-
StartTime: new Date(startTime.getTime() - 60 * 1000), // minus 1 minute,
144+
StartTime: adjustedStartTime,
150145
Dimensions: expectedDimensions,
151-
EndTime: new Date(new Date().getTime() + 60 * 1000),
146+
EndTime: endTime,
152147
Period: 60,
153148
MetricName: expectedMetricName,
154149
Statistics: ['Sum'],
@@ -160,7 +155,7 @@ describe('happy cases', () => {
160155
// Since lambda has been called twice in this test and potentially more in others, metric sum should be at least of expectedMetricValue * invocationCount
161156
const singleDataPoint = metricStat.Datapoints ? metricStat.Datapoints[0] : {};
162157
expect(singleDataPoint.Sum).toBeGreaterThanOrEqual(parseInt(expectedMetricValue) * invocationCount);
163-
}, 15000);
158+
}, ONE_MINUTE * 3);
164159

165160
afterAll(async () => {
166161
if (!process.env.DISABLE_TEARDOWN) {
@@ -176,5 +171,5 @@ describe('happy cases', () => {
176171
quiet: true,
177172
});
178173
}
179-
}, 200000);
174+
}, ONE_MINUTE * 3);
180175
});

Diff for: packages/metrics/tests/helpers/metricsUtils.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { CloudWatch } from 'aws-sdk';
2+
import promiseRetry from 'promise-retry';
3+
4+
const getMetrics = async (cloudWatchClient: CloudWatch, namespace: string, metric: string, expectedMetrics: number): Promise<CloudWatch.ListMetricsOutput> => {
5+
const retryOptions = { retries: 20, minTimeout: 5_000, maxTimeout: 10_000, factor: 1.25 };
6+
7+
return promiseRetry(async (retry: (err?: Error) => never, _: number) => {
8+
9+
const result = await cloudWatchClient
10+
.listMetrics({
11+
Namespace: namespace,
12+
MetricName: metric,
13+
})
14+
.promise();
15+
16+
if (result.Metrics?.length !== expectedMetrics) {
17+
retry(new Error(`Expected ${expectedMetrics} metrics, got ${result.Metrics?.length} for ${namespace}.${metric}`));
18+
}
19+
20+
return result;
21+
}, retryOptions);
22+
};
23+
24+
export { getMetrics };

Diff for: packages/tracing/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
"main": "./lib/index.js",
3131
"types": "./lib/index.d.ts",
3232
"devDependencies": {
33-
"@aws-sdk/client-dynamodb": "^3.52.0"
33+
"@aws-sdk/client-dynamodb": "^3.52.0",
34+
"@types/promise-retry": "^1.1.3",
35+
"promise-retry": "^2.0.1"
3436
},
3537
"files": [
3638
"lib"

0 commit comments

Comments
 (0)