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; ...@@ -22,7 +22,10 @@ import io.micrometer.core.instrument.binder.MeterBinder;
import net.sf.ehcache.Ehcache; import net.sf.ehcache.Ehcache;
import org.springframework.boot.actuate.metrics.cache.CacheMeterBinderProvider; 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.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cache.caffeine.CaffeineCache; import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.cache.ehcache.EhCacheCache; import org.springframework.cache.ehcache.EhCacheCache;
...@@ -35,19 +38,19 @@ import org.springframework.context.annotation.Configuration; ...@@ -35,19 +38,19 @@ import org.springframework.context.annotation.Configuration;
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 2.0.0 * @since 2.0.0
* @see CacheMeterBinderProviders
*/ */
@Configuration @Configuration
@ConditionalOnClass(MeterBinder.class) @ConditionalOnClass(MeterBinder.class)
class CacheMeterBinderProvidersConfiguration { class CacheMeterBinderProvidersConfiguration {
@Configuration @Configuration
@ConditionalOnClass({ CaffeineCache.class, com.github.benmanes.caffeine.cache.Cache.class }) @ConditionalOnClass({ CaffeineCache.class,
com.github.benmanes.caffeine.cache.Cache.class })
static class CaffeineCacheMeterBinderProviderConfiguration { static class CaffeineCacheMeterBinderProviderConfiguration {
@Bean @Bean
public CacheMeterBinderProvider caffeineCacheMeterBinderProvider() { public CaffeineCacheMeterBinderProvider caffeineCacheMeterBinderProvider() {
return new CacheMeterBinderProviders.CaffeineCacheMeterBinderProvider(); return new CaffeineCacheMeterBinderProvider();
} }
} }
...@@ -57,8 +60,8 @@ class CacheMeterBinderProvidersConfiguration { ...@@ -57,8 +60,8 @@ class CacheMeterBinderProvidersConfiguration {
static class EhCache2CacheMeterBinderProviderConfiguration { static class EhCache2CacheMeterBinderProviderConfiguration {
@Bean @Bean
public CacheMeterBinderProvider ehCache2CacheMeterBinderProvider() { public EhCache2CacheMeterBinderProvider ehCache2CacheMeterBinderProvider() {
return new CacheMeterBinderProviders.EhCache2CacheMeterBinderProvider(); return new EhCache2CacheMeterBinderProvider();
} }
} }
...@@ -68,8 +71,8 @@ class CacheMeterBinderProvidersConfiguration { ...@@ -68,8 +71,8 @@ class CacheMeterBinderProvidersConfiguration {
static class HazelcastCacheMeterBinderProviderConfiguration { static class HazelcastCacheMeterBinderProviderConfiguration {
@Bean @Bean
public CacheMeterBinderProvider hazelcastCacheMeterBinderProvider() { public HazelcastCacheMeterBinderProvider hazelcastCacheMeterBinderProvider() {
return new CacheMeterBinderProviders.HazelcastCacheMeterBinderProvider(); return new HazelcastCacheMeterBinderProvider();
} }
} }
...@@ -79,8 +82,8 @@ class CacheMeterBinderProvidersConfiguration { ...@@ -79,8 +82,8 @@ class CacheMeterBinderProvidersConfiguration {
static class JCacheCacheMeterBinderProviderConfiguration { static class JCacheCacheMeterBinderProviderConfiguration {
@Bean @Bean
public CacheMeterBinderProvider jCacheCacheMeterBinderProvider() { public JCacheCacheMeterBinderProvider jCacheCacheMeterBinderProvider() {
return new CacheMeterBinderProviders.JCacheCacheMeterBinderProvider(); return new JCacheCacheMeterBinderProvider();
} }
} }
......
...@@ -34,7 +34,8 @@ import org.springframework.context.annotation.Import; ...@@ -34,7 +34,8 @@ import org.springframework.context.annotation.Import;
@ConditionalOnBean(CacheManager.class) @ConditionalOnBean(CacheManager.class)
@ConditionalOnProperty(value = "management.metrics.cache.instrument-cache", matchIfMissing = true) @ConditionalOnProperty(value = "management.metrics.cache.instrument-cache", matchIfMissing = true)
@EnableConfigurationProperties(CacheMetricsProperties.class) @EnableConfigurationProperties(CacheMetricsProperties.class)
@Import({ CacheMeterBinderProvidersConfiguration.class, CacheMetricsRegistrarConfiguration.class }) @Import({ CacheMeterBinderProvidersConfiguration.class,
CacheMetricsRegistrarConfiguration.class })
public class CacheMetricsConfiguration { public class CacheMetricsConfiguration {
} }
...@@ -25,6 +25,7 @@ import io.micrometer.core.instrument.MeterRegistry; ...@@ -25,6 +25,7 @@ import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag; import io.micrometer.core.instrument.Tag;
import org.springframework.boot.actuate.metrics.cache.CacheMeterBinderProvider; 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.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.cache.Cache; import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager; import org.springframework.cache.CacheManager;
...@@ -33,8 +34,8 @@ import org.springframework.context.annotation.Configuration; ...@@ -33,8 +34,8 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
* Configure a {@link CacheMetricsRegistrar} and register all available * Configure a {@link CacheMetricsRegistrar} and register all available {@link Cache
* {@link Cache caches}. * caches}.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 2.0.0 * @since 2.0.0
...@@ -47,18 +48,18 @@ class CacheMetricsRegistrarConfiguration { ...@@ -47,18 +48,18 @@ class CacheMetricsRegistrarConfiguration {
private final MeterRegistry registry; 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; private final Map<String, CacheManager> cacheManagers;
CacheMetricsRegistrarConfiguration(MeterRegistry registry, CacheMetricsRegistrarConfiguration(MeterRegistry registry,
CacheMetricsProperties properties, CacheMetricsProperties properties,
Collection<CacheMeterBinderProvider> cacheMeterBinderProviders, Collection<CacheMeterBinderProvider<?>> binderProviders,
Map<String, CacheManager> cacheManagers) { Map<String, CacheManager> cacheManagers) {
this.registry = registry; this.registry = registry;
this.cacheMeterBinderProviders = cacheMeterBinderProviders; this.binderProviders = binderProviders;
this.properties = properties; this.properties = properties;
this.cacheManagers = cacheManagers; this.cacheManagers = cacheManagers;
} }
...@@ -66,14 +67,17 @@ class CacheMetricsRegistrarConfiguration { ...@@ -66,14 +67,17 @@ class CacheMetricsRegistrarConfiguration {
@Bean @Bean
public CacheMetricsRegistrar cacheMetricsRegistrar() { public CacheMetricsRegistrar cacheMetricsRegistrar() {
return new CacheMetricsRegistrar(this.registry, return new CacheMetricsRegistrar(this.registry,
this.properties.getCacheMetricName(), this.cacheMeterBinderProviders); this.properties.getCacheMetricName(), this.binderProviders);
} }
@PostConstruct @PostConstruct
public void bindCachesToRegistry() { public void bindCachesToRegistry() {
this.cacheManagers.forEach((beanName, cacheManager) -> cacheManager.getCacheNames() this.cacheManagers.forEach(this::bindCacheManagerToRegistry);
.forEach((cacheName) -> }
bindCacheToRegistry(beanName, cacheManager.getCache(cacheName))));
private void bindCacheManagerToRegistry(String beanName, CacheManager cacheManager) {
cacheManager.getCacheNames().forEach((cacheName) -> bindCacheToRegistry(beanName,
cacheManager.getCache(cacheName)));
} }
private void bindCacheToRegistry(String beanName, Cache cache) { private void bindCacheToRegistry(String beanName, Cache cache) {
......
...@@ -45,17 +45,14 @@ public class CacheMetricsConfigurationTests { ...@@ -45,17 +45,14 @@ public class CacheMetricsConfigurationTests {
@Test @Test
public void autoConfiguredCacheManagerIsInstrumented() { public void autoConfiguredCacheManagerIsInstrumented() {
this.contextRunner this.contextRunner
.withConfiguration( .withConfiguration(AutoConfigurations.of(CacheAutoConfiguration.class))
AutoConfigurations.of(CacheAutoConfiguration.class))
.withPropertyValues("spring.cache.type=caffeine", .withPropertyValues("spring.cache.type=caffeine",
"spring.cache.cache-names=cache1,cache2") "spring.cache.cache-names=cache1,cache2")
.run((context) -> { .run((context) -> {
MeterRegistry registry = context.getBean(MeterRegistry.class); MeterRegistry registry = context.getBean(MeterRegistry.class);
assertThat(registry.find("cache.requests") assertThat(registry.find("cache.requests").tags("name", "cache1")
.tags("name", "cache1")
.tags("cacheManager", "cacheManager").meter()).isPresent(); .tags("cacheManager", "cacheManager").meter()).isPresent();
assertThat(registry.find("cache.requests") assertThat(registry.find("cache.requests").tags("name", "cache2")
.tags("name", "cache2")
.tags("cacheManager", "cacheManager").meter()).isPresent(); .tags("cacheManager", "cacheManager").meter()).isPresent();
}); });
} }
...@@ -63,33 +60,30 @@ public class CacheMetricsConfigurationTests { ...@@ -63,33 +60,30 @@ public class CacheMetricsConfigurationTests {
@Test @Test
public void autoConfiguredCacheManagerWithCustomMetricName() { public void autoConfiguredCacheManagerWithCustomMetricName() {
this.contextRunner this.contextRunner
.withConfiguration( .withConfiguration(AutoConfigurations.of(CacheAutoConfiguration.class))
AutoConfigurations.of(CacheAutoConfiguration.class))
.withPropertyValues( .withPropertyValues(
"management.metrics.cache.cache-metric-name=custom.name", "management.metrics.cache.cache-metric-name=custom.name",
"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.find("custom.name.requests") assertThat(
.tags("name", "cache1") registry.find("custom.name.requests").tags("name", "cache1")
.tags("cacheManager", "cacheManager").meter()).isPresent(); .tags("cacheManager", "cacheManager").meter())
.isPresent();
}); });
} }
@Test @Test
public void autoConfiguredNonSupportedCacheManagerIsIgnored() { public void autoConfiguredNonSupportedCacheManagerIsIgnored() {
this.contextRunner this.contextRunner
.withConfiguration( .withConfiguration(AutoConfigurations.of(CacheAutoConfiguration.class))
AutoConfigurations.of(CacheAutoConfiguration.class))
.withPropertyValues("spring.cache.type=simple", .withPropertyValues("spring.cache.type=simple",
"spring.cache.cache-names=cache1,cache2") "spring.cache.cache-names=cache1,cache2")
.run((context) -> { .run((context) -> {
MeterRegistry registry = context.getBean(MeterRegistry.class); MeterRegistry registry = context.getBean(MeterRegistry.class);
assertThat(registry.find("cache.requests") assertThat(registry.find("cache.requests").tags("name", "cache1")
.tags("name", "cache1")
.tags("cacheManager", "cacheManager").meter()).isNotPresent(); .tags("cacheManager", "cacheManager").meter()).isNotPresent();
assertThat(registry.find("cache.requests") assertThat(registry.find("cache.requests").tags("name", "cache2")
.tags("name", "cache2")
.tags("cacheManager", "cacheManager").meter()).isNotPresent(); .tags("cacheManager", "cacheManager").meter()).isNotPresent();
}); });
} }
...@@ -97,19 +91,16 @@ public class CacheMetricsConfigurationTests { ...@@ -97,19 +91,16 @@ public class CacheMetricsConfigurationTests {
@Test @Test
public void cacheInstrumentationCanBeDisabled() { public void cacheInstrumentationCanBeDisabled() {
this.contextRunner this.contextRunner
.withConfiguration( .withConfiguration(AutoConfigurations.of(CacheAutoConfiguration.class))
AutoConfigurations.of(CacheAutoConfiguration.class))
.withPropertyValues("management.metrics.cache.instrument-cache=false", .withPropertyValues("management.metrics.cache.instrument-cache=false",
"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.find("cache.requests") assertThat(registry.find("cache.requests").tags("name", "cache1")
.tags("name", "cache1")
.tags("cacheManager", "cacheManager").meter()).isNotPresent(); .tags("cacheManager", "cacheManager").meter()).isNotPresent();
}); });
} }
@Configuration @Configuration
@EnableCaching @EnableCaching
static class RegistryConfiguration { static class RegistryConfiguration {
......
...@@ -24,10 +24,11 @@ import org.springframework.cache.Cache; ...@@ -24,10 +24,11 @@ import org.springframework.cache.Cache;
/** /**
* Provide a {@link MeterBinder} based on a {@link Cache}. * Provide a {@link MeterBinder} based on a {@link Cache}.
* *
* @param <C> The cache type
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 2.0.0 * @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} * Return the {@link MeterBinder} managing the specified {@link Cache} or {@code null}
...@@ -37,6 +38,6 @@ public interface CacheMeterBinderProvider { ...@@ -37,6 +38,6 @@ public interface CacheMeterBinderProvider {
* @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(Cache cache, String name, Iterable<Tag> tags); MeterBinder getMeterBinder(C cache, String name, Iterable<Tag> tags);
} }
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
* limitations under the License. * 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.ArrayList;
import java.util.Arrays; import java.util.Arrays;
...@@ -25,9 +25,11 @@ import io.micrometer.core.instrument.MeterRegistry; ...@@ -25,9 +25,11 @@ import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag; import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Tags; import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.binder.MeterBinder; 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.cache.Cache;
import org.springframework.core.ResolvableType;
/** /**
* Register supported {@link Cache} to a {@link MeterRegistry}. * Register supported {@link Cache} to a {@link MeterRegistry}.
...@@ -41,7 +43,7 @@ public class CacheMetricsRegistrar { ...@@ -41,7 +43,7 @@ public class CacheMetricsRegistrar {
private final String metricName; private final String metricName;
private final Collection<CacheMeterBinderProvider> cacheMeterBinderProviders; private final Collection<CacheMeterBinderProvider<?>> binderProviders;
/** /**
* Creates a new registrar. * Creates a new registrar.
...@@ -51,21 +53,21 @@ public class CacheMetricsRegistrar { ...@@ -51,21 +53,21 @@ public class CacheMetricsRegistrar {
* be used to detect compatible caches * be used to detect compatible caches
*/ */
public CacheMetricsRegistrar(MeterRegistry registry, String metricName, public CacheMetricsRegistrar(MeterRegistry registry, String metricName,
Collection<CacheMeterBinderProvider> binderProviders) { Collection<CacheMeterBinderProvider<?>> binderProviders) {
this.registry = registry; this.registry = registry;
this.metricName = metricName; this.metricName = metricName;
this.cacheMeterBinderProviders = binderProviders; this.binderProviders = binderProviders;
} }
/** /**
* Attempt to bind the specified {@link Cache} to the registry. Return {@code true} * Attempt to bind the specified {@link Cache} to the registry. Return {@code true} if
* if the cache is supported and was bound to the registry, {@code false} otherwise. * the cache is supported and was bound to the registry, {@code false} otherwise.
* @param cache the cache to handle * @param cache the cache to handle
* @param tags the tags to associate with the metrics of that cache * @param tags the tags to associate with the metrics of that cache
* @return {@code true} if the {@code cache} is supported and was registered * @return {@code true} if the {@code cache} is supported and was registered
*/ */
public boolean bindCacheToRegistry(Cache cache, Tag... tags) { 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); MeterBinder meterBinder = getMeterBinder(cache, allTags);
if (meterBinder != null) { if (meterBinder != null) {
meterBinder.bindTo(this.registry); meterBinder.bindTo(this.registry);
...@@ -74,15 +76,41 @@ public class CacheMetricsRegistrar { ...@@ -74,15 +76,41 @@ public class CacheMetricsRegistrar {
return false; return false;
} }
@SuppressWarnings({ "unchecked", "rawtypes" })
private MeterBinder getMeterBinder(Cache cache, List<Tag> tags) { private MeterBinder getMeterBinder(Cache cache, List<Tag> tags) {
tags.addAll(getAdditionalTags(cache)); tags.addAll(getAdditionalTags(cache));
for (CacheMeterBinderProvider binderProvider : this.cacheMeterBinderProviders) { for (CacheMeterBinderProvider<?> binderProvider : this.binderProviders) {
MeterBinder meterBinder = binderProvider.getMeterBinder(cache, Class<?> cacheType = ResolvableType
this.metricName, tags); .forClass(CacheMeterBinderProvider.class, binderProvider.getClass())
.resolveGeneric();
if (cacheType.isInstance(cache)) {
try {
MeterBinder meterBinder = ((CacheMeterBinderProvider) binderProvider)
.getMeterBinder(cache, this.metricName, tags);
if (meterBinder != null) { if (meterBinder != null) {
return meterBinder; 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; 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; ...@@ -20,85 +20,23 @@ import com.hazelcast.core.IMap;
import com.hazelcast.spring.cache.HazelcastCache; import com.hazelcast.spring.cache.HazelcastCache;
import io.micrometer.core.instrument.Tag; import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.binder.MeterBinder; 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.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 * @author Stephane Nicoll
* @since 2.0.0 * @since 2.0.0
*/ */
public abstract class CacheMeterBinderProviders { public class HazelcastCacheMeterBinderProvider
implements CacheMeterBinderProvider<HazelcastCache> {
/**
* {@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 @Override
public MeterBinder getMeterBinder(Cache cache, String name, Iterable<Tag> tags) { @SuppressWarnings("unchecked")
if (cache instanceof JCacheCache) { public MeterBinder getMeterBinder(HazelcastCache cache, String name,
return new JCacheMetrics(((JCacheCache) cache).getNativeCache(), Iterable<Tag> tags) {
return new HazelcastCacheMetrics((IMap<Object, Object>) cache.getNativeCache(),
name, tags); name, tags);
} }
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.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 @@ ...@@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.boot.actuate.autoconfigure.metrics.cache; package org.springframework.boot.actuate.metrics.cache;
import java.util.Collections; import java.util.Collections;
...@@ -23,7 +23,6 @@ import io.micrometer.core.instrument.MeterRegistry; ...@@ -23,7 +23,6 @@ import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry; import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.actuate.metrics.cache.CacheMeterBinderProviders;
import org.springframework.cache.caffeine.CaffeineCache; import org.springframework.cache.caffeine.CaffeineCache;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
...@@ -40,22 +39,21 @@ public class CacheMetricsRegistrarTests { ...@@ -40,22 +39,21 @@ 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( "root", Collections.singleton(new CaffeineCacheMeterBinderProvider()));
new CacheMeterBinderProviders.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") assertThat(this.meterRegistry.find("root.requests").tags("name", "test").meter())
.tags("name", "test").meter()).isPresent(); .isPresent();
} }
@Test @Test
public void bindToUnsupportedCache() { public void bindToUnsupportedCache() {
CacheMetricsRegistrar registrar = new CacheMetricsRegistrar(this.meterRegistry, CacheMetricsRegistrar registrar = new CacheMetricsRegistrar(this.meterRegistry,
"root", Collections.EMPTY_LIST); "root", 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") assertThat(this.meterRegistry.find("root.requests").tags("name", "test").meter())
.tags("name", "test").meter()).isNotPresent(); .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