Upgrade to micrometer 1.0.0-SNAPSHOT

Move to the snapshots and rework changed APIs in preparation for
the upcoming rc8 release.

See gh-11575
pull/11805/head
Jon Schneider 7 years ago committed by Phillip Webb
parent b234501af3
commit d1de1cd053

@ -16,7 +16,6 @@
package org.springframework.boot.actuate.autoconfigure.metrics.amqp; package org.springframework.boot.actuate.autoconfigure.metrics.amqp;
import java.util.List;
import java.util.Map; import java.util.Map;
import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.ConnectionFactory;
@ -67,7 +66,7 @@ public class RabbitMetricsConfiguration {
private void bindConnectionFactoryToRegistry(String beanName, private void bindConnectionFactoryToRegistry(String beanName,
AbstractConnectionFactory connectionFactory) { AbstractConnectionFactory connectionFactory) {
List<Tag> tags = Tags.zip("name", getConnectionFactoryName(beanName)); Iterable<Tag> tags = Tags.zip("name", getConnectionFactoryName(beanName));
new RabbitMetrics(connectionFactory.getRabbitConnectionFactory(), this.metricName, new RabbitMetrics(connectionFactory.getRabbitConnectionFactory(), this.metricName,
tags).bindTo(this.registry); tags).bindTo(this.registry);
} }

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -50,7 +50,7 @@ public class GangliaExportConfiguration {
@ConditionalOnProperty(value = "management.metrics.export.ganglia.enabled", matchIfMissing = true) @ConditionalOnProperty(value = "management.metrics.export.ganglia.enabled", matchIfMissing = true)
public MetricsExporter gangliaExporter(GangliaConfig gangliaConfig, public MetricsExporter gangliaExporter(GangliaConfig gangliaConfig,
HierarchicalNameMapper nameMapper, Clock clock) { HierarchicalNameMapper nameMapper, Clock clock) {
return () -> new GangliaMeterRegistry(gangliaConfig, nameMapper, clock); return () -> new GangliaMeterRegistry(gangliaConfig, clock, nameMapper);
} }
@Bean @Bean

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -50,7 +50,7 @@ public class GraphiteExportConfiguration {
@ConditionalOnProperty(value = "management.metrics.export.graphite.enabled", matchIfMissing = true) @ConditionalOnProperty(value = "management.metrics.export.graphite.enabled", matchIfMissing = true)
public MetricsExporter graphiteExporter(GraphiteConfig graphiteConfig, public MetricsExporter graphiteExporter(GraphiteConfig graphiteConfig,
HierarchicalNameMapper nameMapper, Clock clock) { HierarchicalNameMapper nameMapper, Clock clock) {
return () -> new GraphiteMeterRegistry(graphiteConfig, nameMapper, clock); return () -> new GraphiteMeterRegistry(graphiteConfig, clock, nameMapper);
} }
@Bean @Bean

@ -50,7 +50,7 @@ public class JmxExportConfiguration {
@ConditionalOnProperty(value = "management.metrics.export.jmx.enabled", matchIfMissing = true) @ConditionalOnProperty(value = "management.metrics.export.jmx.enabled", matchIfMissing = true)
public MetricsExporter jmxExporter(JmxConfig config, public MetricsExporter jmxExporter(JmxConfig config,
HierarchicalNameMapper nameMapper, Clock clock) { HierarchicalNameMapper nameMapper, Clock clock) {
return () -> new JmxMeterRegistry(config, nameMapper, clock); return () -> new JmxMeterRegistry(config, clock, nameMapper);
} }
@Bean @Bean

@ -17,7 +17,6 @@
package org.springframework.boot.actuate.autoconfigure.metrics.jdbc; package org.springframework.boot.actuate.autoconfigure.metrics.jdbc;
import java.util.Collection; import java.util.Collection;
import java.util.List;
import java.util.Map; import java.util.Map;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -69,7 +68,7 @@ public class DataSourcePoolMetricsConfiguration {
} }
private void bindDataSourceToRegistry(String beanName, DataSource dataSource) { private void bindDataSourceToRegistry(String beanName, DataSource dataSource) {
List<Tag> tags = Tags.zip("name", getDataSourceName(beanName)); Iterable<Tag> tags = Tags.zip("name", getDataSourceName(beanName));
new DataSourcePoolMetrics(dataSource, this.metadataProviders, this.metricName, new DataSourcePoolMetrics(dataSource, this.metadataProviders, this.metricName,
tags).bindTo(this.registry); tags).bindTo(this.registry);
} }

@ -42,8 +42,8 @@ public class MeterRegistryConfigurerTests {
UserConfigurations.of(MeterRegistryConfigurerConfiguration.class)) UserConfigurations.of(MeterRegistryConfigurerConfiguration.class))
.withPropertyValues("management.metrics.use-global-registry=false") .withPropertyValues("management.metrics.use-global-registry=false")
.run((context) -> assertThat(context.getBean(MeterRegistry.class) .run((context) -> assertThat(context.getBean(MeterRegistry.class)
.find("jvm.memory.used").tags("region", "us-east-1").gauge()) .get("jvm.memory.used").tags("region", "us-east-1").gauge())
.isPresent()); .isNotNull());
} }
@Test @Test
@ -54,8 +54,8 @@ public class MeterRegistryConfigurerTests {
UserConfigurations.of(MeterRegistryConfigurerConfiguration.class)) UserConfigurations.of(MeterRegistryConfigurerConfiguration.class))
.withPropertyValues("management.metrics.use-global-registry=false") .withPropertyValues("management.metrics.use-global-registry=false")
.run((context) -> assertThat(context.getBean(MeterRegistry.class) .run((context) -> assertThat(context.getBean(MeterRegistry.class)
.find("my.thing").tags("region", "us-east-1").gauge()) .get("my.thing").tags("region", "us-east-1").gauge())
.isPresent()); .isNotNull());
} }
static class MeterRegistryConfigurerConfiguration { static class MeterRegistryConfigurerConfiguration {

@ -22,7 +22,6 @@ import java.util.Set;
import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.MockClock; import io.micrometer.core.instrument.MockClock;
import io.micrometer.core.instrument.Statistic;
import io.micrometer.core.instrument.binder.MeterBinder; import io.micrometer.core.instrument.binder.MeterBinder;
import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics; import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics;
import io.micrometer.core.instrument.binder.logging.LogbackMetrics; import io.micrometer.core.instrument.binder.logging.LogbackMetrics;
@ -94,15 +93,15 @@ public class MetricsAutoConfigurationIntegrationTests {
"{\"message\": \"hello\"}", MediaType.APPLICATION_JSON)); "{\"message\": \"hello\"}", MediaType.APPLICATION_JSON));
assertThat(this.external.getForObject("/api/external", Map.class)) assertThat(this.external.getForObject("/api/external", Map.class))
.containsKey("message"); .containsKey("message");
assertThat(this.registry.find("http.client.requests").value(Statistic.Count, 1.0) assertThat(this.registry.get("http.client.requests").timer().count())
.timer()).isPresent(); .isEqualTo(1);
} }
@Test @Test
public void requestMappingIsInstrumented() { public void requestMappingIsInstrumented() {
this.loopback.getForObject("/api/people", Set.class); this.loopback.getForObject("/api/people", Set.class);
assertThat(this.registry.find("http.server.requests").value(Statistic.Count, 1.0) assertThat(this.registry.get("http.server.requests").timer().count())
.timer()).isPresent(); .isEqualTo(1);
} }
@Test @Test

@ -54,8 +54,8 @@ public class MetricsAutoConfigurationTests {
.run((context) -> { .run((context) -> {
context.getBean(DataSource.class).getConnection().getMetaData(); context.getBean(DataSource.class).getConnection().getMetaData();
MeterRegistry registry = context.getBean(MeterRegistry.class); MeterRegistry registry = context.getBean(MeterRegistry.class);
assertThat(registry.find("data.source.max.connections") registry.get("data.source.max.connections").tags("name", "dataSource")
.tags("name", "dataSource").meter()).isPresent(); .meter();
}); });
} }
@ -69,8 +69,8 @@ public class MetricsAutoConfigurationTests {
.run((context) -> { .run((context) -> {
context.getBean(DataSource.class).getConnection().getMetaData(); context.getBean(DataSource.class).getConnection().getMetaData();
MeterRegistry registry = context.getBean(MeterRegistry.class); MeterRegistry registry = context.getBean(MeterRegistry.class);
assertThat(registry.find("custom.name.max.connections") registry.get("custom.name.max.connections").tags("name", "dataSource")
.tags("name", "dataSource").meter()).isPresent(); .meter();
}); });
} }
@ -85,7 +85,7 @@ public class MetricsAutoConfigurationTests {
context.getBean(DataSource.class).getConnection().getMetaData(); context.getBean(DataSource.class).getConnection().getMetaData();
MeterRegistry registry = context.getBean(MeterRegistry.class); MeterRegistry registry = context.getBean(MeterRegistry.class);
assertThat(registry.find("data.source.max.connections") assertThat(registry.find("data.source.max.connections")
.tags("name", "dataSource").meter()).isNotPresent(); .tags("name", "dataSource").meter()).isNull();
}); });
} }
@ -100,10 +100,10 @@ public class MetricsAutoConfigurationTests {
context.getBean("secondOne", DataSource.class).getConnection() context.getBean("secondOne", DataSource.class).getConnection()
.getMetaData(); .getMetaData();
MeterRegistry registry = context.getBean(MeterRegistry.class); MeterRegistry registry = context.getBean(MeterRegistry.class);
assertThat(registry.find("data.source.max.connections") registry.get("data.source.max.connections").tags("name", "first")
.tags("name", "first").meter()).isPresent(); .meter();
assertThat(registry.find("data.source.max.connections") registry.get("data.source.max.connections").tags("name", "secondOne")
.tags("name", "secondOne").meter()).isPresent(); .meter();
}); });
} }

@ -46,7 +46,7 @@ public class RabbitMetricsConfigurationTests {
public void autoConfiguredConnectionFactoryIsInstrumented() { public void autoConfiguredConnectionFactoryIsInstrumented() {
this.contextRunner.run((context) -> { this.contextRunner.run((context) -> {
MeterRegistry registry = context.getBean(MeterRegistry.class); MeterRegistry registry = context.getBean(MeterRegistry.class);
assertThat(registry.find("rabbitmq.connections").meter()).isPresent(); registry.get("rabbitmq.connections").meter();
}); });
} }
@ -56,10 +56,8 @@ public class RabbitMetricsConfigurationTests {
.withPropertyValues("management.metrics.rabbitmq.metric-name=custom.name") .withPropertyValues("management.metrics.rabbitmq.metric-name=custom.name")
.run((context) -> { .run((context) -> {
MeterRegistry registry = context.getBean(MeterRegistry.class); MeterRegistry registry = context.getBean(MeterRegistry.class);
assertThat(registry.find("custom.name.connections").meter()) registry.get("custom.name.connections").meter();
.isPresent(); assertThat(registry.find("rabbitmq.connections").meter()).isNull();
assertThat(registry.find("rabbitmq.connections").meter())
.isNotPresent();
}); });
} }
@ -69,8 +67,7 @@ public class RabbitMetricsConfigurationTests {
.withPropertyValues("management.metrics.rabbitmq.instrument=false") .withPropertyValues("management.metrics.rabbitmq.instrument=false")
.run((context) -> { .run((context) -> {
MeterRegistry registry = context.getBean(MeterRegistry.class); MeterRegistry registry = context.getBean(MeterRegistry.class);
assertThat(registry.find("rabbitmq.connections").meter()) assertThat(registry.find("rabbitmq.connections").meter()).isNull();
.isNotPresent();
}); });
} }

@ -48,10 +48,10 @@ public class CacheMetricsConfigurationTests {
this.contextRunner.withPropertyValues("spring.cache.type=caffeine", this.contextRunner.withPropertyValues("spring.cache.type=caffeine",
"spring.cache.cache-names=cache1,cache2").run((context) -> { "spring.cache.cache-names=cache1,cache2").run((context) -> {
MeterRegistry registry = context.getBean(MeterRegistry.class); MeterRegistry registry = context.getBean(MeterRegistry.class);
assertThat(registry.find("cache.requests").tags("name", "cache1") registry.get("cache.requests").tags("name", "cache1")
.tags("cacheManager", "cacheManager").meter()).isPresent(); .tags("cacheManager", "cacheManager").meter();
assertThat(registry.find("cache.requests").tags("name", "cache2") registry.get("cache.requests").tags("name", "cache2")
.tags("cacheManager", "cacheManager").meter()).isPresent(); .tags("cacheManager", "cacheManager").meter();
}); });
} }
@ -62,10 +62,8 @@ public class CacheMetricsConfigurationTests {
"spring.cache.type=caffeine", "spring.cache.cache-names=cache1") "spring.cache.type=caffeine", "spring.cache.cache-names=cache1")
.run((context) -> { .run((context) -> {
MeterRegistry registry = context.getBean(MeterRegistry.class); MeterRegistry registry = context.getBean(MeterRegistry.class);
assertThat( registry.get("custom.name.requests").tags("name", "cache1")
registry.find("custom.name.requests").tags("name", "cache1") .tags("cacheManager", "cacheManager").meter();
.tags("cacheManager", "cacheManager").meter())
.isPresent();
}); });
} }
@ -75,9 +73,9 @@ public class CacheMetricsConfigurationTests {
"spring.cache.cache-names=cache1,cache2").run((context) -> { "spring.cache.cache-names=cache1,cache2").run((context) -> {
MeterRegistry registry = context.getBean(MeterRegistry.class); MeterRegistry registry = context.getBean(MeterRegistry.class);
assertThat(registry.find("cache.requests").tags("name", "cache1") assertThat(registry.find("cache.requests").tags("name", "cache1")
.tags("cacheManager", "cacheManager").meter()).isNotPresent(); .tags("cacheManager", "cacheManager").meter()).isNull();
assertThat(registry.find("cache.requests").tags("name", "cache2") assertThat(registry.find("cache.requests").tags("name", "cache2")
.tags("cacheManager", "cacheManager").meter()).isNotPresent(); .tags("cacheManager", "cacheManager").meter()).isNull();
}); });
} }
@ -89,7 +87,7 @@ public class CacheMetricsConfigurationTests {
.run((context) -> { .run((context) -> {
MeterRegistry registry = context.getBean(MeterRegistry.class); MeterRegistry registry = context.getBean(MeterRegistry.class);
assertThat(registry.find("cache.requests").tags("name", "cache1") assertThat(registry.find("cache.requests").tags("name", "cache1")
.tags("cacheManager", "cacheManager").meter()).isNotPresent(); .tags("cacheManager", "cacheManager").meter()).isNull();
}); });
} }

