Skip to content

Commit c3fa3e8

Browse files
committed
refactored to use metric parameters
1 parent f59577b commit c3fa3e8

File tree

6 files changed

+61
-41
lines changed

6 files changed

+61
-41
lines changed

src/main/java/software/amazon/cloudwatchlogs/emf/model/Metric.java

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package software.amazon.cloudwatchlogs.emf.model;
1818

19+
import com.fasterxml.jackson.annotation.JsonIgnore;
1920
import com.fasterxml.jackson.annotation.JsonInclude;
2021
import com.fasterxml.jackson.annotation.JsonProperty;
2122
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
@@ -31,7 +32,7 @@
3132

3233
/** Abstract immutable (except for name) class that all Metrics are based on. */
3334
@Getter
34-
public abstract class Metric {
35+
public abstract class Metric<V> {
3536
@JsonProperty("Name")
3637
@Setter(AccessLevel.PROTECTED)
3738
@NonNull
@@ -50,8 +51,7 @@ public abstract class Metric {
5051
@JsonSerialize(using = StorageResolutionSerializer.class)
5152
protected StorageResolution storageResolution;
5253

53-
/** @return the values stored by this metric. */
54-
abstract Object getValues();
54+
@JsonIgnore @Getter protected V values;
5555

5656
/** @return the values of this metric formatted to be flushed */
5757
protected Object getFormattedValues() {
@@ -76,20 +76,49 @@ protected Object getFormattedValues() {
7676
*/
7777
protected abstract Metric getMetricValuesOverSize(int size);
7878

79-
public static interface MetricBuilder {
79+
public abstract static class MetricBuilder<V, T extends MetricBuilder<V, T>> extends Metric<V> {
80+
81+
protected abstract T getThis();
8082

8183
/**
8284
* Adds a value to the metric.
8385
*
8486
* @param value the value to be added to this metric
8587
*/
86-
MetricBuilder addValue(double value);
88+
abstract T addValue(double value);
8789

8890
/**
8991
* Builds the metric.
9092
*
9193
* @return the built metric
9294
*/
93-
Metric build();
95+
abstract Metric build();
96+
97+
protected T name(@NonNull String name) {
98+
this.name = name;
99+
return getThis();
100+
}
101+
102+
public T unit(Unit unit) {
103+
this.unit = unit;
104+
return getThis();
105+
}
106+
107+
public T storageResolution(StorageResolution storageResolution) {
108+
this.storageResolution = storageResolution;
109+
return getThis();
110+
}
111+
112+
protected Metric getMetricValuesOverSize(int size) {
113+
return build().getMetricValuesOverSize(size);
114+
}
115+
116+
protected Metric getMetricValuesUnderSize(int size) {
117+
return build().getMetricValuesUnderSize(size);
118+
}
119+
120+
protected Object getFormattedValues() {
121+
return build().getFormattedValues();
122+
}
94123
}
95124
}

src/main/java/software/amazon/cloudwatchlogs/emf/model/MetricDefinition.java

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,12 @@
1616

1717
package software.amazon.cloudwatchlogs.emf.model;
1818

19-
import com.fasterxml.jackson.annotation.JsonIgnore;
2019
import java.util.ArrayList;
2120
import java.util.List;
22-
import lombok.Getter;
2321
import lombok.NonNull;
2422

2523
/** Represents the MetricDefinition of the EMF schema. */
26-
public class MetricDefinition extends Metric {
27-
@JsonIgnore @Getter protected List<Double> values;
24+
public class MetricDefinition extends Metric<List<Double>> {
2825

2926
private MetricDefinition(
3027
@NonNull String name,
@@ -85,30 +82,21 @@ protected Object getFormattedValues() {
8582
return values.size() == 1 ? values.get(0) : values;
8683
}
8784

88-
public static class MetricDefinitionBuilder extends MetricDefinition
89-
implements Metric.MetricBuilder {
90-
public MetricDefinitionBuilder() {
91-
super(Unit.NONE, StorageResolution.STANDARD, new ArrayList<>());
92-
}
93-
94-
protected MetricDefinitionBuilder name(@NonNull String name) {
95-
this.name = name;
96-
return this;
97-
}
85+
public static class MetricDefinitionBuilder
86+
extends Metric.MetricBuilder<List<Double>, MetricDefinitionBuilder> {
9887

9988
@Override
100-
public MetricDefinitionBuilder addValue(double value) {
101-
this.values.add(value);
89+
protected MetricDefinitionBuilder getThis() {
10290
return this;
10391
}
10492

105-
public MetricDefinitionBuilder unit(Unit unit) {
106-
this.unit = unit;
107-
return this;
93+
public MetricDefinitionBuilder() {
94+
this.values = new ArrayList<>();
10895
}
10996

110-
public MetricDefinitionBuilder storageResolution(StorageResolution storageResolution) {
111-
this.storageResolution = storageResolution;
97+
@Override
98+
public MetricDefinitionBuilder addValue(double value) {
99+
this.values.add(value);
112100
return this;
113101
}
114102

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class MetricDefinitionTest {
2828

2929
@Test(expected = NullPointerException.class)
3030
public void testThrowExceptionIfNameIsNull() {
31-
MetricDefinition builder = MetricDefinition.builder();
31+
MetricDefinition.MetricDefinitionBuilder builder = MetricDefinition.builder();
3232
builder.setName(null);
3333
}
3434

@@ -80,7 +80,7 @@ public void testSerializeMetricDefinitionWithoutUnitWithStandardStorageResolutio
8080
@Test
8181
public void testSerializeMetricDefinitionWithoutUnit() throws JsonProcessingException {
8282
ObjectMapper objectMapper = new ObjectMapper();
83-
MetricDefinition metricDefinition = MetricDefinition.builder().name("Time");
83+
MetricDefinition metricDefinition = MetricDefinition.builder().name("Time").build();
8484
String metricString = objectMapper.writeValueAsString(metricDefinition);
8585

8686
assertEquals("{\"Name\":\"Time\",\"Unit\":\"None\"}", metricString);

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ void testPutSameMetricMultipleTimes() {
7171
metricDirective.putMetric("Time", 20);
7272

7373
Assertions.assertEquals(1, metricDirective.getAllMetrics().size());
74-
MetricDefinition[] mds = metricDirective.getAllMetrics().toArray(new MetricDefinition[0]);
74+
MetricDefinition.MetricDefinitionBuilder[] mds =
75+
metricDirective
76+
.getAllMetrics()
77+
.toArray(new MetricDefinition.MetricDefinitionBuilder[0]);
7578
Assertions.assertEquals(Arrays.asList(10d, 20d), mds[0].getValues());
7679
}
7780

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.junit.Assert.assertEquals;
44

55
import java.util.Collections;
6+
import java.util.List;
67
import org.junit.After;
78
import org.junit.Test;
89

@@ -50,8 +51,7 @@ public void testConcurrentPutMetricWithDifferentKey() throws InterruptedExceptio
5051
assertEquals(metricDirective.getAllMetrics().size(), N_THREAD * N_PUT_METRIC);
5152
for (int i = 0; i < N_THREAD * N_PUT_METRIC; i++) {
5253
assertEquals(
53-
((MetricDefinition) metricDirective.getMetrics().get("Metric-" + i))
54-
.getValues()
54+
((List<Double>) metricDirective.getMetrics().get("Metric-" + i).getValues())
5555
.get(0),
5656
i,
5757
1e-5);
@@ -90,10 +90,11 @@ public void testConcurrentPutMetricWithSameKey() throws InterruptedException {
9090
}
9191

9292
assertEquals(1, metricDirective.getAllMetrics().size());
93-
MetricDefinition md = metricDirective.getAllMetrics().toArray(new MetricDefinition[0])[0];
94-
Collections.sort(md.getValues());
93+
Metric md = metricDirective.getAllMetrics().toArray(new Metric[0])[0];
94+
List<Double> values = (List<Double>) md.getValues();
95+
Collections.sort(values);
9596
for (int i = 0; i < N_THREAD * N_PUT_METRIC; i++) {
96-
assertEquals(md.getValues().get(i), i, 1e-5);
97+
assertEquals(values.get(i), i, 1e-5);
9798
}
9899
}
99100

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@ void testSerializeLessThan100Metrics() throws JsonProcessingException, InvalidMe
4848

4949
List<MetricDefinition> metrics = parseMetrics(events.get(0));
5050
Assertions.assertEquals(metrics.size(), metricCount);
51-
for (MetricDefinition metric : metrics) {
52-
MetricDefinition originalMetric =
53-
(MetricDefinition) (mc.getRootNode().metrics().get(metric.getName()));
51+
for (Metric metric : metrics) {
52+
Metric originalMetric = mc.getRootNode().metrics().get(metric.getName());
5453
Assertions.assertEquals(originalMetric.getName(), metric.getName());
5554
Assertions.assertEquals(originalMetric.getUnit(), metric.getUnit());
5655
}
@@ -74,9 +73,9 @@ void testSerializeMoreThen100Metrics() throws JsonProcessingException, InvalidMe
7473
allMetrics.addAll(parseMetrics(event));
7574
}
7675
Assertions.assertEquals(metricCount, allMetrics.size());
77-
for (MetricDefinition metric : allMetrics) {
78-
MetricDefinition originalMetric =
79-
(MetricDefinition) (mc.getRootNode().metrics().get(metric.getName()));
76+
for (Metric metric : allMetrics) {
77+
Metric originalMetric = mc.getRootNode().metrics().get(metric.getName());
78+
8079
Assertions.assertEquals(originalMetric.getName(), metric.getName());
8180
Assertions.assertEquals(originalMetric.getUnit(), metric.getUnit());
8281
}

0 commit comments

Comments
 (0)