Merge pull request #11918 from Jon Schneider

* gh-11918:
  Polish "Align with Micrometer's standardized cache metrics"
  Align with Micrometer's standardized cache metrics
pull/11922/head
Andy Wilkinson 7 years ago
commit 6b70c96e37

@ -18,7 +18,6 @@ package org.springframework.boot.actuate.autoconfigure.metrics.cache;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.Cache; import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager; import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -33,7 +32,6 @@ import org.springframework.context.annotation.Import;
@Configuration @Configuration
@ConditionalOnBean(CacheManager.class) @ConditionalOnBean(CacheManager.class)
@ConditionalOnProperty(value = "management.metrics.cache.instrument", matchIfMissing = true) @ConditionalOnProperty(value = "management.metrics.cache.instrument", matchIfMissing = true)
@EnableConfigurationProperties(CacheMetricsProperties.class)
@Import({ CacheMeterBinderProvidersConfiguration.class, @Import({ CacheMeterBinderProvidersConfiguration.class,
CacheMetricsRegistrarConfiguration.class }) CacheMetricsRegistrarConfiguration.class })
public class CacheMetricsConfiguration { public class CacheMetricsConfiguration {

@ -1,43 +0,0 @@
/*
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.autoconfigure.metrics.cache;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Configuration properties for Cache-based metrics.
*
* @author Stephane Nicoll
* @since 2.0.0
*/
@ConfigurationProperties("management.metrics.cache")
public class CacheMetricsProperties {
/**
* Name of the metric for cache usage.
*/
private String metricName = "cache";
public String getMetricName() {
return this.metricName;
}
public void setMetricName(String metricName) {
this.metricName = metricName;
}
}

@ -49,24 +49,19 @@ class CacheMetricsRegistrarConfiguration {
private final Collection<CacheMeterBinderProvider<?>> binderProviders; private final Collection<CacheMeterBinderProvider<?>> binderProviders;
private final CacheMetricsProperties properties;
private final Map<String, CacheManager> cacheManagers; private final Map<String, CacheManager> cacheManagers;
CacheMetricsRegistrarConfiguration(MeterRegistry registry, CacheMetricsRegistrarConfiguration(MeterRegistry registry,
CacheMetricsProperties properties,
Collection<CacheMeterBinderProvider<?>> binderProviders, Collection<CacheMeterBinderProvider<?>> binderProviders,
Map<String, CacheManager> cacheManagers) { Map<String, CacheManager> cacheManagers) {
this.registry = registry; this.registry = registry;
this.properties = properties;
this.binderProviders = binderProviders; this.binderProviders = binderProviders;
this.cacheManagers = cacheManagers; this.cacheManagers = cacheManagers;
} }
@Bean @Bean
public CacheMetricsRegistrar cacheMetricsRegistrar() { public CacheMetricsRegistrar cacheMetricsRegistrar() {
return new CacheMetricsRegistrar(this.registry, this.properties.getMetricName(), return new CacheMetricsRegistrar(this.registry, this.binderProviders);
this.binderProviders);
} }
@PostConstruct @PostConstruct

@ -44,21 +44,9 @@ 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);
registry.get("cache.requests").tags("name", "cache1") registry.get("cache.gets").tags("name", "cache1")
.tags("cacheManager", "cacheManager").meter(); .tags("cacheManager", "cacheManager").meter();
registry.get("cache.requests").tags("name", "cache2") registry.get("cache.gets").tags("name", "cache2")
.tags("cacheManager", "cacheManager").meter();
});
}
@Test
public void autoConfiguredCacheManagerWithCustomMetricName() {
this.contextRunner
.withPropertyValues("management.metrics.cache.metric-name=custom.name",
"spring.cache.type=caffeine", "spring.cache.cache-names=cache1")
.run((context) -> {
MeterRegistry registry = context.getBean(MeterRegistry.class);
registry.get("custom.name.requests").tags("name", "cache1")
.tags("cacheManager", "cacheManager").meter(); .tags("cacheManager", "cacheManager").meter();
}); });
} }
@ -68,9 +56,9 @@ public class CacheMetricsConfigurationTests {
this.contextRunner.withPropertyValues("spring.cache.type=simple", this.contextRunner.withPropertyValues("spring.cache.type=simple",
"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.gets").tags("name", "cache1")
.tags("cacheManager", "cacheManager").meter()).isNull(); .tags("cacheManager", "cacheManager").meter()).isNull();
assertThat(registry.find("cache.requests").tags("name", "cache2") assertThat(registry.find("cache.gets").tags("name", "cache2")
.tags("cacheManager", "cacheManager").meter()).isNull(); .tags("cacheManager", "cacheManager").meter()).isNull();
}); });
} }

