Skip to content

Commit 37ebad5

Browse files
committed
Consistently use UTC time in MetricsE2ET.
1 parent 758ffc6 commit 37ebad5

File tree

1 file changed

+31
-34
lines changed
  • powertools-e2e-tests/src/test/java/software/amazon/lambda/powertools

1 file changed

+31
-34
lines changed

powertools-e2e-tests/src/test/java/software/amazon/lambda/powertools/MetricsE2ET.java

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818
import static software.amazon.lambda.powertools.testutils.Infrastructure.FUNCTION_NAME_OUTPUT;
1919
import static software.amazon.lambda.powertools.testutils.lambda.LambdaInvoker.invokeFunction;
2020

21+
import java.time.Clock;
2122
import java.time.Instant;
22-
import java.time.LocalDateTime;
23-
import java.time.ZoneOffset;
2423
import java.time.temporal.ChronoUnit;
2524
import java.util.Collections;
2625
import java.util.List;
@@ -29,10 +28,12 @@
2928
import java.util.concurrent.TimeUnit;
3029
import java.util.stream.Collectors;
3130
import java.util.stream.Stream;
31+
3232
import org.junit.jupiter.api.AfterAll;
3333
import org.junit.jupiter.api.BeforeAll;
3434
import org.junit.jupiter.api.Test;
3535
import org.junit.jupiter.api.Timeout;
36+
3637
import software.amazon.lambda.powertools.testutils.Infrastructure;
3738
import software.amazon.lambda.powertools.testutils.lambda.InvocationResult;
3839
import 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,58 +72,54 @@ public static void tearDown() {
7172
public void test_recordMetrics() {
7273
// GIVEN
7374

74-
Instant currentTimeTruncatedToMinutes = Instant.now().truncatedTo(ChronoUnit.MINUTES);
75-
String event1 =
76-
"{ \"metrics\": {\"orders\": 1, \"products\": 4}, \"dimensions\": { \"Environment\": \"test\"}, \"highResolution\": \"false\"}";
75+
Instant currentTimeTruncatedToMinutes = Instant.now(Clock.systemUTC()).truncatedTo(ChronoUnit.MINUTES);
76+
String event1 = "{ \"metrics\": {\"orders\": 1, \"products\": 4}, \"dimensions\": { \"Environment\": \"test\"}, \"highResolution\": \"false\"}";
7777

78-
String event2 =
79-
"{ \"metrics\": {\"orders\": 1, \"products\": 8}, \"dimensions\": { \"Environment\": \"test\"}, \"highResolution\": \"true\"}";
78+
String event2 = "{ \"metrics\": {\"orders\": 1, \"products\": 8}, \"dimensions\": { \"Environment\": \"test\"}, \"highResolution\": \"true\"}";
8079
// WHEN
8180
InvocationResult invocationResult = invokeFunction(functionName, event1);
8281

8382
invokeFunction(functionName, event2);
8483

8584
// THEN
8685
MetricsFetcher metricsFetcher = new MetricsFetcher();
87-
List<Double> coldStart =
88-
metricsFetcher.fetchMetrics(invocationResult.getStart(), invocationResult.getEnd(), 60, namespace,
89-
"ColdStart", Stream.of(new String[][] {
90-
{"FunctionName", functionName},
91-
{"Service", service}}
92-
).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])));
9391
assertThat(coldStart.get(0)).isEqualTo(1);
94-
List<Double> orderMetrics =
95-
metricsFetcher.fetchMetrics(invocationResult.getStart(), invocationResult.getEnd(), 60, namespace,
96-
"orders", Collections.singletonMap("Environment", "test"));
92+
List<Double> orderMetrics = metricsFetcher.fetchMetrics(invocationResult.getStart(), invocationResult.getEnd(),
93+
60, namespace,
94+
"orders", Collections.singletonMap("Environment", "test"));
9795
assertThat(orderMetrics.get(0)).isEqualTo(2);
98-
List<Double> productMetrics =
99-
metricsFetcher.fetchMetrics(invocationResult.getStart(), invocationResult.getEnd(), 60, namespace,
100-
"products", Collections.singletonMap("Environment", "test"));
96+
List<Double> productMetrics = metricsFetcher.fetchMetrics(invocationResult.getStart(),
97+
invocationResult.getEnd(), 60, namespace,
98+
"products", Collections.singletonMap("Environment", "test"));
10199

102-
// 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
103101

104102
assertThat(productMetrics.get(0)).isEqualTo(12);
105103

106-
orderMetrics =
107-
metricsFetcher.fetchMetrics(invocationResult.getStart(), invocationResult.getEnd(), 60, namespace,
108-
"orders", Collections.singletonMap("Service", service));
104+
orderMetrics = metricsFetcher.fetchMetrics(invocationResult.getStart(), invocationResult.getEnd(), 60,
105+
namespace,
106+
"orders", Collections.singletonMap("Service", service));
109107
assertThat(orderMetrics.get(0)).isEqualTo(2);
110-
productMetrics =
111-
metricsFetcher.fetchMetrics(invocationResult.getStart(), invocationResult.getEnd(), 60, namespace,
112-
"products", Collections.singletonMap("Service", service));
108+
productMetrics = metricsFetcher.fetchMetrics(invocationResult.getStart(), invocationResult.getEnd(), 60,
109+
namespace,
110+
"products", Collections.singletonMap("Service", service));
113111
assertThat(productMetrics.get(0)).isEqualTo(12);
114112

115113
Instant searchStartTime = currentTimeTruncatedToMinutes.plusSeconds(15);
116114
Instant searchEndTime = currentTimeTruncatedToMinutes.plusSeconds(45);
117115

118-
List<Double> productMetricDataResult =
119-
metricsFetcher.fetchMetrics(searchStartTime, searchEndTime, 1, namespace,
120-
"products", Collections.singletonMap("Environment", "test"));
116+
List<Double> productMetricDataResult = metricsFetcher.fetchMetrics(searchStartTime, searchEndTime, 1, namespace,
117+
"products", Collections.singletonMap("Environment", "test"));
121118

122-
// 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
123121

124122
assertThat(productMetricDataResult.get(0)).isEqualTo(8);
125123

126-
127124
}
128125
}

0 commit comments

Comments
 (0)