@ -74,10 +74,10 @@ public class RestTemplateMetricsConfigurationTests {
private void validateRestTemplate(RestTemplate restTemplate, MeterRegistry registry) { private void validateRestTemplate(RestTemplate restTemplate, MeterRegistry registry) {
MockRestServiceServer server = MockRestServiceServer.createServer(restTemplate); MockRestServiceServer server = MockRestServiceServer.createServer(restTemplate);
server.expect(requestTo("/test")).andRespond(withStatus(HttpStatus.OK)); server.expect(requestTo("/test")).andRespond(withStatus(HttpStatus.OK));
assertThat(registry.find("http.client.requests").meter()).isNotPresent(); assertThat(registry.find("http.client.requests").meter()).isNull();
assertThat(restTemplate.getForEntity("/test", Void.class).getStatusCode()) assertThat(restTemplate.getForEntity("/test", Void.class).getStatusCode())
.isEqualTo(HttpStatus.OK); .isEqualTo(HttpStatus.OK);
assertThat(registry.find("http.client.requests").meter()).isPresent(); registry.get("http.client.requests").meter();
} }
@Configuration @Configuration

@ -79,8 +79,8 @@ public class CacheMetricsRegistrar {
} }
@SuppressWarnings({ "unchecked", "rawtypes" }) @SuppressWarnings({ "unchecked", "rawtypes" })
private MeterBinder getMeterBinder(Cache cache, List<Tag> tags) { private MeterBinder getMeterBinder(Cache cache, Iterable<Tag> tags) {
tags.addAll(getAdditionalTags(cache)); Iterable<Tag> withAdditionalTags = Tags.concat(tags, getAdditionalTags(cache));
for (CacheMeterBinderProvider<?> binderProvider : this.binderProviders) { for (CacheMeterBinderProvider<?> binderProvider : this.binderProviders) {
Class<?> cacheType = ResolvableType Class<?> cacheType = ResolvableType
.forClass(CacheMeterBinderProvider.class, binderProvider.getClass()) .forClass(CacheMeterBinderProvider.class, binderProvider.getClass())
@ -88,7 +88,7 @@ public class CacheMetricsRegistrar {
if (cacheType.isInstance(cache)) { if (cacheType.isInstance(cache)) {
try { try {
MeterBinder meterBinder = ((CacheMeterBinderProvider) binderProvider) MeterBinder meterBinder = ((CacheMeterBinderProvider) binderProvider)
.getMeterBinder(cache, this.metricName, tags); .getMeterBinder(cache, this.metricName, withAdditionalTags);
if (meterBinder != null) { if (meterBinder != null) {
return meterBinder; return meterBinder;
} }
@ -120,7 +120,7 @@ public class CacheMetricsRegistrar {
* @param cache the cache * @param cache the cache
* @return a list of additional tags to associate to that {@code cache}. * @return a list of additional tags to associate to that {@code cache}.
*/ */
protected List<Tag> getAdditionalTags(Cache cache) { protected Iterable<Tag> getAdditionalTags(Cache cache) {
return Tags.zip("name", cache.getName()); return Tags.zip("name", cache.getName());
} }

@ -64,7 +64,7 @@ public class WebMvcMetrics {
private static final Log logger = LogFactory.getLog(WebMvcMetrics.class); private static final Log logger = LogFactory.getLog(WebMvcMetrics.class);
private final Map<HttpServletRequest, Long> longTaskTimerIds = Collections private final Map<HttpServletRequest, LongTaskTimer.Sample> longTaskTimerSamples = Collections
.synchronizedMap(new IdentityHashMap<>()); .synchronizedMap(new IdentityHashMap<>());
private final MeterRegistry registry; private final MeterRegistry registry;
@ -103,7 +103,7 @@ public class WebMvcMetrics {
logWarning(request, handler); logWarning(request, handler);
return; return;
} }
this.longTaskTimerIds.put(request, this.longTaskTimerSamples.put(request,
longTaskTimer(config, request, handler).start()); longTaskTimer(config, request, handler).start());
}); });
} }
@ -138,8 +138,10 @@ public class WebMvcMetrics {
private void completeLongTimerTask(HttpServletRequest request, Object handler, private void completeLongTimerTask(HttpServletRequest request, Object handler,
TimerConfig config) { TimerConfig config) {
if (config.getName() != null) { if (config.getName() != null) {
longTaskTimer(config, request, handler) LongTaskTimer.Sample sample = this.longTaskTimerSamples.remove(request);
.stop(this.longTaskTimerIds.remove(request)); if (sample != null) {
sample.stop();
}
} }
} }

@ -36,7 +36,7 @@ public class RabbitMetricsTests {
ConnectionFactory connectionFactory = mockConnectionFactory(); ConnectionFactory connectionFactory = mockConnectionFactory();
SimpleMeterRegistry registry = new SimpleMeterRegistry(); SimpleMeterRegistry registry = new SimpleMeterRegistry();
new RabbitMetrics(connectionFactory, "rabbit", null).bindTo(registry); new RabbitMetrics(connectionFactory, "rabbit", null).bindTo(registry);
assertThat(registry.find("rabbit.connections").meter()).isPresent(); registry.get("rabbit.connections");
} }
@Test @Test
@ -45,10 +45,9 @@ public class RabbitMetricsTests {
SimpleMeterRegistry registry = new SimpleMeterRegistry(); SimpleMeterRegistry registry = new SimpleMeterRegistry();
new RabbitMetrics(connectionFactory, "test", Tags.zip("env", "prod")) new RabbitMetrics(connectionFactory, "test", Tags.zip("env", "prod"))
.bindTo(registry); .bindTo(registry);
assertThat(registry.find("test.connections").tags("env", "prod").meter()) assertThat(registry.get("test.connections").tags("env", "prod").meter())
.isPresent(); .isNotNull();
assertThat(registry.find("test.connections").tags("env", "dev").meter()) assertThat(registry.find("test.connections").tags("env", "dev").meter()).isNull();
.isNotPresent();
} }
private ConnectionFactory mockConnectionFactory() { private ConnectionFactory mockConnectionFactory() {

@ -42,8 +42,8 @@ public class CacheMetricsRegistrarTests {
"root", Collections.singleton(new CaffeineCacheMeterBinderProvider())); "root", Collections.singleton(new CaffeineCacheMeterBinderProvider()));
assertThat(registrar.bindCacheToRegistry( assertThat(registrar.bindCacheToRegistry(
new CaffeineCache("test", Caffeine.newBuilder().build()))).isTrue(); new CaffeineCache("test", Caffeine.newBuilder().build()))).isTrue();
assertThat(this.meterRegistry.find("root.requests").tags("name", "test").meter()) assertThat(this.meterRegistry.get("root.requests").tags("name", "test").meter())
.isPresent(); .isNotNull();
} }
@Test @Test
@ -53,7 +53,7 @@ public class CacheMetricsRegistrarTests {
assertThat(registrar.bindCacheToRegistry( assertThat(registrar.bindCacheToRegistry(
new CaffeineCache("test", Caffeine.newBuilder().build()))).isFalse(); new CaffeineCache("test", Caffeine.newBuilder().build()))).isFalse();
assertThat(this.meterRegistry.find("root.requests").tags("name", "test").meter()) assertThat(this.meterRegistry.find("root.requests").tags("name", "test").meter())
.isNotPresent(); .isNull();
} }
} }

