diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/cache/CacheMeterBinderProvidersConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/cache/CacheMeterBinderProvidersConfiguration.java index e0f7e6223a..38d825a2dd 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/cache/CacheMeterBinderProvidersConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/cache/CacheMeterBinderProvidersConfiguration.java @@ -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(); } } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/cache/CacheMetricsConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/cache/CacheMetricsConfiguration.java index fc35039387..cd306c3c66 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/cache/CacheMetricsConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/cache/CacheMetricsConfiguration.java @@ -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 { } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/cache/CacheMetricsRegistrarConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/cache/CacheMetricsRegistrarConfiguration.java index 8c5ae0aa23..9fc1cfbb3d 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/cache/CacheMetricsRegistrarConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/cache/CacheMetricsRegistrarConfiguration.java @@ -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> binderProviders; - private final Collection cacheMeterBinderProviders; + private final CacheMetricsProperties properties; private final Map cacheManagers; CacheMetricsRegistrarConfiguration(MeterRegistry registry, CacheMetricsProperties properties, - Collection cacheMeterBinderProviders, + Collection> binderProviders, Map 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) { diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/cache/CacheMetricsConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/cache/CacheMetricsConfigurationTests.java index cf769be4b6..39c9743603 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/cache/CacheMetricsConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/cache/CacheMetricsConfigurationTests.java @@ -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 { diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/cache/CacheMeterBinderProvider.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/cache/CacheMeterBinderProvider.java index 2bc9a093f2..e9659c1764 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/cache/CacheMeterBinderProvider.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/cache/CacheMeterBinderProvider.java @@ -24,10 +24,11 @@ import org.springframework.cache.Cache; /** * Provide a {@link MeterBinder} based on a {@link Cache}. * + * @param The cache type * @author Stephane Nicoll * @since 2.0.0 */ -public interface CacheMeterBinderProvider { +public interface CacheMeterBinderProvider { /** * 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 tags); + MeterBinder getMeterBinder(C cache, String name, Iterable tags); } diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/cache/CacheMeterBinderProviders.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/cache/CacheMeterBinderProviders.java deleted file mode 100644 index 15b2ea9a6a..0000000000 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/cache/CacheMeterBinderProviders.java +++ /dev/null @@ -1,104 +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.metrics.cache; - -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. - * - * @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 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 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 tags) { - if (cache instanceof HazelcastCache) { - IMap nativeCache = (IMap) ((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 tags) { - if (cache instanceof JCacheCache) { - return new JCacheMetrics(((JCacheCache) cache).getNativeCache(), - name, tags); - } - return null; - } - } - -} diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/cache/CacheMetricsRegistrar.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/cache/CacheMetricsRegistrar.java similarity index 61% rename from spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/cache/CacheMetricsRegistrar.java rename to spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/cache/CacheMetricsRegistrar.java index 9383fb68a4..194a3e0abc 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/cache/CacheMetricsRegistrar.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/cache/CacheMetricsRegistrar.java @@ -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 cacheMeterBinderProviders; + private final Collection> 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 binderProviders) { + Collection> 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 allTags = new ArrayList(Arrays.asList(tags)); + List 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 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; } diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/cache/CaffeineCacheMeterBinderProvider.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/cache/CaffeineCacheMeterBinderProvider.java new file mode 100644 index 0000000000..3450059c62 --- /dev/null +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/cache/CaffeineCacheMeterBinderProvider.java @@ -0,0 +1,40 @@ +/* + * 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 { + + @Override + public MeterBinder getMeterBinder(CaffeineCache cache, String name, + Iterable tags) { + return new CaffeineCacheMetrics(cache.getNativeCache(), tags, name); + } + +} diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/cache/EhCache2CacheMeterBinderProvider.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/cache/EhCache2CacheMeterBinderProvider.java new file mode 100644 index 0000000000..53a775da9c --- /dev/null +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/cache/EhCache2CacheMeterBinderProvider.java @@ -0,0 +1,40 @@ +/* + * 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 { + + @Override + public MeterBinder getMeterBinder(EhCacheCache cache, String name, + Iterable tags) { + return new EhCache2Metrics(cache.getNativeCache(), name, tags); + } + +} diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/cache/HazelcastCacheMeterBinderProvider.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/cache/HazelcastCacheMeterBinderProvider.java new file mode 100644 index 0000000000..660bcee0b4 --- /dev/null +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/cache/HazelcastCacheMeterBinderProvider.java @@ -0,0 +1,42 @@ +/* + * 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 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.HazelcastCacheMetrics; + +/** + * {@link CacheMeterBinderProvider} implementation for Hazelcast. + * + * @author Stephane Nicoll + * @since 2.0.0 + */ +public class HazelcastCacheMeterBinderProvider + implements CacheMeterBinderProvider { + + @Override + @SuppressWarnings("unchecked") + public MeterBinder getMeterBinder(HazelcastCache cache, String name, + Iterable tags) { + return new HazelcastCacheMetrics((IMap) cache.getNativeCache(), + name, tags); + } + +} diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/cache/JCacheCacheMeterBinderProvider.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/cache/JCacheCacheMeterBinderProvider.java new file mode 100644 index 0000000000..65188c5a92 --- /dev/null +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/cache/JCacheCacheMeterBinderProvider.java @@ -0,0 +1,40 @@ +/* + * 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 { + + @Override + public MeterBinder getMeterBinder(JCacheCache cache, String name, + Iterable tags) { + return new JCacheMetrics(cache.getNativeCache(), name, tags); + } + +} diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/cache/CacheMeterBinderProvidersTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/cache/CacheMeterBinderProvidersTests.java deleted file mode 100644 index 424531b0d0..0000000000 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/cache/CacheMeterBinderProvidersTests.java +++ /dev/null @@ -1,135 +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.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 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 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(); - } - -} diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/cache/CacheMetricsRegistrarTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/cache/CacheMetricsRegistrarTests.java similarity index 76% rename from spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/cache/CacheMetricsRegistrarTests.java rename to spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/cache/CacheMetricsRegistrarTests.java index f8d91d50dd..bd3fbc5430 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/cache/CacheMetricsRegistrarTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/cache/CacheMetricsRegistrarTests.java @@ -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(); } } diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/cache/CaffeineCacheMeterBinderProviderTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/cache/CaffeineCacheMeterBinderProviderTests.java new file mode 100644 index 0000000000..d094b82858 --- /dev/null +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/cache/CaffeineCacheMeterBinderProviderTests.java @@ -0,0 +1,45 @@ +/* + * 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); + } + +} diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/cache/EhCache2CacheMeterBinderProviderTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/cache/EhCache2CacheMeterBinderProviderTests.java new file mode 100644 index 0000000000..09542ecc43 --- /dev/null +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/cache/EhCache2CacheMeterBinderProviderTests.java @@ -0,0 +1,58 @@ +/* + * 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(); + } + } + +} diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/cache/HazelcastCacheMeterBinderProviderTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/cache/HazelcastCacheMeterBinderProviderTests.java new file mode 100644 index 0000000000..f83190d40d --- /dev/null +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/cache/HazelcastCacheMeterBinderProviderTests.java @@ -0,0 +1,51 @@ +/* + * 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 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); + } + +} diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/cache/JCacheCacheMeterBinderProviderTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/cache/JCacheCacheMeterBinderProviderTests.java new file mode 100644 index 0000000000..a2bd4b2f56 --- /dev/null +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/cache/JCacheCacheMeterBinderProviderTests.java @@ -0,0 +1,59 @@ +/* + * 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 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); + } + +}