1818import static software .amazon .lambda .powertools .testutils .Infrastructure .FUNCTION_NAME_OUTPUT ;
1919import static software .amazon .lambda .powertools .testutils .lambda .LambdaInvoker .invokeFunction ;
2020
21- import java .time .Clock ;
2221import java .time .Instant ;
2322import java .time .temporal .ChronoUnit ;
2423import java .util .Collections ;
@@ -74,7 +73,6 @@ static void tearDown() {
7473 void test_recordMetrics () {
7574 // GIVEN
7675
77- Instant currentTimeTruncatedToMinutes = Instant .now (Clock .systemUTC ()).truncatedTo (ChronoUnit .MINUTES );
7876 String event1 = "{ \" metrics\" : {\" orders\" : 1, \" products\" : 4}, \" dimensions\" : { \" Environment\" : \" test\" }, \" highResolution\" : \" false\" }" ;
7977
8078 String event2 = "{ \" metrics\" : {\" orders\" : 1, \" products\" : 8}, \" dimensions\" : { \" Environment\" : \" test\" }, \" highResolution\" : \" true\" }" ;
@@ -84,42 +82,45 @@ void test_recordMetrics() {
8482 invokeFunction (functionName , event2 );
8583
8684 // THEN
85+ // Pad the query window to address CloudWatch eventual consistency:
86+ // metric timestamps can shift by up to a minute during batch processing.
87+ Instant paddedStart = invocationResult .getStart ().minus (1 , ChronoUnit .MINUTES );
88+ Instant paddedEnd = invocationResult .getEnd ().plus (2 , ChronoUnit .MINUTES );
89+
8790 MetricsFetcher metricsFetcher = new MetricsFetcher ();
88- List <Double > coldStart = metricsFetcher .fetchMetrics (invocationResult . getStart (), invocationResult . getEnd () , 60 ,
91+ List <Double > coldStart = metricsFetcher .fetchMetrics (paddedStart , paddedEnd , 60 ,
8992 NAMESPACE ,
9093 "ColdStart" , Stream .of (new String [][] {
9194 { "FunctionName" , functionName },
9295 { "Service" , SERVICE } }).collect (Collectors .toMap (data -> data [0 ], data -> data [1 ])));
9396 assertThat (coldStart .get (0 )).isEqualTo (1 );
9497 List <Double > orderMetrics = RetryUtils .withRetry (() -> {
95- List <Double > metrics = metricsFetcher .fetchMetrics (invocationResult . getStart (), invocationResult . getEnd () ,
98+ List <Double > metrics = metricsFetcher .fetchMetrics (paddedStart , paddedEnd ,
9699 60 , NAMESPACE , "orders" , Collections .singletonMap ("Environment" , "test" ));
97100 if (metrics .get (0 ) != 2.0 ) {
98101 throw new DataNotReadyException ("Expected 2.0 orders but got " + metrics .get (0 ));
99102 }
100103 return metrics ;
101104 }, "orderMetricsRetry" , DataNotReadyException .class ).get ();
102105 assertThat (orderMetrics .get (0 )).isEqualTo (2 );
103- List <Double > productMetrics = metricsFetcher .fetchMetrics (invocationResult . getStart () ,
104- invocationResult . getEnd () , 60 , NAMESPACE ,
106+ List <Double > productMetrics = metricsFetcher .fetchMetrics (paddedStart ,
107+ paddedEnd , 60 , NAMESPACE ,
105108 "products" , Collections .singletonMap ("Environment" , "test" ));
106109
107110 // When searching across a 1 minute time period with a period of 60 we find both metrics and the sum is 12
108111 assertThat (productMetrics .get (0 )).isEqualTo (12 );
109112
110- orderMetrics = metricsFetcher .fetchMetrics (invocationResult . getStart (), invocationResult . getEnd () , 60 ,
113+ orderMetrics = metricsFetcher .fetchMetrics (paddedStart , paddedEnd , 60 ,
111114 NAMESPACE ,
112115 "orders" , Collections .singletonMap ("Service" , SERVICE ));
113116 assertThat (orderMetrics .get (0 )).isEqualTo (2 );
114- productMetrics = metricsFetcher .fetchMetrics (invocationResult . getStart (), invocationResult . getEnd () , 60 ,
117+ productMetrics = metricsFetcher .fetchMetrics (paddedStart , paddedEnd , 60 ,
115118 NAMESPACE ,
116119 "products" , Collections .singletonMap ("Service" , SERVICE ));
117120 assertThat (productMetrics .get (0 )).isEqualTo (12 );
118121
119- Instant searchStartTime = currentTimeTruncatedToMinutes .plusSeconds (15 );
120- Instant searchEndTime = currentTimeTruncatedToMinutes .plusSeconds (45 );
121-
122- List <Double > productMetricDataResult = metricsFetcher .fetchMetrics (searchStartTime , searchEndTime , 1 , NAMESPACE ,
122+ List <Double > productMetricDataResult = metricsFetcher .fetchMetrics (invocationResult .getStart (),
123+ invocationResult .getEnd (), 1 , NAMESPACE ,
123124 "products" , Collections .singletonMap ("Environment" , "test" ));
124125
125126 // We are searching across the time period the metric was created but with a period of 1 second. Only the high
0 commit comments