From b73e1d46ae8591c2f9aba6c87ef68b8218d4a2fb Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 22 May 2017 10:07:44 +0200 Subject: [PATCH 1/2] Start building against Spring Framework snapshots See gh-9280 --- spring-boot-dependencies/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-boot-dependencies/pom.xml b/spring-boot-dependencies/pom.xml index b7719c6fa5..41e860d22e 100644 --- a/spring-boot-dependencies/pom.xml +++ b/spring-boot-dependencies/pom.xml @@ -145,7 +145,7 @@ 1.17 5.5.4 1.0-groovy-2.4 - 4.3.8.RELEASE + 4.3.9.BUILD-SNAPSHOT 1.6.9.RELEASE 1.2.4.RELEASE 3.0.7.RELEASE From a59000354cce2a9b518ec1bdaac1da37f9f0dffe Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 22 May 2017 11:05:39 +0200 Subject: [PATCH 2/2] Support for TransactionAwareCacheDecorator This commit makes sure to unwrap any transaction aware cache before collecting metrics for them. Closes gh-8984 --- .../actuate/endpoint/CachePublicMetrics.java | 27 ++++++++++++++++++- .../endpoint/CachePublicMetricsTests.java | 17 ++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/CachePublicMetrics.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/CachePublicMetrics.java index 70c7050fca..d51d73020c 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/CachePublicMetrics.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/CachePublicMetrics.java @@ -27,7 +27,9 @@ import org.springframework.boot.actuate.cache.CacheStatisticsProvider; import org.springframework.boot.actuate.metrics.Metric; import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; +import org.springframework.cache.transaction.TransactionAwareCacheDecorator; import org.springframework.core.ResolvableType; +import org.springframework.util.ClassUtils; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; @@ -87,7 +89,7 @@ public class CachePublicMetrics implements PublicMetrics { List cacheManagerBeans) { for (CacheManagerBean cacheManagerBean : cacheManagerBeans) { CacheManager cacheManager = cacheManagerBean.getCacheManager(); - Cache cache = cacheManager.getCache(cacheName); + Cache cache = unwrapIfNecessary(cacheManager.getCache(cacheName)); CacheStatistics statistics = getCacheStatistics(cache, cacheManager); if (statistics != null) { String prefix = cacheName; @@ -100,6 +102,14 @@ public class CachePublicMetrics implements PublicMetrics { } } + private Cache unwrapIfNecessary(Cache cache) { + if (ClassUtils.isPresent("org.springframework.cache.transaction.TransactionAwareCacheDecorator", + getClass().getClassLoader())) { + return TransactionAwareCacheDecoratorHandler.unwrapIfNecessary(cache); + } + return cache; + } + @SuppressWarnings({ "rawtypes", "unchecked" }) private CacheStatistics getCacheStatistics(Cache cache, CacheManager cacheManager) { if (this.statisticsProviders != null) { @@ -140,4 +150,19 @@ public class CachePublicMetrics implements PublicMetrics { } + private static class TransactionAwareCacheDecoratorHandler { + + private static Cache unwrapIfNecessary(Cache cache) { + try { + if (cache instanceof TransactionAwareCacheDecorator) { + return ((TransactionAwareCacheDecorator) cache).getTargetCache(); + } + } + catch (NoClassDefFoundError ex) { + // Ignore + } + return cache; + } + } + } diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/CachePublicMetricsTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/CachePublicMetricsTests.java index 46f341d5bf..9986fbbe4f 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/CachePublicMetricsTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/CachePublicMetricsTests.java @@ -18,6 +18,7 @@ package org.springframework.boot.actuate.endpoint; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -29,7 +30,10 @@ import org.springframework.boot.actuate.cache.CaffeineCacheStatisticsProvider; import org.springframework.boot.actuate.cache.ConcurrentMapCacheStatisticsProvider; import org.springframework.boot.actuate.metrics.Metric; import org.springframework.cache.CacheManager; +import org.springframework.cache.concurrent.ConcurrentMapCache; import org.springframework.cache.concurrent.ConcurrentMapCacheManager; +import org.springframework.cache.support.SimpleCacheManager; +import org.springframework.cache.transaction.TransactionAwareCacheDecorator; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.entry; @@ -78,6 +82,19 @@ public class CachePublicMetricsTests { entry("cache.anotherCacheManager_foo.size", 0L)); } + @Test + public void cacheMetricsWithTransactionAwareCacheDecorator() { + SimpleCacheManager cacheManager = new SimpleCacheManager(); + cacheManager.setCaches(Collections.singletonList( + new TransactionAwareCacheDecorator(new ConcurrentMapCache("foo")))); + cacheManager.afterPropertiesSet(); + this.cacheManagers.put("cacheManager", cacheManager); + CachePublicMetrics cpm = new CachePublicMetrics(this.cacheManagers, + providers(new ConcurrentMapCacheStatisticsProvider())); + Map metrics = metrics(cpm); + assertThat(metrics).containsOnly(entry("cache.foo.size", 0L)); + } + private Map metrics(CachePublicMetrics cpm) { Collection> metrics = cpm.metrics(); assertThat(metrics).isNotNull();