Skip to content

Commit 9606d10

Browse files
author
Meshwa Savalia
committed
Add unit tests for High Resolution Metrics
1 parent 11914d9 commit 9606d10

File tree

4 files changed

+71
-5
lines changed

4 files changed

+71
-5
lines changed

src/main/java/software/amazon/cloudwatchlogs/emf/logger/MetricsLogger.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import software.amazon.cloudwatchlogs.emf.model.StorageResolution;
3636
import software.amazon.cloudwatchlogs.emf.model.Unit;
3737
import software.amazon.cloudwatchlogs.emf.sinks.ISink;
38+
import software.amazon.cloudwatchlogs.emf.util.Validator;
3839

3940
/**
4041
* A metrics logger. Use this interface to publish logs to CloudWatch Logs and extract metrics to
@@ -93,6 +94,7 @@ public void flush() {
9394
configureContextForEnvironment(context, environment);
9495
sink.accept(context);
9596
context = context.createCopyWithContext(flushPreserveDimensions);
97+
Validator.metricNameAndResolutionMap.clear();
9698
} finally {
9799
rwl.writeLock().unlock();
98100
}

src/main/java/software/amazon/cloudwatchlogs/emf/util/Validator.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import software.amazon.cloudwatchlogs.emf.model.Unit;
2727

2828
public class Validator {
29+
public static Map<String, String> metricNameAndResolutionMap = new HashMap<>();
30+
2931
private Validator() {
3032
throw new IllegalStateException("Utility class");
3133
}
@@ -98,8 +100,6 @@ public static void validateMetric(
98100
String name, double value, Unit unit, StorageResolution storageResolution)
99101
throws InvalidMetricException {
100102

101-
Map<String, String> metricNameAndResolutionMap = new HashMap<>();
102-
103103
if (name == null || name.trim().isEmpty()) {
104104
throw new InvalidMetricException(
105105
"Metric name " + name + " must include at least one non-whitespace character");
@@ -120,8 +120,8 @@ public static void validateMetric(
120120
if (unit == null) {
121121
throw new InvalidMetricException("Metric unit cannot be null");
122122
}
123-
// TODO_M validate storageResolution ?
124-
if (storageResolution.getValue() == -1) {
123+
124+
if (storageResolution == null) {
125125
throw new InvalidMetricException("Metric resolution cannot be null");
126126
}
127127

src/test/java/software/amazon/cloudwatchlogs/emf/logger/MetricsLoggerTest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import software.amazon.cloudwatchlogs.emf.exception.InvalidTimestampException;
3939
import software.amazon.cloudwatchlogs.emf.model.DimensionSet;
4040
import software.amazon.cloudwatchlogs.emf.model.MetricsContext;
41+
import software.amazon.cloudwatchlogs.emf.model.StorageResolution;
42+
import software.amazon.cloudwatchlogs.emf.model.Unit;
4143
import software.amazon.cloudwatchlogs.emf.sinks.SinkShunt;
4244

4345
class MetricsLoggerTest {
@@ -349,7 +351,24 @@ void whenPutMetric_withInvalidValue_thenThrowInvalidMetricException(double value
349351

350352
@Test
351353
void whenPutMetric_withNullUnit_thenThrowInvalidMetricException() {
352-
assertThrows(InvalidMetricException.class, () -> logger.putMetric("test", 1, null));
354+
assertThrows(InvalidMetricException.class, () -> logger.putMetric("test", 1, (Unit) null));
355+
}
356+
357+
@Test
358+
void whenPutMetric_withNullStorageResolution_thenThrowInvalidMetricException() {
359+
assertThrows(
360+
InvalidMetricException.class,
361+
() -> logger.putMetric("test", 1, (StorageResolution) null));
362+
}
363+
364+
@Test
365+
void whenPutMetric_withDifferentStorageResolution_thenThrowInvalidMetricException() {
366+
assertThrows(
367+
InvalidMetricException.class,
368+
() -> {
369+
logger.putMetric("test", 1);
370+
logger.putMetric("test", 1, StorageResolution.HIGH);
371+
});
353372
}
354373

355374
@Test

src/test/java/software/amazon/cloudwatchlogs/emf/model/MetricDirectiveTest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,51 @@ void testPutMetricWithUnit() {
9090
Unit.MILLISECONDS, metricDirective.getMetrics().get("Time").getUnit());
9191
}
9292

93+
@Test
94+
void testPutMetricWithoutStorageResolution() throws JsonProcessingException {
95+
MetricDirective metricDirective = new MetricDirective();
96+
metricDirective.putMetric("Time", 10);
97+
98+
String serializedMetricDirective = objectMapper.writeValueAsString(metricDirective);
99+
100+
Assertions.assertEquals(
101+
StorageResolution.STANDARD,
102+
metricDirective.getMetrics().get("Time").getStorageResolution());
103+
Assertions.assertEquals(
104+
"{\"Dimensions\":[[]],\"Metrics\":[{\"Name\":\"Time\",\"Unit\":\"None\"}],\"Namespace\":\"aws-embedded-metrics\"}",
105+
serializedMetricDirective);
106+
}
107+
108+
@Test
109+
void testPutMetricWithStandardStorageResolution() throws JsonProcessingException {
110+
MetricDirective metricDirective = new MetricDirective();
111+
metricDirective.putMetric("Time", 10, StorageResolution.STANDARD);
112+
113+
String serializedMetricDirective = objectMapper.writeValueAsString(metricDirective);
114+
115+
Assertions.assertEquals(
116+
StorageResolution.STANDARD,
117+
metricDirective.getMetrics().get("Time").getStorageResolution());
118+
Assertions.assertEquals(
119+
"{\"Dimensions\":[[]],\"Metrics\":[{\"Name\":\"Time\",\"Unit\":\"None\"}],\"Namespace\":\"aws-embedded-metrics\"}",
120+
serializedMetricDirective);
121+
}
122+
123+
@Test
124+
void testPutMetricWithHighStorageResolution() throws JsonProcessingException {
125+
MetricDirective metricDirective = new MetricDirective();
126+
metricDirective.putMetric("Time", 10, StorageResolution.HIGH);
127+
128+
String serializedMetricDirective = objectMapper.writeValueAsString(metricDirective);
129+
130+
Assertions.assertEquals(
131+
StorageResolution.HIGH,
132+
metricDirective.getMetrics().get("Time").getStorageResolution());
133+
Assertions.assertEquals(
134+
"{\"Dimensions\":[[]],\"Metrics\":[{\"Name\":\"Time\",\"StorageResolution\":1,\"Unit\":\"None\"}],\"Namespace\":\"aws-embedded-metrics\"}",
135+
serializedMetricDirective);
136+
}
137+
93138
@Test
94139
void testPutDimensions()
95140
throws JsonProcessingException, InvalidDimensionException,

0 commit comments

Comments
 (0)