Commit 6bac365a authored by Phillip Webb's avatar Phillip Webb

Polish micrometer cache metrics

Change `CacheMeterBinderProvider` to include a cache type
generic which helps to simplify implementations. Also move
and extract a few classes.

See gh-11221
parent 37fe7bc8
......@@ -22,7 +22,10 @@ import io.micrometer.core.instrument.binder.MeterBinder;
import net.sf.ehcache.Ehcache;
import org.springframework.boot.actuate.metrics.cache.CacheMeterBinderProvider;
import org.springframework.boot.actuate.metrics.cache.CacheMeterBinderProviders;
import org.springframework.boot.actuate.metrics.cache.CaffeineCacheMeterBinderProvider;
import org.springframework.boot.actuate.metrics.cache.EhCache2CacheMeterBinderProvider;
import org.springframework.boot.actuate.metrics.cache.HazelcastCacheMeterBinderProvider;
import org.springframework.boot.actuate.metrics.cache.JCacheCacheMeterBinderProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.cache.ehcache.EhCacheCache;
......@@ -35,19 +38,19 @@ import org.springframework.context.annotation.Configuration;
*
* @author Stephane Nicoll
* @since 2.0.0
* @see CacheMeterBinderProviders
*/
@Configuration
@ConditionalOnClass(MeterBinder.class)
class CacheMeterBinderProvidersConfiguration {
@Configuration
@ConditionalOnClass({ CaffeineCache.class, com.github.benmanes.caffeine.cache.Cache.class })
@ConditionalOnClass({ CaffeineCache.class,
com.github.benmanes.caffeine.cache.Cache.class })
static class CaffeineCacheMeterBinderProviderConfiguration {
@Bean
public CacheMeterBinderProvider caffeineCacheMeterBinderProvider() {
return new CacheMeterBinderProviders.CaffeineCacheMeterBinderProvider();
public CaffeineCacheMeterBinderProvider caffeineCacheMeterBinderProvider() {
return new CaffeineCacheMeterBinderProvider();
}
}
......@@ -57,8 +60,8 @@ class CacheMeterBinderProvidersConfiguration {
static class EhCache2CacheMeterBinderProviderConfiguration {
@Bean
public CacheMeterBinderProvider ehCache2CacheMeterBinderProvider() {
return new CacheMeterBinderProviders.EhCache2CacheMeterBinderProvider();
public EhCache2CacheMeterBinderProvider ehCache2CacheMeterBinderProvider() {
return new EhCache2CacheMeterBinderProvider();
}
}
......@@ -68,8 +71,8 @@ class CacheMeterBinderProvidersConfiguration {
static class HazelcastCacheMeterBinderProviderConfiguration {
@Bean
public CacheMeterBinderProvider hazelcastCacheMeterBinderProvider() {
return new CacheMeterBinderProviders.HazelcastCacheMeterBinderProvider();
public HazelcastCacheMeterBinderProvider hazelcastCacheMeterBinderProvider() {
return new HazelcastCacheMeterBinderProvider();
}
}
......@@ -79,8 +82,8 @@ class CacheMeterBinderProvidersConfiguration {
static class JCacheCacheMeterBinderProviderConfiguration {
@Bean
public CacheMeterBinderProvider jCacheCacheMeterBinderProvider() {
return new CacheMeterBinderProviders.JCacheCacheMeterBinderProvider();
public JCacheCacheMeterBinderProvider jCacheCacheMeterBinderProvider() {
return new JCacheCacheMeterBinderProvider();
}
}
......
......@@ -34,7 +34,8 @@ import org.springframework.context.annotation.Import;
@ConditionalOnBean(CacheManager.class)
@ConditionalOnProperty(value = "management.metrics.cache.instrument-cache", matchIfMissing = true)
@EnableConfigurationProperties(CacheMetricsProperties.class)
@Import({ CacheMeterBinderProvidersConfiguration.class, CacheMetricsRegistrarConfiguration.class })
@Import({ CacheMeterBinderProvidersConfiguration.class,
CacheMetricsRegistrarConfiguration.class })
public class CacheMetricsConfiguration {
}
......@@ -25,6 +25,7 @@ import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import org.springframework.boot.actuate.metrics.cache.CacheMeterBinderProvider;
import org.springframework.boot.actuate.metrics.cache.CacheMetricsRegistrar;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
......@@ -33,8 +34,8 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
/**
* Configure a {@link CacheMetricsRegistrar} and register all available
* {@link Cache caches}.
* Configure a {@link CacheMetricsRegistrar} and register all available {@link Cache
* caches}.
*
* @author Stephane Nicoll
* @since 2.0.0
......@@ -47,18 +48,18 @@ class CacheMetricsRegistrarConfiguration {
private final MeterRegistry registry;
private final CacheMetricsProperties properties;
private final Collection<CacheMeterBinderProvider<?>> binderProviders;
private final Collection<CacheMeterBinderProvider> cacheMeterBinderProviders;
private final CacheMetricsProperties properties;
private final Map<String, CacheManager> cacheManagers;
CacheMetricsRegistrarConfiguration(MeterRegistry registry,
CacheMetricsProperties properties,
Collection<CacheMeterBinderProvider> cacheMeterBinderProviders,
Collection<CacheMeterBinderProvider<?>> binderProviders,
Map<String, CacheManager> cacheManagers) {
this.registry = registry;
this.cacheMeterBinderProviders = cacheMeterBinderProviders;
this.binderProviders = binderProviders;
this.properties = properties;
this.cacheManagers = cacheManagers;
}
......@@ -66,14 +67,17 @@ class CacheMetricsRegistrarConfiguration {
@Bean
public CacheMetricsRegistrar cacheMetricsRegistrar() {
return new CacheMetricsRegistrar(this.registry,
this.properties.getCacheMetricName(), this.cacheMeterBinderProviders);
this.properties.getCacheMetricName(), this.binderProviders);
}
@PostConstruct
public void bindCachesToRegistry() {
this.cacheManagers.forEach((beanName, cacheManager) -> cacheManager.getCacheNames()
.forEach((cacheName) ->
bindCacheToRegistry(beanName, cacheManager.getCache(cacheName))));
this.cacheManagers.forEach(this::bindCacheManagerToRegistry);
}
private void bindCacheManagerToRegistry(String beanName, CacheManager cacheManager) {
cacheManager.getCacheNames().forEach((cacheName) -> bindCacheToRegistry(beanName,
cacheManager.getCache(cacheName)));
}
private void bindCacheToRegistry(String beanName, Cache cache) {
......
......@@ -45,17 +45,14 @@ public class CacheMetricsConfigurationTests {
@Test
public void autoConfiguredCacheManagerIsInstrumented() {
this.contextRunner
.withConfiguration(
AutoConfigurations.of(CacheAutoConfiguration.class))
.withConfiguration(AutoConfigurations.of(CacheAutoConfiguration.class))
.withPropertyValues("spring.cache.type=caffeine",
"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.requests").tags("name", "cache1")
.tags("cacheManager", "cacheManager").meter()).isPresent();
assertThat(registry.find("cache.requests")
.tags("name", "cache2")
assertThat(registry.find("cache.requests").tags("name", "cache2")
.tags("cacheManager", "cacheManager").meter()).isPresent();
});
}
......@@ -63,33 +60,30 @@ public class CacheMetricsConfigurationTests {
@Test
public void autoConfiguredCacheManagerWithCustomMetricName() {
this.contextRunner
.withConfiguration(
AutoConfigurations.of(CacheAutoConfiguration.class))
.withConfiguration(AutoConfigurations.of(CacheAutoConfiguration.class))
.withPropertyValues(
"management.metrics.cache.cache-metric-name=custom.name",
"spring.cache.type=caffeine", "spring.cache.cache-names=cache1")
.run((context) -> {
MeterRegistry registry = context.getBean(MeterRegistry.class);
assertThat(registry.find("custom.name.requests")
.tags("name", "cache1")
.tags("cacheManager", "cacheManager").meter()).isPresent();
assertThat(
registry.find("custom.name.requests").tags("name", "cache1")
.tags("cacheManager", "cacheManager").meter())
.isPresent();
});
}
@Test
public void autoConfiguredNonSupportedCacheManagerIsIgnored() {
this.contextRunner
.withConfiguration(
AutoConfigurations.of(CacheAutoConfiguration.class))
.withConfiguration(AutoConfigurations.of(CacheAutoConfiguration.class))
.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.requests").tags("name", "cache1")
.tags("cacheManager", "cacheManager").meter()).isNotPresent();
assertThat(registry.find("cache.requests")
.tags("name", "cache2")
assertThat(registry.find("cache.requests").tags("name", "cache2")
.tags("cacheManager", "cacheManager").meter()).isNotPresent();
});
}
......@@ -97,19 +91,16 @@ public class CacheMetricsConfigurationTests {
@Test
public void cacheInstrumentationCanBeDisabled() {
this.contextRunner
.withConfiguration(
AutoConfigurations.of(CacheAutoConfiguration.class))
.withConfiguration(AutoConfigurations.of(CacheAutoConfiguration.class))
.withPropertyValues("management.metrics.cache.instrument-cache=false",
"spring.cache.type=caffeine", "spring.cache.cache-names=cache1")
.run((context) -> {
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();
});
}
@Configuration
@EnableCaching
static class RegistryConfiguration {
......
......@@ -24,10 +24,11 @@ import org.springframework.cache.Cache;
/**
* Provide a {@link MeterBinder} based on a {@link Cache}.
*
* @param <C> The cache type
* @author Stephane Nicoll
* @since 2.0.0
*/
public interface CacheMeterBinderProvider {
public interface CacheMeterBinderProvider<C extends Cache> {
/**
* Return the {@link MeterBinder} managing the specified {@link Cache} or {@code null}
......@@ -37,6 +38,6 @@ public interface CacheMeterBinderProvider {
* @param tags tags to apply to all recorded metrics
* @return a {@link MeterBinder} handling the specified {@link Cache} or {@code null}
*/
MeterBinder getMeterBinder(Cache cache, String name, Iterable<Tag> tags);
MeterBinder getMeterBinder(C cache, String name, Iterable<Tag> tags);
}
......@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.actuate.autoconfigure.metrics.cache;
package org.springframework.boot.actuate.metrics.cache;
import java.util.ArrayList;
import java.util.Arrays;
......@@ -25,9 +25,11 @@ import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.binder.MeterBinder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.actuate.metrics.cache.CacheMeterBinderProvider;
import org.springframework.cache.Cache;
import org.springframework.core.ResolvableType;
/**
* Register supported {@link Cache} to a {@link MeterRegistry}.
......@@ -41,7 +43,7 @@ public class CacheMetricsRegistrar {
private final String metricName;
private final Collection<CacheMeterBinderProvider> cacheMeterBinderProviders;
private final Collection<CacheMeterBinderProvider<?>> binderProviders;
/**
* Creates a new registrar.
......@@ -51,21 +53,21 @@ public class CacheMetricsRegistrar {
* be used to detect compatible caches
*/
public CacheMetricsRegistrar(MeterRegistry registry, String metricName,
Collection<CacheMeterBinderProvider> binderProviders) {
Collection<CacheMeterBinderProvider<?>> binderProviders) {
this.registry = registry;
this.metricName = metricName;
this.cacheMeterBinderProviders = binderProviders;
this.binderProviders = binderProviders;
}
/**
* Attempt to bind the specified {@link Cache} to the registry. Return {@code true}
* if the cache is supported and was bound to the registry, {@code false} otherwise.
* Attempt to bind the specified {@link Cache} to the registry. Return {@code true} if
* the cache is supported and was bound to the registry, {@code false} otherwise.
* @param cache the cache to handle
* @param tags the tags to associate with the metrics of that cache
* @return {@code true} if the {@code cache} is supported and was registered
*/
public boolean bindCacheToRegistry(Cache cache, Tag... tags) {
List<Tag> allTags = new ArrayList(Arrays.asList(tags));
List<Tag> allTags = new ArrayList<>(Arrays.asList(tags));
MeterBinder meterBinder = getMeterBinder(cache, allTags);
if (meterBinder != null) {
meterBinder.bindTo(this.registry);
......@@ -74,14 +76,40 @@ public class CacheMetricsRegistrar {
return false;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private MeterBinder getMeterBinder(Cache cache, List<Tag> tags) {
tags.addAll(getAdditionalTags(cache));
for (CacheMeterBinderProvider binderProvider : this.cacheMeterBinderProviders) {
MeterBinder meterBinder = binderProvider.getMeterBinder(cache,
this.metricName, tags);
if (meterBinder != null) {
return meterBinder;
for (CacheMeterBinderProvider<?> binderProvider : this.binderProviders) {
Class<?> cacheType = ResolvableType
.forClass(CacheMeterBinderProvider.class, binderProvider.getClass())
.resolveGeneric();
if (cacheType.isInstance(cache)) {
try {
MeterBinder meterBinder = ((CacheMeterBinderProvider) binderProvider)
.getMeterBinder(cache, this.metricName, tags);
if (meterBinder != null) {
return meterBinder;
}
}
catch (ClassCastException ex) {
String msg = ex.getMessage();
if (msg == null || msg.startsWith(cache.getClass().getName())) {
// Possibly a lambda-defined listener which we could not resolve
// the generic event type for
Log logger = LogFactory.getLog(getClass());
if (logger.isDebugEnabled()) {
logger.debug(
"Non-matching event type for CacheMeterBinderProvider: "
+ binderProvider,
ex);
}
}
else {
throw ex;
}
}
}
}
return null;
}
......
/*
* 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.metrics.cache;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.binder.MeterBinder;
import io.micrometer.core.instrument.binder.cache.CaffeineCacheMetrics;
import org.springframework.cache.caffeine.CaffeineCache;
/**
* {@link CacheMeterBinderProvider} implementation for Caffeine.
*
* @author Stephane Nicoll
* @since 2.0.0
*/
public class CaffeineCacheMeterBinderProvider
implements CacheMeterBinderProvider<CaffeineCache> {
@Override
public MeterBinder getMeterBinder(CaffeineCache cache, String name,
Iterable<Tag> tags) {
return new CaffeineCacheMetrics(cache.getNativeCache(), tags, name);
}
}
/*
* 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.metrics.cache;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.binder.MeterBinder;
import io.micrometer.core.instrument.binder.cache.EhCache2Metrics;
import org.springframework.cache.ehcache.EhCacheCache;
/**
* {@link CacheMeterBinderProvider} implementation for EhCache2.
*
* @author Stephane Nicoll
* @since 2.0.0
*/
public class EhCache2CacheMeterBinderProvider
implements CacheMeterBinderProvider<EhCacheCache> {
@Override
public MeterBinder getMeterBinder(EhCacheCache cache, String name,
Iterable<Tag> tags) {
return new EhCache2Metrics(cache.getNativeCache(), name, tags);
}
}
......@@ -20,85 +20,23 @@ import com.hazelcast.core.IMap;
import com.hazelcast.spring.cache.HazelcastCache;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.binder.MeterBinder;
import io.micrometer.core.instrument.binder.cache.CaffeineCacheMetrics;
import io.micrometer.core.instrument.binder.cache.EhCache2Metrics;
import io.micrometer.core.instrument.binder.cache.HazelcastCacheMetrics;
import io.micrometer.core.instrument.binder.cache.JCacheMetrics;
import org.springframework.cache.Cache;
import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.cache.ehcache.EhCacheCache;
import org.springframework.cache.jcache.JCacheCache;
/**
* Common {@link CacheMeterBinderProvider} implementations.
* {@link CacheMeterBinderProvider} implementation for Hazelcast.
*
* @author Stephane Nicoll
* @since 2.0.0
*/
public abstract class CacheMeterBinderProviders {
/**
* {@link CacheMeterBinderProvider} implementation for Caffeine.
*/
public static class CaffeineCacheMeterBinderProvider
implements CacheMeterBinderProvider {
@Override
public MeterBinder getMeterBinder(Cache cache, String name, Iterable<Tag> tags) {
if (cache instanceof CaffeineCache) {
return new CaffeineCacheMetrics(
((CaffeineCache) cache).getNativeCache(), tags, name);
}
return null;
}
}
/**
* {@link CacheMeterBinderProvider} implementation for EhCache2.
*/
public static class EhCache2CacheMeterBinderProvider
implements CacheMeterBinderProvider {
@Override
public MeterBinder getMeterBinder(Cache cache, String name, Iterable<Tag> tags) {
if (cache instanceof EhCacheCache) {
return new EhCache2Metrics(((EhCacheCache) cache).getNativeCache(),
name, tags);
}
return null;
}
}
/**
* {@link CacheMeterBinderProvider} implementation for Hazelcast.
*/
public static class HazelcastCacheMeterBinderProvider
implements CacheMeterBinderProvider {
@Override
public MeterBinder getMeterBinder(Cache cache, String name, Iterable<Tag> tags) {
if (cache instanceof HazelcastCache) {
IMap<Object, Object> nativeCache = (IMap<Object, Object>) ((HazelcastCache) cache).getNativeCache();
return new HazelcastCacheMetrics(nativeCache, name, tags);
}
return null;
}
}
/**
* {@link CacheMeterBinderProvider} implementation for JCache.
*/
public static class JCacheCacheMeterBinderProvider
implements CacheMeterBinderProvider {
@Override
public MeterBinder getMeterBinder(Cache cache, String name, Iterable<Tag> tags) {
if (cache instanceof JCacheCache) {
return new JCacheMetrics(((JCacheCache) cache).getNativeCache(),
name, tags);
}
return null;
}
public class HazelcastCacheMeterBinderProvider
implements CacheMeterBinderProvider<HazelcastCache> {
@Override
@SuppressWarnings("unchecked")
public MeterBinder getMeterBinder(HazelcastCache cache, String name,
Iterable<Tag> tags) {
return new HazelcastCacheMetrics((IMap<Object, Object>) cache.getNativeCache(),
name, tags);
}
}
/*
* 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.metrics.cache;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.binder.MeterBinder;
import io.micrometer.core.instrument.binder.cache.JCacheMetrics;
import org.springframework.cache.jcache.JCacheCache;
/**
* {@link CacheMeterBinderProvider} implementation for JCache.
*
* @author Stephane Nicoll
* @since 2.0.0
*/
public class JCacheCacheMeterBinderProvider
implements CacheMeterBinderProvider<JCacheCache> {
@Override
public MeterBinder getMeterBinder(JCacheCache cache, String name,
Iterable<Tag> tags) {
return new JCacheMetrics(cache.getNativeCache(), name, tags);
}
}
/*
* 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.metrics.cache;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.hazelcast.core.IMap;
import com.hazelcast.spring.cache.HazelcastCache;
import io.micrometer.core.instrument.binder.MeterBinder;
import io.micrometer.core.instrument.binder.cache.CaffeineCacheMetrics;
import io.micrometer.core.instrument.binder.cache.EhCache2Metrics;
import io.micrometer.core.instrument.binder.cache.HazelcastCacheMetrics;
import io.micrometer.core.instrument.binder.cache.JCacheMetrics;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.config.Configuration;
import org.junit.Test;
import org.springframework.boot.actuate.metrics.cache.CacheMeterBinderProviders.CaffeineCacheMeterBinderProvider;
import org.springframework.boot.actuate.metrics.cache.CacheMeterBinderProviders.EhCache2CacheMeterBinderProvider;
import org.springframework.boot.actuate.metrics.cache.CacheMeterBinderProviders.HazelcastCacheMeterBinderProvider;
import org.springframework.boot.actuate.metrics.cache.CacheMeterBinderProviders.JCacheCacheMeterBinderProvider;
import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.ehcache.EhCacheCache;
import org.springframework.cache.jcache.JCacheCache;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link CacheMeterBinderProviders}.
*
* @author Stephane Nicoll
*/
public class CacheMeterBinderProvidersTests {
@Test
public void caffeineCacheProvider() {
CaffeineCache cache = new CaffeineCache("test", Caffeine.newBuilder().build());
MeterBinder meterBinder = new CaffeineCacheMeterBinderProvider().getMeterBinder(
cache, "test", Collections.EMPTY_LIST);
assertThat(meterBinder).isInstanceOf(CaffeineCacheMetrics.class);
}
@Test
public void caffeineCacheProviderWithUnsupportedCache() {
MeterBinder meterBinder = new CaffeineCacheMeterBinderProvider().getMeterBinder(
new ConcurrentMapCache("test"), "test", Collections.EMPTY_LIST);
assertThat(meterBinder).isNull();
}
@Test
public void ehCache2CacheProvider() {
CacheManager cacheManager = new CacheManager(
new Configuration().name("EhCacheCacheTests").defaultCache(
new CacheConfiguration("default", 100)));
try {
Cache nativeCache = new Cache(
new CacheConfiguration("test", 100));
cacheManager.addCache(nativeCache);
EhCacheCache cache = new EhCacheCache(nativeCache);
MeterBinder meterBinder = new EhCache2CacheMeterBinderProvider().getMeterBinder(
cache, "test", Collections.EMPTY_LIST);
assertThat(meterBinder).isInstanceOf(EhCache2Metrics.class);
}
finally {
cacheManager.shutdown();
}
}
@Test
public void ehCache2CacheProviderWithUnsupportedCache() {
MeterBinder meterBinder = new EhCache2CacheMeterBinderProvider().getMeterBinder(
new ConcurrentMapCache("test"), "test", Collections.EMPTY_LIST);
assertThat(meterBinder).isNull();
}
@Test
public void hazelcastCacheProvider() {
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.EMPTY_LIST);
assertThat(meterBinder).isInstanceOf(HazelcastCacheMetrics.class);
}
@Test
public void hazelcastCacheProviderWithUnsupportedCache() {
MeterBinder meterBinder = new HazelcastCacheMeterBinderProvider().getMeterBinder(
new ConcurrentMapCache("test"), "test", Collections.EMPTY_LIST);
assertThat(meterBinder).isNull();
}
@Test
public void jCacheCacheProvider() throws URISyntaxException {
javax.cache.CacheManager cacheManager = mock(javax.cache.CacheManager.class);
given(cacheManager.getURI()).willReturn(new URI("/test"));
javax.cache.Cache<Object, Object> nativeCache = mock(javax.cache.Cache.class);
given(nativeCache.getCacheManager()).willReturn(cacheManager);
given(nativeCache.getName()).willReturn("test");
JCacheCache cache = new JCacheCache(nativeCache);
MeterBinder meterBinder = new JCacheCacheMeterBinderProvider().getMeterBinder(
cache, "test", Collections.EMPTY_LIST);
assertThat(meterBinder).isInstanceOf(JCacheMetrics.class);
}
@Test
public void jCacheCacheWithUnsupportedCache() {
MeterBinder meterBinder = new JCacheCacheMeterBinderProvider().getMeterBinder(
new ConcurrentMapCache("test"), "test", Collections.EMPTY_LIST);
assertThat(meterBinder).isNull();
}
}
......@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.actuate.autoconfigure.metrics.cache;
package org.springframework.boot.actuate.metrics.cache;
import java.util.Collections;
......@@ -23,7 +23,6 @@ import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.junit.Test;
import org.springframework.boot.actuate.metrics.cache.CacheMeterBinderProviders;
import org.springframework.cache.caffeine.CaffeineCache;
import static org.assertj.core.api.Assertions.assertThat;
......@@ -40,22 +39,21 @@ public class CacheMetricsRegistrarTests {
@Test
public void bindToSupportedCache() {
CacheMetricsRegistrar registrar = new CacheMetricsRegistrar(this.meterRegistry,
"root", Collections.singleton(
new CacheMeterBinderProviders.CaffeineCacheMeterBinderProvider()));
"root", Collections.singleton(new CaffeineCacheMeterBinderProvider()));
assertThat(registrar.bindCacheToRegistry(
new CaffeineCache("test", Caffeine.newBuilder().build()))).isTrue();
assertThat(this.meterRegistry.find("root.requests")
.tags("name", "test").meter()).isPresent();
assertThat(this.meterRegistry.find("root.requests").tags("name", "test").meter())
.isPresent();
}
@Test
public void bindToUnsupportedCache() {
CacheMetricsRegistrar registrar = new CacheMetricsRegistrar(this.meterRegistry,
"root", Collections.EMPTY_LIST);
"root", Collections.emptyList());
assertThat(registrar.bindCacheToRegistry(
new CaffeineCache("test", Caffeine.newBuilder().build()))).isFalse();
assertThat(this.meterRegistry.find("root.requests")
.tags("name", "test").meter()).isNotPresent();
assertThat(this.meterRegistry.find("root.requests").tags("name", "test").meter())
.isNotPresent();
}
}
/*
* Copyright 2012-2017 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.metrics.cache;
import java.util.Collections;
import com.github.benmanes.caffeine.cache.Caffeine;
import io.micrometer.core.instrument.binder.MeterBinder;
import io.micrometer.core.instrument.binder.cache.CaffeineCacheMetrics;
import org.junit.Test;
import org.springframework.cache.caffeine.CaffeineCache;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link CaffeineCacheMeterBinderProvider}.
*
* @author Stephane Nicoll
*/
public class CaffeineCacheMeterBinderProviderTests {
@Test
public void caffeineCacheProvider() {
CaffeineCache cache = new CaffeineCache("test", Caffeine.newBuilder().build());
MeterBinder meterBinder = new CaffeineCacheMeterBinderProvider()
.getMeterBinder(cache, "test", Collections.emptyList());
assertThat(meterBinder).isInstanceOf(CaffeineCacheMetrics.class);
}
}
/*
* Copyright 2012-2017 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.metrics.cache;
import java.util.Collections;
import io.micrometer.core.instrument.binder.MeterBinder;
import io.micrometer.core.instrument.binder.cache.EhCache2Metrics;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.config.Configuration;
import org.junit.Test;
import org.springframework.cache.ehcache.EhCacheCache;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link EhCache2CacheMeterBinderProvider}.
*
* @author Stephane Nicoll
*/
public class EhCache2CacheMeterBinderProviderTests {
@Test
public void ehCache2CacheProvider() {
CacheManager cacheManager = new CacheManager(
new Configuration().name("EhCacheCacheTests")
.defaultCache(new CacheConfiguration("default", 100)));
try {
Cache nativeCache = new Cache(new CacheConfiguration("test", 100));
cacheManager.addCache(nativeCache);
EhCacheCache cache = new EhCacheCache(nativeCache);
MeterBinder meterBinder = new EhCache2CacheMeterBinderProvider()
.getMeterBinder(cache, "test", Collections.emptyList());
assertThat(meterBinder).isInstanceOf(EhCache2Metrics.class);
}
finally {
cacheManager.shutdown();
}
}
}
/*
* Copyright 2012-2017 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.metrics.cache;
import java.util.Collections;
import com.hazelcast.core.IMap;
import com.hazelcast.spring.cache.HazelcastCache;
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.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link HazelcastCacheMeterBinderProvider}.
*
* @author Stephane Nicoll
*/
@RunWith(MockitoJUnitRunner.class)
public class HazelcastCacheMeterBinderProviderTests {
@Mock
private IMap<Object, Object> nativeCache;
@Test
public void hazelcastCacheProvider() {
HazelcastCache cache = new HazelcastCache(this.nativeCache);
MeterBinder meterBinder = new HazelcastCacheMeterBinderProvider()
.getMeterBinder(cache, "test", Collections.emptyList());
assertThat(meterBinder).isInstanceOf(HazelcastCacheMetrics.class);
}
}
/*
* Copyright 2012-2017 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.metrics.cache;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
import io.micrometer.core.instrument.binder.MeterBinder;
import io.micrometer.core.instrument.binder.cache.JCacheMetrics;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.cache.jcache.JCacheCache;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link JCacheCacheMeterBinderProvider}.
*
* @author Stephane Nicoll
*/
@RunWith(MockitoJUnitRunner.class)
public class JCacheCacheMeterBinderProviderTests {
@Mock
private javax.cache.Cache<Object, Object> nativeCache;
@Test
public void jCacheCacheProvider() throws URISyntaxException {
javax.cache.CacheManager cacheManager = mock(javax.cache.CacheManager.class);
given(cacheManager.getURI()).willReturn(new URI("/test"));
given(this.nativeCache.getCacheManager()).willReturn(cacheManager);
given(this.nativeCache.getName()).willReturn("test");
JCacheCache cache = new JCacheCache(this.nativeCache);
MeterBinder meterBinder = new JCacheCacheMeterBinderProvider()
.getMeterBinder(cache, "test", Collections.emptyList());
assertThat(meterBinder).isInstanceOf(JCacheMetrics.class);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment