Skip to content

Commit 8a40ef3

Browse files
Dropwizard based custom metric client - code review
1 parent 7b863fa commit 8a40ef3

File tree

4 files changed

+16
-17
lines changed

4 files changed

+16
-17
lines changed

cf-java-monitoring-custom-metrics-clients/cf-custom-metrics-clients-core/src/test/java/com/sap/cloud/cf/monitoring/client/configuration/CustomMetricsConfigurationFactoryTest.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ public void testMatches_WithoutEnv() throws Exception {
2020

2121
@Test
2222
public void testMatches_WithEmptyEnv() throws Exception {
23-
String[] CUSTOM_METRICS_ENV = new String[] { "CUSTOM_METRICS", "" };
24-
EnvUtils.setEnvs(new String[][] { CUSTOM_METRICS_ENV });
23+
EnvUtils.setEnvs(new String[][] { { "CUSTOM_METRICS", "" } });
2524

2625
testDefault();
2726
}
@@ -78,10 +77,10 @@ private static String[] getCustomMetricsEnv(String interval) {
7877

7978
private static String getJson(String interval) {
8079
return "{\n" + //
81-
" \"interval\": \"" + interval + "\",\n" + //
82-
" \"enabled\": \"false\",\n" + //
83-
" \"metrics\": [\"timer\", \"summary\"],\n" + //
84-
" \"metricQuantiles\": \"true\"\n" + //
85-
"}";
80+
" \"interval\": \"" + interval + "\",\n" + //
81+
" \"enabled\": \"false\",\n" + //
82+
" \"metrics\": [\"timer\", \"summary\"],\n" + //
83+
" \"metricQuantiles\": \"true\"\n" + //
84+
"}";
8685
}
8786
}

cf-java-monitoring-custom-metrics-clients/cf-custom-metrics-clients-java/src/main/java/com/sap/cloud/cf/monitoring/java/converter/GaugeConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ protected List<Metric> convertMetricEntry(Entry<String, Gauge> metricEntry, long
1818
result.add(buildCustomMetric(metricEntry.getKey() + ".value", number.doubleValue(), MetricType.GAUGE,
1919
timestamp));
2020
} else {
21-
throw new IllegalArgumentException(String.format("The type for Gauge {%s} is invalid. The supported type is {%s}", gaugeValue.getClass().getName(), Number.class.getName()));
21+
throw new IllegalArgumentException(String.format("The type {%s} for Gauge {%s} is invalid. The supported type is {%s}", gaugeValue.getClass().getName(), metricEntry.getKey(), Number.class.getName()));
2222
}
2323
return result;
2424
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package com.sap.cloud.cf.monitoring.java.converter;
22

3-
import java.util.ArrayList;
43
import java.util.HashMap;
54
import java.util.List;
65
import java.util.Map;
76
import java.util.Map.Entry;
7+
import java.util.stream.Collectors;
88

99
import com.sap.cloud.cf.monitoring.client.model.Metric;
1010

1111
public abstract class MetricConverter<T> {
1212
private static final String KEY_TYPE = "type";
1313

1414
public enum MetricType {
15-
TIMER("timer"), HISTOGRAM("histogram"), GAUGE("gauge"), METER("meter"), COUNTER("counter");
15+
TIMER("timer"), HISTOGRAM("histogram"), GAUGE("gauge"), METER("meter"), COUNTER("counter");
1616

1717
private final String metricTypeName;
1818

@@ -26,11 +26,11 @@ private MetricType(String metricTypeName) {
2626
}
2727

2828
public List<Metric> convert(Map<String, T> metrics, long timestamp) {
29-
ArrayList<Metric> result = new ArrayList<>();
30-
for (Entry<String, T> entry: metrics.entrySet()) {
31-
result.addAll(convertMetricEntry(entry, timestamp));
32-
}
33-
return result;
29+
return metrics.entrySet()
30+
.stream()
31+
.map(entry -> convertMetricEntry(entry, timestamp))
32+
.flatMap(List::stream)
33+
.collect(Collectors.toList());
3434
}
3535

3636
protected abstract List<Metric> convertMetricEntry(Entry<String, T> metricEntry, long timestamp);
@@ -40,4 +40,4 @@ protected Metric buildCustomMetric(String name, double value, MetricType type, l
4040
tags.put(KEY_TYPE, type.getMetricTypeName());
4141
return new Metric(name, value, timestamp, tags);
4242
}
43-
}
43+
}

cf-java-monitoring-custom-metrics-clients/cf-custom-metrics-clients-java/src/test/java/com/sap/cloud/cf/monitoring/java/converter/GaugeConverterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public String getValue() {
5656
gauges.put(GAUGE_METRIC, gauge);
5757

5858
expectedException.expect(IllegalArgumentException.class);
59-
expectedException.expectMessage(String.format("The type for Gauge {%s} is invalid. The supported type is {%s}", gauge.getValue().getClass().getName(), Number.class.getName()));
59+
expectedException.expectMessage(String.format("The type {%s} for Gauge {%s} is invalid. The supported type is {%s}", gauge.getValue().getClass().getName(), GAUGE_METRIC, Number.class.getName()));
6060

6161
new GaugeConverter().convert(gauges, currentTimeMillis);
6262
}

0 commit comments

Comments
 (0)