@ -35,10 +35,9 @@ public interface CacheMeterBinderProvider<C extends Cache> {
* Return the {@link MeterBinder} managing the specified {@link Cache} or {@code null} * Return the {@link MeterBinder} managing the specified {@link Cache} or {@code null}
* if the specified {@link Cache} is not supported. * if the specified {@link Cache} is not supported.
* @param cache the cache to instrument * @param cache the cache to instrument
* @param name the name prefix of the metrics
* @param tags tags to apply to all recorded metrics * @param tags tags to apply to all recorded metrics
* @return a {@link MeterBinder} handling the specified {@link Cache} or {@code null} * @return a {@link MeterBinder} handling the specified {@link Cache} or {@code null}
*/ */
MeterBinder getMeterBinder(C cache, String name, Iterable<Tag> tags); MeterBinder getMeterBinder(C cache, Iterable<Tag> tags);
} }

@ -37,21 +37,17 @@ public class CacheMetricsRegistrar {
private final MeterRegistry registry; private final MeterRegistry registry;
private final String metricName;
private final Collection<CacheMeterBinderProvider<?>> binderProviders; private final Collection<CacheMeterBinderProvider<?>> binderProviders;
/** /**
* Creates a new registrar. * Creates a new registrar.
* @param registry the {@link MeterRegistry} to use * @param registry the {@link MeterRegistry} to use
* @param metricName the name of the metric
* @param binderProviders the {@link CacheMeterBinderProvider} instances that should * @param binderProviders the {@link CacheMeterBinderProvider} instances that should
* be used to detect compatible caches * be used to detect compatible caches
*/ */
public CacheMetricsRegistrar(MeterRegistry registry, String metricName, public CacheMetricsRegistrar(MeterRegistry registry,
Collection<CacheMeterBinderProvider<?>> binderProviders) { Collection<CacheMeterBinderProvider<?>> binderProviders) {
this.registry = registry; this.registry = registry;
this.metricName = metricName;
this.binderProviders = binderProviders; this.binderProviders = binderProviders;
} }
@ -78,7 +74,7 @@ public class CacheMetricsRegistrar {
.callbacks(CacheMeterBinderProvider.class, this.binderProviders, cache) .callbacks(CacheMeterBinderProvider.class, this.binderProviders, cache)
.withLogger(CacheMetricsRegistrar.class) .withLogger(CacheMetricsRegistrar.class)
.invokeAnd((binderProvider) -> binderProvider.getMeterBinder(cache, .invokeAnd((binderProvider) -> binderProvider.getMeterBinder(cache,
this.metricName, cacheTags)) cacheTags))
.filter(Objects::nonNull).findFirst().orElse(null); .filter(Objects::nonNull).findFirst().orElse(null);
} }

@ -32,9 +32,8 @@ public class CaffeineCacheMeterBinderProvider
implements CacheMeterBinderProvider<CaffeineCache> { implements CacheMeterBinderProvider<CaffeineCache> {
@Override @Override
public MeterBinder getMeterBinder(CaffeineCache cache, String name, public MeterBinder getMeterBinder(CaffeineCache cache, Iterable<Tag> tags) {
Iterable<Tag> tags) { return new CaffeineCacheMetrics(cache.getNativeCache(), cache.getName(), tags);
return new CaffeineCacheMetrics(cache.getNativeCache(), name, tags);
} }
} }

@ -32,9 +32,8 @@ public class EhCache2CacheMeterBinderProvider
implements CacheMeterBinderProvider<EhCacheCache> { implements CacheMeterBinderProvider<EhCacheCache> {
@Override @Override
public MeterBinder getMeterBinder(EhCacheCache cache, String name, public MeterBinder getMeterBinder(EhCacheCache cache, Iterable<Tag> tags) {
Iterable<Tag> tags) { return new EhCache2Metrics(cache.getNativeCache(), tags);
return new EhCache2Metrics(cache.getNativeCache(), name, tags);
} }
} }

