@@ -15,6 +15,9 @@ import { SdkProvider } from 'aws-cdk/lib/api/aws-auth';
15
15
import { CloudFormationDeployments } from 'aws-cdk/lib/api/cloudformation-deployments' ;
16
16
import * as AWS from 'aws-sdk' ;
17
17
import { MetricUnits } from '../../src' ;
18
+ import { getMetrics } from '../helpers/metricsUtils' ;
19
+
20
+ const ONE_MINUTE = 1000 * 60 ;
18
21
19
22
const cloudwatchClient = new AWS . CloudWatch ( ) ;
20
23
const lambdaClient = new AWS . Lambda ( ) ;
@@ -80,10 +83,7 @@ describe('happy cases', () => {
80
83
. promise ( ) ;
81
84
}
82
85
83
- // THEN
84
- // sleep to allow metrics to be collected
85
- await new Promise ( ( resolve ) => setTimeout ( resolve , 15000 ) ) ;
86
- } , 200000 ) ;
86
+ } , ONE_MINUTE * 3 ) ;
87
87
88
88
it ( 'capture ColdStart Metric' , async ( ) => {
89
89
const expectedDimensions = [
@@ -92,12 +92,8 @@ describe('happy cases', () => {
92
92
{ Name : Object . keys ( expectedDefaultDimensions ) [ 0 ] , Value : expectedDefaultDimensions . MyDimension } ,
93
93
] ;
94
94
// 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
+
101
97
expect ( coldStartMetrics . Metrics ?. length ) . toBe ( 1 ) ;
102
98
const coldStartMetric = coldStartMetrics . Metrics ?. [ 0 ] ;
103
99
expect ( coldStartMetric ?. Dimensions ) . toStrictEqual ( expectedDimensions ) ;
@@ -124,16 +120,12 @@ describe('happy cases', () => {
124
120
// Despite lambda has been called twice, coldstart metric sum should only be 1
125
121
const singleDataPoint = coldStartMetricStat . Datapoints ? coldStartMetricStat . Datapoints [ 0 ] : { } ;
126
122
expect ( singleDataPoint ?. Sum ) . toBe ( 1 ) ;
127
- } , 15000 ) ;
123
+ } , ONE_MINUTE * 3 ) ;
128
124
129
125
it ( 'produce added Metric with the default and extra one dimensions' , async ( ) => {
130
126
// 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
+
137
129
expect ( metrics . Metrics ?. length ) . toBe ( 1 ) ;
138
130
const metric = metrics . Metrics ?. [ 0 ] ;
139
131
const expectedDimensions = [
@@ -144,16 +136,16 @@ describe('happy cases', () => {
144
136
expect ( metric ?. Dimensions ) . toStrictEqual ( expectedDimensions ) ;
145
137
146
138
// 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 ) ;
149
141
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 ) } '` ) ;
150
142
const metricStat = await cloudwatchClient
151
143
. getMetricStatistics (
152
144
{
153
145
Namespace : expectedNamespace ,
154
- StartTime : new Date ( startTime . getTime ( ) - 60 * 1000 ) , // minus 1 minute ,
146
+ StartTime : adjustedStartTime ,
155
147
Dimensions : expectedDimensions ,
156
- EndTime : new Date ( new Date ( ) . getTime ( ) + 60 * 1000 ) ,
148
+ EndTime : endTime ,
157
149
Period : 60 ,
158
150
MetricName : expectedMetricName ,
159
151
Statistics : [ 'Sum' ] ,
@@ -165,7 +157,7 @@ describe('happy cases', () => {
165
157
// Since lambda has been called twice in this test and potentially more in others, metric sum should be at least of expectedMetricValue * invocationCount
166
158
const singleDataPoint = metricStat . Datapoints ? metricStat . Datapoints [ 0 ] : { } ;
167
159
expect ( singleDataPoint ?. Sum ) . toBeGreaterThanOrEqual ( parseInt ( expectedMetricValue ) * invocationCount ) ;
168
- } , 15000 ) ;
160
+ } , ONE_MINUTE * 3 ) ;
169
161
170
162
afterAll ( async ( ) => {
171
163
if ( ! process . env . DISABLE_TEARDOWN ) {
@@ -181,5 +173,5 @@ describe('happy cases', () => {
181
173
quiet : true ,
182
174
} ) ;
183
175
}
184
- } , 200000 ) ;
176
+ } , ONE_MINUTE * 3 ) ;
185
177
} ) ;
0 commit comments