From a25b6bd473fb2e2071e377f9bfce8b60b1900c95 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 25 Jul 2018 09:17:01 +0200 Subject: [PATCH] Restore support for TransactionAwareCacheDecorator This commit makes sure to unwrap any transaction aware cache before collecting metrics for them. Closes gh-13861 --- .../metrics/cache/CacheMetricsRegistrar.java | 29 ++++++++++++++++++- .../cache/CacheMetricsRegistrarTests.java | 11 +++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/cache/CacheMetricsRegistrar.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/cache/CacheMetricsRegistrar.java index d3e717e9c8..b9e9825e3d 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/cache/CacheMetricsRegistrar.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/cache/CacheMetricsRegistrar.java @@ -26,6 +26,8 @@ import io.micrometer.core.instrument.binder.MeterBinder; import org.springframework.boot.util.LambdaSafe; import org.springframework.cache.Cache; +import org.springframework.cache.transaction.TransactionAwareCacheDecorator; +import org.springframework.util.ClassUtils; /** * Register supported {@link Cache} to a {@link MeterRegistry}. @@ -59,7 +61,7 @@ public class CacheMetricsRegistrar { * @return {@code true} if the {@code cache} is supported and was registered */ public boolean bindCacheToRegistry(Cache cache, Tag... tags) { - MeterBinder meterBinder = getMeterBinder(cache, Tags.of(tags)); + MeterBinder meterBinder = getMeterBinder(unwrapIfNecessary(cache), Tags.of(tags)); if (meterBinder != null) { meterBinder.bindTo(this.registry); return true; @@ -87,4 +89,29 @@ public class CacheMetricsRegistrar { return Tags.of("name", cache.getName()); } + private Cache unwrapIfNecessary(Cache cache) { + if (ClassUtils.isPresent( + "org.springframework.cache.transaction.TransactionAwareCacheDecorator", + getClass().getClassLoader())) { + return TransactionAwareCacheDecoratorHandler.unwrapIfNecessary(cache); + } + return cache; + } + + 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-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/cache/CacheMetricsRegistrarTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/cache/CacheMetricsRegistrarTests.java index 84a41e06e4..7561688626 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/cache/CacheMetricsRegistrarTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/cache/CacheMetricsRegistrarTests.java @@ -24,6 +24,7 @@ import io.micrometer.core.instrument.simple.SimpleMeterRegistry; import org.junit.Test; import org.springframework.cache.caffeine.CaffeineCache; +import org.springframework.cache.transaction.TransactionAwareCacheDecorator; import static org.assertj.core.api.Assertions.assertThat; @@ -46,6 +47,16 @@ public class CacheMetricsRegistrarTests { .isNotNull(); } + @Test + public void bindToSupportedCacheWrappedInTransactionProxy() { + CacheMetricsRegistrar registrar = new CacheMetricsRegistrar(this.meterRegistry, + Collections.singleton(new CaffeineCacheMeterBinderProvider())); + assertThat(registrar.bindCacheToRegistry(new TransactionAwareCacheDecorator( + new CaffeineCache("test", Caffeine.newBuilder().build())))).isTrue(); + assertThat(this.meterRegistry.get("cache.gets").tags("name", "test").meter()) + .isNotNull(); + } + @Test public void bindToUnsupportedCache() { CacheMetricsRegistrar registrar = new CacheMetricsRegistrar(this.meterRegistry,