@ -33,10 +33,9 @@ public class HazelcastCacheMeterBinderProvider
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public MeterBinder getMeterBinder(HazelcastCache cache, String name, public MeterBinder getMeterBinder(HazelcastCache cache, Iterable<Tag> tags) {
Iterable<Tag> tags) {
return new HazelcastCacheMetrics((IMap<Object, Object>) cache.getNativeCache(), return new HazelcastCacheMetrics((IMap<Object, Object>) cache.getNativeCache(),
name, tags); tags);
} }
} }

@ -32,9 +32,8 @@ public class JCacheCacheMeterBinderProvider
implements CacheMeterBinderProvider<JCacheCache> { implements CacheMeterBinderProvider<JCacheCache> {
@Override @Override
public MeterBinder getMeterBinder(JCacheCache cache, String name, public MeterBinder getMeterBinder(JCacheCache cache, Iterable<Tag> tags) {
Iterable<Tag> tags) { return new JCacheMetrics(cache.getNativeCache(), tags);
return new JCacheMetrics(cache.getNativeCache(), name, tags);
} }
} }

@ -39,20 +39,20 @@ public class CacheMetricsRegistrarTests {
@Test @Test
public void bindToSupportedCache() { public void bindToSupportedCache() {
CacheMetricsRegistrar registrar = new CacheMetricsRegistrar(this.meterRegistry, CacheMetricsRegistrar registrar = new CacheMetricsRegistrar(this.meterRegistry,
"root", Collections.singleton(new CaffeineCacheMeterBinderProvider())); 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.get("root.requests").tags("name", "test").meter()) assertThat(this.meterRegistry.get("cache.gets").tags("name", "test").meter())
.isNotNull(); .isNotNull();
} }
@Test @Test
public void bindToUnsupportedCache() { public void bindToUnsupportedCache() {
CacheMetricsRegistrar registrar = new CacheMetricsRegistrar(this.meterRegistry, CacheMetricsRegistrar registrar = new CacheMetricsRegistrar(this.meterRegistry,
"root", Collections.emptyList()); Collections.emptyList());
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("cache.gets").tags("name", "test").meter())
.isNull(); .isNull();
} }

