Polish "Align with Micrometer's standardized cache metrics"

Closes gh-11918
pull/11922/head
Andy Wilkinson 7 years ago
parent ba52aa3674
commit 7d561f5ed0

@ -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.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Configuration;
@ -33,7 +32,6 @@ import org.springframework.context.annotation.Import;
@Configuration
@ConditionalOnBean(CacheManager.class)
@ConditionalOnProperty(value = "management.metrics.cache.instrument", matchIfMissing = true)
@EnableConfigurationProperties(CacheMetricsProperties.class)
@Import({ CacheMeterBinderProvidersConfiguration.class,
CacheMetricsRegistrarConfiguration.class })
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 CacheMetricsProperties properties;
private final Map<String, CacheManager> cacheManagers;
CacheMetricsRegistrarConfiguration(MeterRegistry registry,
CacheMetricsProperties properties,
Collection<CacheMeterBinderProvider<?>> binderProviders,
Map<String, CacheManager> cacheManagers) {
this.registry = registry;
this.properties = properties;
this.binderProviders = binderProviders;
this.cacheManagers = cacheManagers;
}
@Bean
public CacheMetricsRegistrar cacheMetricsRegistrar() {
return new CacheMetricsRegistrar(this.registry, this.properties.getMetricName(),
this.binderProviders);
return new CacheMetricsRegistrar(this.registry, this.binderProviders);
}
@PostConstruct

@ -44,21 +44,9 @@ public class CacheMetricsConfigurationTests {
this.contextRunner.withPropertyValues("spring.cache.type=caffeine",
"spring.cache.cache-names=cache1,cache2").run((context) -> {
MeterRegistry registry = context.getBean(MeterRegistry.class);
registry.get("cache.requests").tags("name", "cache1")
registry.get("cache.gets").tags("name", "cache1")
.tags("cacheManager", "cacheManager").meter();
registry.get("cache.requests").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")
registry.get("cache.gets").tags("name", "cache2")
.tags("cacheManager", "cacheManager").meter();
});
}
@ -68,9 +56,9 @@ public class CacheMetricsConfigurationTests {
this.contextRunner.withPropertyValues("spring.cache.type=simple",
"spring.cache.cache-names=cache1,cache2").run((context) -> {
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();
assertThat(registry.find("cache.requests").tags("name", "cache2")
assertThat(registry.find("cache.gets").tags("name", "cache2")
.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}
* if the specified {@link Cache} is not supported.
* @param cache the cache to instrument
* @param name the name prefix of the metrics
* @param tags tags to apply to all recorded metrics
* @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 String metricName;
private final Collection<CacheMeterBinderProvider<?>> binderProviders;
/**
* Creates a new registrar.
* @param registry the {@link MeterRegistry} to use
* @param metricName the name of the metric
* @param binderProviders the {@link CacheMeterBinderProvider} instances that should
* be used to detect compatible caches
*/
public CacheMetricsRegistrar(MeterRegistry registry, String metricName,
public CacheMetricsRegistrar(MeterRegistry registry,
Collection<CacheMeterBinderProvider<?>> binderProviders) {
this.registry = registry;
this.metricName = metricName;
this.binderProviders = binderProviders;
}
@ -78,7 +74,7 @@ public class CacheMetricsRegistrar {
.callbacks(CacheMeterBinderProvider.class, this.binderProviders, cache)
.withLogger(CacheMetricsRegistrar.class)
.invokeAnd((binderProvider) -> binderProvider.getMeterBinder(cache,
this.metricName, cacheTags))
cacheTags))
.filter(Objects::nonNull).findFirst().orElse(null);
}

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

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

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

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

@ -39,7 +39,7 @@ public class CacheMetricsRegistrarTests {
@Test
public void bindToSupportedCache() {
CacheMetricsRegistrar registrar = new CacheMetricsRegistrar(this.meterRegistry,
"root", Collections.singleton(new CaffeineCacheMeterBinderProvider()));
Collections.singleton(new CaffeineCacheMeterBinderProvider()));
assertThat(registrar.bindCacheToRegistry(
new CaffeineCache("test", Caffeine.newBuilder().build()))).isTrue();
assertThat(this.meterRegistry.get("cache.gets").tags("name", "test").meter())
@ -49,7 +49,7 @@ public class CacheMetricsRegistrarTests {
@Test
public void bindToUnsupportedCache() {
CacheMetricsRegistrar registrar = new CacheMetricsRegistrar(this.meterRegistry,
"root", Collections.emptyList());
Collections.emptyList());
assertThat(registrar.bindCacheToRegistry(
new CaffeineCache("test", Caffeine.newBuilder().build()))).isFalse();
assertThat(this.meterRegistry.find("cache.gets").tags("name", "test").meter())

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

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

@ -24,10 +24,11 @@ import io.micrometer.core.instrument.binder.MeterBinder;
import io.micrometer.core.instrument.binder.cache.HazelcastCacheMetrics;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link HazelcastCacheMeterBinderProvider}.
@ -40,16 +41,11 @@ public class HazelcastCacheMeterBinderProviderTests {
@SuppressWarnings("unchecked")
@Test
public void hazelcastCacheProvider() {
IMap<Object, Object> nativeCache = Mockito.mock(IMap.class);
// It is not possible to create a real Hazelcast cache with a null name,
// so Micrometer's Hazelcast binder uses the name from the cache for its tag value.
Mockito.when(nativeCache.getName()).thenReturn("test");
IMap<Object, Object> nativeCache = mock(IMap.class);
given(nativeCache.getName()).willReturn("test");
HazelcastCache cache = new HazelcastCache(nativeCache);
MeterBinder meterBinder = new HazelcastCacheMeterBinderProvider()
.getMeterBinder(cache, "test", Collections.emptyList());
.getMeterBinder(cache, Collections.emptyList());
assertThat(meterBinder).isInstanceOf(HazelcastCacheMetrics.class);
}

@ -52,7 +52,7 @@ public class JCacheCacheMeterBinderProviderTests {
given(this.nativeCache.getName()).willReturn("test");
JCacheCache cache = new JCacheCache(this.nativeCache);
MeterBinder meterBinder = new JCacheCacheMeterBinderProvider()
.getMeterBinder(cache, "test", Collections.emptyList());
.getMeterBinder(cache, Collections.emptyList());
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.processor.enabled=true # Whether to enable processor 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.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.

@ -1194,10 +1194,9 @@ following information:
[[production-ready-metrics-cache]]
=== Cache metrics
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
`management.metrics.cache.metric-name` property. Cache instrumentation is specific
to each cache library, refer to https://micrometer.io/docs[the micrometer documentation]
for more details.
with metrics prefixed with `cache.`. Cache instrumentation is standardized for a basic set
of metrics. Additional, cache-specific metrics are also available. Please refer to
https://micrometer.io/docs[the Micrometer documentation] for more details.
The following cache libraries are supported:
@ -1206,8 +1205,8 @@ The following cache libraries are supported:
* Hazelcast
* Any compliant JCache (JSR-107) implementation
Metrics will also be tagged by the name of the `CacheManager` computed based on the bean
name.
Metrics are tagged by the name of the cache and by the name of the `CacheManager` that is
derived from the bean name.
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

Loading…
Cancel
Save