@ -21,7 +21,6 @@ import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Statistic;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry; import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -57,13 +56,10 @@ public class SpringIntegrationMetricsIntegrationTests {
@Test @Test
public void springIntegrationMetrics() { public void springIntegrationMetrics() {
this.converter.fahrenheitToCelsius(68.0); this.converter.fahrenheitToCelsius(68.0);
assertThat(this.registry.find("spring.integration.channel.sends") assertThat(this.registry.get("spring.integration.channel.sends")
.tags("channel", "convert.input").value(Statistic.Count, 1).meter()) .tags("channel", "convert.input").functionCounter().count()).isEqualTo(1);
.isPresent(); this.registry.get("spring.integration.handler.duration.min").gauge();
assertThat(this.registry.find("spring.integration.handler.duration.min").meter()) this.registry.get("spring.integration.sourceNames").meter();
.isPresent();
assertThat(this.registry.find("spring.integration.sourceNames").meter())
.isPresent();
} }
@Configuration @Configuration

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,8 +32,6 @@ import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import static org.assertj.core.api.Assertions.assertThat;
/** /**
* Tests for {@link DataSourcePoolMetrics}. * Tests for {@link DataSourcePoolMetrics}.
* *
@ -52,8 +50,8 @@ public class DataSourcePoolMetricsTests {
"metrics.use-global-registry=false") "metrics.use-global-registry=false")
.run((context) -> { .run((context) -> {
context.getBean(DataSource.class).getConnection().getMetaData(); context.getBean(DataSource.class).getConnection().getMetaData();
assertThat(context.getBean(MeterRegistry.class) context.getBean(MeterRegistry.class)
.find("data.source.max.connections").meter()).isPresent(); .get("data.source.max.connections").meter();
}); });
} }

@ -20,7 +20,6 @@ import java.util.stream.StreamSupport;
import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.MockClock; import io.micrometer.core.instrument.MockClock;
import io.micrometer.core.instrument.Statistic;
import io.micrometer.core.instrument.Tag; import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.simple.SimpleConfig; import io.micrometer.core.instrument.simple.SimpleConfig;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry; import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
@ -71,9 +70,9 @@ public class MetricsRestTemplateCustomizerTests {
.meters()).anySatisfy((m) -> assertThat( .meters()).anySatisfy((m) -> assertThat(
StreamSupport.stream(m.getId().getTags().spliterator(), false) StreamSupport.stream(m.getId().getTags().spliterator(), false)
.map(Tag::getKey)).doesNotContain("bucket")); .map(Tag::getKey)).doesNotContain("bucket"));
assertThat(this.registry.find("http.client.requests") assertThat(this.registry.get("http.client.requests")
.tags("method", "GET", "uri", "/test/{id}", "status", "200") .tags("method", "GET", "uri", "/test/{id}", "status", "200").timer()
.value(Statistic.Count, 1.0).timer()).isPresent(); .count()).isEqualTo(1);
assertThat(result).isEqualTo("OK"); assertThat(result).isEqualTo("OK");
mockServer.verify(); mockServer.verify();
} }

@ -72,9 +72,8 @@ public class WebMvcMetricsFilterAutoTimedTests {
@Test @Test
public void metricsCanBeAutoTimed() throws Exception { public void metricsCanBeAutoTimed() throws Exception {
this.mvc.perform(get("/api/10")).andExpect(status().isOk()); this.mvc.perform(get("/api/10")).andExpect(status().isOk());
assertThat( assertThat(this.registry.get("http.server.requests").tags("status", "200").timer()
this.registry.find("http.server.requests").tags("status", "200").timer()) .count()).isEqualTo(1L);
.hasValueSatisfying((t) -> assertThat(t.count()).isEqualTo(1));
} }
@Configuration @Configuration

@ -34,7 +34,6 @@ import io.micrometer.core.annotation.Timed;
import io.micrometer.core.instrument.Clock; import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.Meter; import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Statistic;
import io.micrometer.core.instrument.Tag; import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.composite.CompositeMeterRegistry; import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
import io.micrometer.core.instrument.config.MeterFilter; import io.micrometer.core.instrument.config.MeterFilter;
@ -112,38 +111,38 @@ public class WebMvcMetricsFilterTests {
@Test @Test
public void timedMethod() throws Exception { public void timedMethod() throws Exception {
this.mvc.perform(get("/api/c1/10")).andExpect(status().isOk()); this.mvc.perform(get("/api/c1/10")).andExpect(status().isOk());
assertThat(this.registry.find("http.server.requests") assertThat(this.registry.get("http.server.requests")
.tags("status", "200", "uri", "/api/c1/{id}", "public", "true") .tags("status", "200", "uri", "/api/c1/{id}", "public", "true").timer()
.value(Statistic.Count, 1.0).timer()).isPresent(); .count()).isEqualTo(1L);
} }
@Test @Test
public void subclassedTimedMethod() throws Exception { public void subclassedTimedMethod() throws Exception {
this.mvc.perform(get("/api/c1/metaTimed/10")).andExpect(status().isOk()); this.mvc.perform(get("/api/c1/metaTimed/10")).andExpect(status().isOk());
assertThat(this.registry.find("http.server.requests") assertThat(this.registry.get("http.server.requests")
.tags("status", "200", "uri", "/api/c1/metaTimed/{id}") .tags("status", "200", "uri", "/api/c1/metaTimed/{id}").timer().count())
.value(Statistic.Count, 1.0).timer()).isPresent(); .isEqualTo(1L);
} }
@Test @Test
public void untimedMethod() throws Exception { public void untimedMethod() throws Exception {
this.mvc.perform(get("/api/c1/untimed/10")).andExpect(status().isOk()); this.mvc.perform(get("/api/c1/untimed/10")).andExpect(status().isOk());
assertThat(this.registry.find("http.server.requests") assertThat(this.registry.find("http.server.requests")
.tags("uri", "/api/c1/untimed/10").timer()).isEmpty(); .tags("uri", "/api/c1/untimed/10").timer()).isNull();
} }
@Test @Test
public void timedControllerClass() throws Exception { public void timedControllerClass() throws Exception {
this.mvc.perform(get("/api/c2/10")).andExpect(status().isOk()); this.mvc.perform(get("/api/c2/10")).andExpect(status().isOk());
assertThat(this.registry.find("http.server.requests").tags("status", "200") assertThat(this.registry.get("http.server.requests").tags("status", "200").timer()
.value(Statistic.Count, 1.0).timer()).isPresent(); .count()).isEqualTo(1L);
} }
@Test @Test
public void badClientRequest() throws Exception { public void badClientRequest() throws Exception {
this.mvc.perform(get("/api/c1/oops")).andExpect(status().is4xxClientError()); this.mvc.perform(get("/api/c1/oops")).andExpect(status().is4xxClientError());
assertThat(this.registry.find("http.server.requests").tags("status", "400") assertThat(this.registry.get("http.server.requests").tags("status", "400").timer()
.value(Statistic.Count, 1.0).timer()).isPresent(); .count()).isEqualTo(1L);
} }
@Test @Test
@ -151,8 +150,8 @@ public class WebMvcMetricsFilterTests {
this.mvc.perform(get("/api/redirect") this.mvc.perform(get("/api/redirect")
.header(RedirectAndNotFoundFilter.TEST_MISBEHAVE_HEADER, "302")) .header(RedirectAndNotFoundFilter.TEST_MISBEHAVE_HEADER, "302"))
.andExpect(status().is3xxRedirection()); .andExpect(status().is3xxRedirection());
assertThat(this.registry.find("http.server.requests").tags("uri", "REDIRECTION") assertThat(this.registry.get("http.server.requests").tags("uri", "REDIRECTION")
.tags("status", "302").timer()).isPresent(); .tags("status", "302").timer()).isNotNull();
} }
@Test @Test
@ -160,8 +159,8 @@ public class WebMvcMetricsFilterTests {
this.mvc.perform(get("/api/not/found") this.mvc.perform(get("/api/not/found")
.header(RedirectAndNotFoundFilter.TEST_MISBEHAVE_HEADER, "404")) .header(RedirectAndNotFoundFilter.TEST_MISBEHAVE_HEADER, "404"))
.andExpect(status().is4xxClientError()); .andExpect(status().is4xxClientError());
assertThat(this.registry.find("http.server.requests").tags("uri", "NOT_FOUND") assertThat(this.registry.get("http.server.requests").tags("uri", "NOT_FOUND")
.tags("status", "404").timer()).isPresent(); .tags("status", "404").timer()).isNotNull();
} }
@Test @Test
@ -169,9 +168,8 @@ public class WebMvcMetricsFilterTests {
assertThatCode(() -> this.mvc.perform(get("/api/c1/unhandledError/10")) assertThatCode(() -> this.mvc.perform(get("/api/c1/unhandledError/10"))
.andExpect(status().isOk())) .andExpect(status().isOk()))
.hasRootCauseInstanceOf(RuntimeException.class); .hasRootCauseInstanceOf(RuntimeException.class);
assertThat(this.registry.find("http.server.requests") assertThat(this.registry.get("http.server.requests")
.tags("exception", "RuntimeException").value(Statistic.Count, 1.0) .tags("exception", "RuntimeException").timer().count()).isEqualTo(1L);
.timer()).isPresent();
} }
@Test @Test
@ -180,30 +178,30 @@ public class WebMvcMetricsFilterTests {
.andExpect(request().asyncStarted()).andReturn(); .andExpect(request().asyncStarted()).andReturn();
// the request is not prematurely recorded as complete // the request is not prematurely recorded as complete
assertThat(this.registry.find("http.server.requests").tags("uri", "/api/c1/async") assertThat(this.registry.find("http.server.requests").tags("uri", "/api/c1/async")
.timer()).isNotPresent(); .timer()).isNull();
// while the mapping is running, it contributes to the activeTasks count // while the mapping is running, it contributes to the activeTasks count
assertThat(this.registry.find("my.long.request").tags("region", "test") assertThat(this.registry.get("my.long.request").tags("region", "test")
.value(Statistic.Count, 1.0).longTaskTimer()).isPresent(); .longTaskTimer().activeTasks()).isEqualTo(1);
// once the mapping completes, we can gather information about status, etc. // once the mapping completes, we can gather information about status, etc.
this.asyncLatch.countDown(); this.asyncLatch.countDown();
this.mvc.perform(asyncDispatch(result)).andExpect(status().isOk()); this.mvc.perform(asyncDispatch(result)).andExpect(status().isOk());
assertThat(this.registry.find("http.server.requests").tags("status", "200") assertThat(this.registry.get("http.server.requests").tags("status", "200").timer()
.value(Statistic.Count, 1.0).timer()).isPresent(); .count()).isEqualTo(1L);
} }
@Test @Test
public void endpointThrowsError() throws Exception { public void endpointThrowsError() throws Exception {
this.mvc.perform(get("/api/c1/error/10")).andExpect(status().is4xxClientError()); this.mvc.perform(get("/api/c1/error/10")).andExpect(status().is4xxClientError());
assertThat(this.registry.find("http.server.requests").tags("status", "422") assertThat(this.registry.get("http.server.requests").tags("status", "422").timer()
.value(Statistic.Count, 1.0).timer()).isPresent(); .count()).isEqualTo(1L);
} }
@Test @Test
public void regexBasedRequestMapping() throws Exception { public void regexBasedRequestMapping() throws Exception {
this.mvc.perform(get("/api/c1/regex/.abc")).andExpect(status().isOk()); this.mvc.perform(get("/api/c1/regex/.abc")).andExpect(status().isOk());
assertThat(this.registry.find("http.server.requests") assertThat(this.registry.get("http.server.requests")
.tags("uri", "/api/c1/regex/{id:\\.[a-z]+}").value(Statistic.Count, 1.0) .tags("uri", "/api/c1/regex/{id:\\.[a-z]+}").timer().count())
.timer()).isPresent(); .isEqualTo(1L);
} }
@Test @Test

@ -20,7 +20,6 @@ import io.micrometer.core.annotation.Timed;
import io.micrometer.core.instrument.Clock; import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.MockClock; import io.micrometer.core.instrument.MockClock;
import io.micrometer.core.instrument.Statistic;
import io.micrometer.core.instrument.simple.SimpleConfig; import io.micrometer.core.instrument.simple.SimpleConfig;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry; import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.junit.Before; import org.junit.Before;
@ -79,18 +78,18 @@ public class WebMvcMetricsIntegrationTests {
@Test @Test
public void handledExceptionIsRecordedInMetricTag() throws Exception { public void handledExceptionIsRecordedInMetricTag() throws Exception {
this.mvc.perform(get("/api/handledError")).andExpect(status().is5xxServerError()); this.mvc.perform(get("/api/handledError")).andExpect(status().is5xxServerError());
assertThat(this.registry.find("http.server.requests") assertThat(this.registry.get("http.server.requests")
.tags("exception", "Exception1", "status", "500") .tags("exception", "Exception1", "status", "500").timer().count())
.value(Statistic.Count, 1.0).timer()).isPresent(); .isEqualTo(1L);
} }
@Test @Test
public void rethrownExceptionIsRecordedInMetricTag() { public void rethrownExceptionIsRecordedInMetricTag() {
assertThatCode(() -> this.mvc.perform(get("/api/rethrownError")) assertThatCode(() -> this.mvc.perform(get("/api/rethrownError"))
.andExpect(status().is5xxServerError())); .andExpect(status().is5xxServerError()));
assertThat(this.registry.find("http.server.requests") assertThat(this.registry.get("http.server.requests")
.tags("exception", "Exception2", "status", "500") .tags("exception", "Exception2", "status", "500").timer().count())
.value(Statistic.Count, 1.0).timer()).isPresent(); .isEqualTo(1L);
} }
@Configuration @Configuration

@ -112,7 +112,7 @@
<logback.version>1.2.3</logback.version> <logback.version>1.2.3</logback.version>
<lombok.version>1.16.20</lombok.version> <lombok.version>1.16.20</lombok.version>
<mariadb.version>2.2.1</mariadb.version> <mariadb.version>2.2.1</mariadb.version>
<micrometer.version>1.0.0-rc.7</micrometer.version> <micrometer.version>1.0.0-SNAPSHOT</micrometer.version>
<mssql-jdbc.version>6.2.2.jre8</mssql-jdbc.version> <mssql-jdbc.version>6.2.2.jre8</mssql-jdbc.version>
<mockito.version>2.13.0</mockito.version> <mockito.version>2.13.0</mockito.version>
<mongo-driver-reactivestreams.version>1.7.0</mongo-driver-reactivestreams.version> <mongo-driver-reactivestreams.version>1.7.0</mongo-driver-reactivestreams.version>

Loading…
Cancel
Save