18
18
import static software .amazon .lambda .powertools .testutils .Infrastructure .FUNCTION_NAME_OUTPUT ;
19
19
import static software .amazon .lambda .powertools .testutils .lambda .LambdaInvoker .invokeFunction ;
20
20
21
+ import java .time .Clock ;
21
22
import java .time .Instant ;
22
- import java .time .LocalDateTime ;
23
- import java .time .ZoneOffset ;
24
23
import java .time .temporal .ChronoUnit ;
25
24
import java .util .Collections ;
26
25
import java .util .List ;
29
28
import java .util .concurrent .TimeUnit ;
30
29
import java .util .stream .Collectors ;
31
30
import java .util .stream .Stream ;
31
+
32
32
import org .junit .jupiter .api .AfterAll ;
33
33
import org .junit .jupiter .api .BeforeAll ;
34
34
import org .junit .jupiter .api .Test ;
35
35
import org .junit .jupiter .api .Timeout ;
36
+
36
37
import software .amazon .lambda .powertools .testutils .Infrastructure ;
37
38
import software .amazon .lambda .powertools .testutils .lambda .InvocationResult ;
38
39
import software .amazon .lambda .powertools .testutils .metrics .MetricsFetcher ;
@@ -51,9 +52,9 @@ public static void setup() {
51
52
.pathToFunction ("metrics" )
52
53
.environmentVariables (
53
54
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
+ })
57
58
.collect (Collectors .toMap (data -> data [0 ], data -> data [1 ])))
58
59
.build ();
59
60
Map <String , String > outputs = infrastructure .deploy ();
@@ -71,60 +72,54 @@ public static void tearDown() {
71
72
public void test_recordMetrics () {
72
73
// GIVEN
73
74
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 \" }" ;
76
77
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\" }" ;
82
79
// WHEN
83
80
InvocationResult invocationResult = invokeFunction (functionName , event1 );
84
81
85
82
invokeFunction (functionName , event2 );
86
83
87
84
// THEN
88
85
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 ])));
95
91
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" ));
99
95
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" ));
103
99
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
105
101
106
102
assertThat (productMetrics .get (0 )).isEqualTo (12 );
107
103
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 ));
111
107
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 ));
115
111
assertThat (productMetrics .get (0 )).isEqualTo (12 );
116
112
117
113
Instant searchStartTime = currentTimeTruncatedToMinutes .plusSeconds (15 );
118
114
Instant searchEndTime = currentTimeTruncatedToMinutes .plusSeconds (45 );
119
115
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" ));
123
118
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
125
121
126
122
assertThat (productMetricDataResult .get (0 )).isEqualTo (8 );
127
123
128
-
129
124
}
130
125
}
0 commit comments