Polish "Align with Micrometer's standardized cache metrics"

Closes gh-11918
This commit is contained in:
Andy Wilkinson
2018-02-06 11:13:37 +00:00
parent ba52aa3674
commit 7d561f5ed0
17 changed files with 28 additions and 105 deletions

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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())

View File

@@ -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);
}

View File

@@ -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 {

View File

@@ -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);
}

View File

@@ -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);
}