@ -38,7 +38,7 @@ public class CaffeineCacheMeterBinderProviderTests {
public void caffeineCacheProvider() { public void caffeineCacheProvider() {
CaffeineCache cache = new CaffeineCache("test", Caffeine.newBuilder().build()); CaffeineCache cache = new CaffeineCache("test", Caffeine.newBuilder().build());
MeterBinder meterBinder = new CaffeineCacheMeterBinderProvider() MeterBinder meterBinder = new CaffeineCacheMeterBinderProvider()
.getMeterBinder(cache, "test", Collections.emptyList()); .getMeterBinder(cache, Collections.emptyList());
assertThat(meterBinder).isInstanceOf(CaffeineCacheMetrics.class); assertThat(meterBinder).isInstanceOf(CaffeineCacheMetrics.class);
} }

@ -47,7 +47,7 @@ public class EhCache2CacheMeterBinderProviderTests {
cacheManager.addCache(nativeCache); cacheManager.addCache(nativeCache);
EhCacheCache cache = new EhCacheCache(nativeCache); EhCacheCache cache = new EhCacheCache(nativeCache);
MeterBinder meterBinder = new EhCache2CacheMeterBinderProvider() MeterBinder meterBinder = new EhCache2CacheMeterBinderProvider()
.getMeterBinder(cache, "test", Collections.emptyList()); .getMeterBinder(cache, Collections.emptyList());
assertThat(meterBinder).isInstanceOf(EhCache2Metrics.class); assertThat(meterBinder).isInstanceOf(EhCache2Metrics.class);
} }
finally { finally {

@ -24,10 +24,11 @@ import io.micrometer.core.instrument.binder.MeterBinder;
import io.micrometer.core.instrument.binder.cache.HazelcastCacheMetrics; import io.micrometer.core.instrument.binder.cache.HazelcastCacheMetrics;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/** /**
* Tests for {@link HazelcastCacheMeterBinderProvider}. * Tests for {@link HazelcastCacheMeterBinderProvider}.
@ -37,14 +38,14 @@ import static org.assertj.core.api.Assertions.assertThat;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class HazelcastCacheMeterBinderProviderTests { public class HazelcastCacheMeterBinderProviderTests {
@Mock @SuppressWarnings("unchecked")
private IMap<Object, Object> nativeCache;
@Test @Test
public void hazelcastCacheProvider() { public void hazelcastCacheProvider() {
HazelcastCache cache = new HazelcastCache(this.nativeCache); IMap<Object, Object> nativeCache = mock(IMap.class);
given(nativeCache.getName()).willReturn("test");
HazelcastCache cache = new HazelcastCache(nativeCache);
MeterBinder meterBinder = new HazelcastCacheMeterBinderProvider() MeterBinder meterBinder = new HazelcastCacheMeterBinderProvider()
.getMeterBinder(cache, "test", Collections.emptyList()); .getMeterBinder(cache, Collections.emptyList());
assertThat(meterBinder).isInstanceOf(HazelcastCacheMetrics.class); assertThat(meterBinder).isInstanceOf(HazelcastCacheMetrics.class);
} }

@ -52,7 +52,7 @@ public class JCacheCacheMeterBinderProviderTests {
given(this.nativeCache.getName()).willReturn("test"); given(this.nativeCache.getName()).willReturn("test");
JCacheCache cache = new JCacheCache(this.nativeCache); JCacheCache cache = new JCacheCache(this.nativeCache);
MeterBinder meterBinder = new JCacheCacheMeterBinderProvider() MeterBinder meterBinder = new JCacheCacheMeterBinderProvider()
.getMeterBinder(cache, "test", Collections.emptyList()); .getMeterBinder(cache, Collections.emptyList());
assertThat(meterBinder).isInstanceOf(JCacheMetrics.class); assertThat(meterBinder).isInstanceOf(JCacheMetrics.class);
} }

@ -1297,7 +1297,6 @@ content into your application. Rather, pick only the properties that you need.
management.metrics.binders.logback.enabled=true # Whether to enable Logback metrics. management.metrics.binders.logback.enabled=true # Whether to enable Logback metrics.
management.metrics.binders.processor.enabled=true # Whether to enable processor metrics. management.metrics.binders.processor.enabled=true # Whether to enable processor metrics.
management.metrics.binders.uptime.enabled=true # Whether to enable uptime metrics. management.metrics.binders.uptime.enabled=true # Whether to enable uptime metrics.
management.metrics.cache.metric-name=cache # Name of the metric for cache usage.
management.metrics.cache.instrument=true # Instrument all available caches. management.metrics.cache.instrument=true # Instrument all available caches.
management.metrics.export.atlas.batch-size= # Number of measurements per request to use for the backend. If more measurements are found, then multiple requests will be made. management.metrics.export.atlas.batch-size= # Number of measurements per request to use for the backend. If more measurements are found, then multiple requests will be made.
management.metrics.export.atlas.config-refresh-frequency= # Frequency for refreshing config settings from the LWC service. management.metrics.export.atlas.config-refresh-frequency= # Frequency for refreshing config settings from the LWC service.

@ -1194,10 +1194,9 @@ following information:
[[production-ready-metrics-cache]] [[production-ready-metrics-cache]]
=== Cache metrics === Cache metrics
Auto-configuration will enable the instrumentation of all available ``Cache``s on startup Auto-configuration will enable the instrumentation of all available ``Cache``s on startup
with a metric named `cache`. The prefix can be customized by using the with metrics prefixed with `cache.`. Cache instrumentation is standardized for a basic set
`management.metrics.cache.metric-name` property. Cache instrumentation is specific of metrics. Additional, cache-specific metrics are also available. Please refer to
to each cache library, refer to https://micrometer.io/docs[the micrometer documentation] https://micrometer.io/docs[the Micrometer documentation] for more details.
for more details.
The following cache libraries are supported: The following cache libraries are supported:
@ -1206,8 +1205,8 @@ The following cache libraries are supported:
* Hazelcast * Hazelcast
* Any compliant JCache (JSR-107) implementation * Any compliant JCache (JSR-107) implementation
Metrics will also be tagged by the name of the `CacheManager` computed based on the bean Metrics are tagged by the name of the cache and by the name of the `CacheManager` that is
name. derived from the bean name.
NOTE: Only caches that are available on startup are bound to the registry. For caches NOTE: Only caches that are available on startup are bound to the registry. For caches
created on-the-fly or programmatically after the startup phase, an explicit registration created on-the-fly or programmatically after the startup phase, an explicit registration

Loading…
Cancel
Save