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 ;
2122import java .time .Instant ;
22- import java .time .LocalDateTime ;
23- import java .time .ZoneOffset ;
2423import java .time .temporal .ChronoUnit ;
2524import java .util .Collections ;
2625import java .util .List ;
2928import java .util .concurrent .TimeUnit ;
3029import java .util .stream .Collectors ;
3130import java .util .stream .Stream ;
31+
3232import org .junit .jupiter .api .AfterAll ;
3333import org .junit .jupiter .api .BeforeAll ;
3434import org .junit .jupiter .api .Test ;
3535import org .junit .jupiter .api .Timeout ;
36+
3637import software .amazon .lambda .powertools .testutils .Infrastructure ;
3738import software .amazon .lambda .powertools .testutils .lambda .InvocationResult ;
3839import software .amazon .lambda .powertools .testutils .metrics .MetricsFetcher ;
@@ -51,9 +52,9 @@ public static void setup() {
5152 .pathToFunction ("metrics" )
5253 .environmentVariables (
5354 Stream .of (new String [][] {
54- { "POWERTOOLS_METRICS_NAMESPACE" , namespace },
55- { "POWERTOOLS_SERVICE_NAME" , service }
56- })
55+ { "POWERTOOLS_METRICS_NAMESPACE" , namespace },
56+ { "POWERTOOLS_SERVICE_NAME" , service }
57+ })
5758 .collect (Collectors .toMap (data -> data [0 ], data -> data [1 ])))
5859 .build ();
5960 Map <String , String > outputs = infrastructure .deploy ();
@@ -71,60 +72,54 @@ public static void tearDown() {
7172 public void test_recordMetrics () {
7273 // GIVEN
7374
74- Instant currentTimeTruncatedToMinutes =
75- LocalDateTime . now (). truncatedTo ( ChronoUnit . MINUTES ). toInstant ( ZoneOffset . UTC ) ;
75+ Instant currentTimeTruncatedToMinutes = Instant . now ( Clock . systemUTC ()). truncatedTo ( ChronoUnit . MINUTES );
76+ String event1 = "{ \" metrics \" : { \" orders \" : 1, \" products \" : 4}, \" dimensions \" : { \" Environment \" : \" test \" }, \" highResolution \" : \" false \" }" ;
7677
77- String event1 =
78- "{ \" metrics\" : {\" orders\" : 1, \" products\" : 4}, \" dimensions\" : { \" Environment\" : \" test\" }, \" highResolution\" : \" false\" }" ;
79-
80- String event2 =
81- "{ \" metrics\" : {\" orders\" : 1, \" products\" : 8}, \" dimensions\" : { \" Environment\" : \" test\" }, \" highResolution\" : \" true\" }" ;
78+ String event2 = "{ \" metrics\" : {\" orders\" : 1, \" products\" : 8}, \" dimensions\" : { \" Environment\" : \" test\" }, \" highResolution\" : \" true\" }" ;
8279 // WHEN
8380 InvocationResult invocationResult = invokeFunction (functionName , event1 );
8481
8582 invokeFunction (functionName , event2 );
8683
8784 // THEN
8885 MetricsFetcher metricsFetcher = new MetricsFetcher ();
89- List <Double > coldStart =
90- metricsFetcher .fetchMetrics (invocationResult .getStart (), invocationResult .getEnd (), 60 , namespace ,
91- "ColdStart" , Stream .of (new String [][] {
92- {"FunctionName" , functionName },
93- {"Service" , service }}
94- ).collect (Collectors .toMap (data -> data [0 ], data -> data [1 ])));
86+ List <Double > coldStart = metricsFetcher .fetchMetrics (invocationResult .getStart (), invocationResult .getEnd (), 60 ,
87+ namespace ,
88+ "ColdStart" , Stream .of (new String [][] {
89+ { "FunctionName" , functionName },
90+ { "Service" , service } }).collect (Collectors .toMap (data -> data [0 ], data -> data [1 ])));
9591 assertThat (coldStart .get (0 )).isEqualTo (1 );
96- List <Double > orderMetrics =
97- metricsFetcher . fetchMetrics ( invocationResult . getStart (), invocationResult . getEnd (), 60 , namespace ,
98- "orders" , Collections .singletonMap ("Environment" , "test" ));
92+ List <Double > orderMetrics = metricsFetcher . fetchMetrics ( invocationResult . getStart (), invocationResult . getEnd (),
93+ 60 , namespace ,
94+ "orders" , Collections .singletonMap ("Environment" , "test" ));
9995 assertThat (orderMetrics .get (0 )).isEqualTo (2 );
100- List <Double > productMetrics =
101- metricsFetcher . fetchMetrics ( invocationResult . getStart (), invocationResult .getEnd (), 60 , namespace ,
102- "products" , Collections .singletonMap ("Environment" , "test" ));
96+ List <Double > productMetrics = metricsFetcher . fetchMetrics ( invocationResult . getStart (),
97+ invocationResult .getEnd (), 60 , namespace ,
98+ "products" , Collections .singletonMap ("Environment" , "test" ));
10399
104- // When searching across a 1 minute time period with a period of 60 we find both metrics and the sum is 12
100+ // When searching across a 1 minute time period with a period of 60 we find both metrics and the sum is 12
105101
106102 assertThat (productMetrics .get (0 )).isEqualTo (12 );
107103
108- orderMetrics =
109- metricsFetcher . fetchMetrics ( invocationResult . getStart (), invocationResult . getEnd (), 60 , namespace ,
110- "orders" , Collections .singletonMap ("Service" , service ));
104+ orderMetrics = metricsFetcher . fetchMetrics ( invocationResult . getStart (), invocationResult . getEnd (), 60 ,
105+ namespace ,
106+ "orders" , Collections .singletonMap ("Service" , service ));
111107 assertThat (orderMetrics .get (0 )).isEqualTo (2 );
112- productMetrics =
113- metricsFetcher . fetchMetrics ( invocationResult . getStart (), invocationResult . getEnd (), 60 , namespace ,
114- "products" , Collections .singletonMap ("Service" , service ));
108+ productMetrics = metricsFetcher . fetchMetrics ( invocationResult . getStart (), invocationResult . getEnd (), 60 ,
109+ namespace ,
110+ "products" , Collections .singletonMap ("Service" , service ));
115111 assertThat (productMetrics .get (0 )).isEqualTo (12 );
116112
117113 Instant searchStartTime = currentTimeTruncatedToMinutes .plusSeconds (15 );
118114 Instant searchEndTime = currentTimeTruncatedToMinutes .plusSeconds (45 );
119115
120- List <Double > productMetricDataResult =
121- metricsFetcher .fetchMetrics (searchStartTime , searchEndTime , 1 , namespace ,
122- "products" , Collections .singletonMap ("Environment" , "test" ));
116+ List <Double > productMetricDataResult = metricsFetcher .fetchMetrics (searchStartTime , searchEndTime , 1 , namespace ,
117+ "products" , Collections .singletonMap ("Environment" , "test" ));
123118
124- // We are searching across the time period the metric was created but with a period of 1 second. Only the high resolution metric will be available at this point
119+ // We are searching across the time period the metric was created but with a period of 1 second. Only the high
120+ // resolution metric will be available at this point
125121
126122 assertThat (productMetricDataResult .get (0 )).isEqualTo (8 );
127123
128-
129124 }
130125}
0 commit comments