BAEL-1064 extract collectors and update micrometer dependency version (#2773)

This commit is contained in:
aietcn 2017-10-21 13:53:44 +08:00 committed by maibin
parent 1d5b17ad6c
commit b3737dd227
2 changed files with 26 additions and 55 deletions

View File

@ -16,7 +16,7 @@
<dep.ver.metrics>3.1.2</dep.ver.metrics> <dep.ver.metrics>3.1.2</dep.ver.metrics>
<dep.ver.servlet>3.1.0</dep.ver.servlet> <dep.ver.servlet>3.1.0</dep.ver.servlet>
<netflix.servo.ver>0.12.17</netflix.servo.ver> <netflix.servo.ver>0.12.17</netflix.servo.ver>
<micrometer.ver>1.0.0-rc.2</micrometer.ver> <micrometer.ver>0.12.0.RELEASE</micrometer.ver>
<spring.boot.ver>2.0.0.M5</spring.boot.ver> <spring.boot.ver>2.0.0.M5</spring.boot.ver>
</properties> </properties>

View File

@ -16,6 +16,7 @@ import java.util.*;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static io.micrometer.core.instrument.Meter.Type;
import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.collection.IsMapContaining.hasEntry; import static org.hamcrest.collection.IsMapContaining.hasEntry;
import static org.hamcrest.core.IsCollectionContaining.hasItems; import static org.hamcrest.core.IsCollectionContaining.hasItems;
@ -27,7 +28,7 @@ import static org.junit.Assert.assertTrue;
*/ */
public class MicrometerAtlasTest { public class MicrometerAtlasTest {
AtlasConfig atlasConfig; private AtlasConfig atlasConfig;
@Before @Before
public void init() { public void init() {
@ -208,27 +209,7 @@ public class MicrometerAtlasTest {
timer.record(8, TimeUnit.SECONDS); timer.record(8, TimeUnit.SECONDS);
timer.record(13, TimeUnit.SECONDS); timer.record(13, TimeUnit.SECONDS);
List<Gauge> quantileGauges = registry Map<String, Integer> quantileMap = extractTagValueMap(registry, Type.Gauge, 1e9);
.getMeters()
.stream()
.filter(meter -> meter
.getType()
.name()
.equals("Gauge"))
.map(meter -> (Gauge) meter)
.collect(Collectors.toList());
assert (3 == quantileGauges.size());
Map<String, Integer> quantileMap = quantileGauges
.stream()
.collect(Collectors.toMap(gauge -> {
Tag tag = gauge
.getId()
.getTags()
.iterator()
.next();
return tag.getKey() + "=" + tag.getValue();
}, gauge -> (int) (gauge.value() / 1e9)));
assertThat(quantileMap.keySet(), hasItems("quantile=0.3", "quantile=0.5", "quantile=0.95")); assertThat(quantileMap.keySet(), hasItems("quantile=0.3", "quantile=0.5", "quantile=0.95"));
assertThat(quantileMap.get("quantile=0.3"), is(2)); assertThat(quantileMap.get("quantile=0.3"), is(2));
@ -236,6 +217,25 @@ public class MicrometerAtlasTest {
assertThat(quantileMap.get("quantile=0.95"), is(8)); assertThat(quantileMap.get("quantile=0.95"), is(8));
} }
private Map<String, Integer> extractTagValueMap(MeterRegistry registry, Type meterType, double valueDivisor) {
return registry
.getMeters()
.stream()
.filter(meter -> meter.getType() == meterType)
.collect(Collectors.toMap(meter -> {
Tag tag = meter
.getId()
.getTags()
.iterator()
.next();
return tag.getKey() + "=" + tag.getValue();
}, meter -> (int) (meter
.measure()
.iterator()
.next()
.getValue() / valueDivisor)));
}
@Test @Test
public void givenDistributionSummary_whenEnrichWithHistograms_thenDataAggregated() { public void givenDistributionSummary_whenEnrichWithHistograms_thenDataAggregated() {
SimpleMeterRegistry registry = new SimpleMeterRegistry(); SimpleMeterRegistry registry = new SimpleMeterRegistry();
@ -243,6 +243,7 @@ public class MicrometerAtlasTest {
.builder("summary") .builder("summary")
.histogram(Histogram.linear(0, 10, 5)) .histogram(Histogram.linear(0, 10, 5))
.register(registry); .register(registry);
hist.record(3); hist.record(3);
hist.record(8); hist.record(8);
hist.record(20); hist.record(20);
@ -250,22 +251,7 @@ public class MicrometerAtlasTest {
hist.record(13); hist.record(13);
hist.record(26); hist.record(26);
Map<String, Integer> histograms = registry Map<String, Integer> histograms = extractTagValueMap(registry, Type.Counter, 1.0);
.getMeters()
.stream()
.filter(meter -> meter.getType() == Meter.Type.Counter)
.collect(Collectors.toMap(counter -> {
Tag tag = counter
.getId()
.getTags()
.iterator()
.next();
return tag.getKey() + "=" + tag.getValue();
}, counter -> (int) counter
.measure()
.iterator()
.next()
.getValue()));
assertThat(histograms, allOf(hasEntry("bucket=0.0", 0), hasEntry("bucket=10.0", 2), hasEntry("bucket=20.0", 2), hasEntry("bucket=30.0", 1), hasEntry("bucket=40.0", 1), hasEntry("bucket=Infinity", 0))); assertThat(histograms, allOf(hasEntry("bucket=0.0", 0), hasEntry("bucket=10.0", 2), hasEntry("bucket=20.0", 2), hasEntry("bucket=30.0", 1), hasEntry("bucket=40.0", 1), hasEntry("bucket=Infinity", 0)));
} }
@ -284,22 +270,7 @@ public class MicrometerAtlasTest {
timer.record(341, TimeUnit.MILLISECONDS); timer.record(341, TimeUnit.MILLISECONDS);
timer.record(500, TimeUnit.MILLISECONDS); timer.record(500, TimeUnit.MILLISECONDS);
Map<String, Integer> histograms = registry Map<String, Integer> histograms = extractTagValueMap(registry, Type.Counter, 1.0);
.getMeters()
.stream()
.filter(meter -> meter.getType() == Meter.Type.Counter)
.collect(Collectors.toMap(counter -> {
Tag tag = counter
.getId()
.getTags()
.iterator()
.next();
return tag.getKey() + "=" + tag.getValue();
}, counter -> (int) counter
.measure()
.iterator()
.next()
.getValue()));
assertThat(histograms, allOf(hasEntry("bucket=0.0", 0), hasEntry("bucket=2.0E8", 1), hasEntry("bucket=4.0E8", 1), hasEntry("bucket=Infinity", 3))); assertThat(histograms, allOf(hasEntry("bucket=0.0", 0), hasEntry("bucket=2.0E8", 1), hasEntry("bucket=4.0E8", 1), hasEntry("bucket=Infinity", 3)));