From ba52aa367476c6cdeabdee4474c1cbd15d7d0da0 Mon Sep 17 00:00:00 2001 From: Jon Schneider Date: Mon, 5 Feb 2018 21:09:10 -0600 Subject: [PATCH 1/2] Align with Micrometer's standardized cache metrics See gh-11918 --- .../cache/EhCache2CacheMeterBinderProvider.java | 2 +- .../cache/HazelcastCacheMeterBinderProvider.java | 2 +- .../cache/JCacheCacheMeterBinderProvider.java | 2 +- .../metrics/cache/CacheMetricsRegistrarTests.java | 4 ++-- .../HazelcastCacheMeterBinderProviderTests.java | 15 ++++++++++----- 5 files changed, 15 insertions(+), 10 deletions(-) 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 index 53a775da9c..0bf3ce1d39 100644 --- 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 @@ -34,7 +34,7 @@ public class EhCache2CacheMeterBinderProvider @Override public MeterBinder getMeterBinder(EhCacheCache cache, String name, Iterable tags) { - return new EhCache2Metrics(cache.getNativeCache(), name, tags); + return new EhCache2Metrics(cache.getNativeCache(), 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 index 660bcee0b4..1503ea0a0b 100644 --- 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 @@ -36,7 +36,7 @@ public class HazelcastCacheMeterBinderProvider public MeterBinder getMeterBinder(HazelcastCache cache, String name, Iterable tags) { return new HazelcastCacheMetrics((IMap) cache.getNativeCache(), - name, tags); + 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 index 65188c5a92..95dddd0e24 100644 --- 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 @@ -34,7 +34,7 @@ public class JCacheCacheMeterBinderProvider @Override public MeterBinder getMeterBinder(JCacheCache cache, String name, Iterable tags) { - return new JCacheMetrics(cache.getNativeCache(), name, tags); + return new JCacheMetrics(cache.getNativeCache(), tags); } } 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 41ebac1e17..c9278c57cb 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 @@ -42,7 +42,7 @@ public class CacheMetricsRegistrarTests { "root", Collections.singleton(new CaffeineCacheMeterBinderProvider())); assertThat(registrar.bindCacheToRegistry( new CaffeineCache("test", Caffeine.newBuilder().build()))).isTrue(); - assertThat(this.meterRegistry.get("root.requests").tags("name", "test").meter()) + assertThat(this.meterRegistry.get("cache.gets").tags("name", "test").meter()) .isNotNull(); } @@ -52,7 +52,7 @@ public class CacheMetricsRegistrarTests { "root", Collections.emptyList()); assertThat(registrar.bindCacheToRegistry( new CaffeineCache("test", Caffeine.newBuilder().build()))).isFalse(); - assertThat(this.meterRegistry.find("root.requests").tags("name", "test").meter()) + assertThat(this.meterRegistry.find("cache.gets").tags("name", "test").meter()) .isNull(); } 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 index 99f0c4a3dc..65ad47461e 100644 --- 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 @@ -24,7 +24,7 @@ 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.Mockito; import org.mockito.junit.MockitoJUnitRunner; import static org.assertj.core.api.Assertions.assertThat; @@ -37,12 +37,17 @@ import static org.assertj.core.api.Assertions.assertThat; @RunWith(MockitoJUnitRunner.class) public class HazelcastCacheMeterBinderProviderTests { - @Mock - private IMap nativeCache; - + @SuppressWarnings("unchecked") @Test public void hazelcastCacheProvider() { - HazelcastCache cache = new HazelcastCache(this.nativeCache); + IMap nativeCache = Mockito.mock(IMap.class); + + // It is not possible to create a real Hazelcast cache with a null name, + // so Micrometer's Hazelcast binder uses the name from the cache for its tag value. + Mockito.when(nativeCache.getName()).thenReturn("test"); + + HazelcastCache cache = new HazelcastCache(nativeCache); + MeterBinder meterBinder = new HazelcastCacheMeterBinderProvider() .getMeterBinder(cache, "test", Collections.emptyList()); assertThat(meterBinder).isInstanceOf(HazelcastCacheMetrics.class); From 7d561f5ed0d527aed8e1dc4807b7666992780e5a Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 6 Feb 2018 11:13:37 +0000 Subject: [PATCH 2/2] Polish "Align with Micrometer's standardized cache metrics" Closes gh-11918 --- .../cache/CacheMetricsConfiguration.java | 2 - .../metrics/cache/CacheMetricsProperties.java | 43 ------------------- .../CacheMetricsRegistrarConfiguration.java | 7 +-- .../cache/CacheMetricsConfigurationTests.java | 20 ++------- .../cache/CacheMeterBinderProvider.java | 3 +- .../metrics/cache/CacheMetricsRegistrar.java | 8 +--- .../CaffeineCacheMeterBinderProvider.java | 5 +-- .../EhCache2CacheMeterBinderProvider.java | 3 +- .../HazelcastCacheMeterBinderProvider.java | 3 +- .../cache/JCacheCacheMeterBinderProvider.java | 3 +- .../cache/CacheMetricsRegistrarTests.java | 4 +- ...CaffeineCacheMeterBinderProviderTests.java | 2 +- ...EhCache2CacheMeterBinderProviderTests.java | 2 +- ...azelcastCacheMeterBinderProviderTests.java | 14 +++--- .../JCacheCacheMeterBinderProviderTests.java | 2 +- .../appendix-application-properties.adoc | 1 - .../asciidoc/production-ready-features.adoc | 11 +++-- 17 files changed, 28 insertions(+), 105 deletions(-) delete mode 100644 spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/cache/CacheMetricsProperties.java 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 432aaab2d1..3f8234e8d1 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 @@ -18,7 +18,6 @@ package org.springframework.boot.actuate.autoconfigure.metrics.cache; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; import org.springframework.context.annotation.Configuration; @@ -33,7 +32,6 @@ import org.springframework.context.annotation.Import; @Configuration @ConditionalOnBean(CacheManager.class) @ConditionalOnProperty(value = "management.metrics.cache.instrument", matchIfMissing = true) -@EnableConfigurationProperties(CacheMetricsProperties.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/CacheMetricsProperties.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/cache/CacheMetricsProperties.java deleted file mode 100644 index 58199f3db6..0000000000 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/cache/CacheMetricsProperties.java +++ /dev/null @@ -1,43 +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.autoconfigure.metrics.cache; - -import org.springframework.boot.context.properties.ConfigurationProperties; - -/** - * Configuration properties for Cache-based metrics. - * - * @author Stephane Nicoll - * @since 2.0.0 - */ -@ConfigurationProperties("management.metrics.cache") -public class CacheMetricsProperties { - - /** - * Name of the metric for cache usage. - */ - private String metricName = "cache"; - - public String getMetricName() { - return this.metricName; - } - - public void setMetricName(String metricName) { - this.metricName = metricName; - } - -} 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 96de0bbf21..73f710c3a9 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 @@ -49,24 +49,19 @@ class CacheMetricsRegistrarConfiguration { private final Collection> binderProviders; - private final CacheMetricsProperties properties; - private final Map cacheManagers; CacheMetricsRegistrarConfiguration(MeterRegistry registry, - CacheMetricsProperties properties, Collection> binderProviders, Map cacheManagers) { this.registry = registry; - this.properties = properties; this.binderProviders = binderProviders; this.cacheManagers = cacheManagers; } @Bean public CacheMetricsRegistrar cacheMetricsRegistrar() { - return new CacheMetricsRegistrar(this.registry, this.properties.getMetricName(), - this.binderProviders); + return new CacheMetricsRegistrar(this.registry, this.binderProviders); } @PostConstruct 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 c4ba64c72d..a57ce95484 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 @@ -44,21 +44,9 @@ public class CacheMetricsConfigurationTests { this.contextRunner.withPropertyValues("spring.cache.type=caffeine", "spring.cache.cache-names=cache1,cache2").run((context) -> { MeterRegistry registry = context.getBean(MeterRegistry.class); - registry.get("cache.requests").tags("name", "cache1") + registry.get("cache.gets").tags("name", "cache1") .tags("cacheManager", "cacheManager").meter(); - registry.get("cache.requests").tags("name", "cache2") - .tags("cacheManager", "cacheManager").meter(); - }); - } - - @Test - public void autoConfiguredCacheManagerWithCustomMetricName() { - this.contextRunner - .withPropertyValues("management.metrics.cache.metric-name=custom.name", - "spring.cache.type=caffeine", "spring.cache.cache-names=cache1") - .run((context) -> { - MeterRegistry registry = context.getBean(MeterRegistry.class); - registry.get("custom.name.requests").tags("name", "cache1") + registry.get("cache.gets").tags("name", "cache2") .tags("cacheManager", "cacheManager").meter(); }); } @@ -68,9 +56,9 @@ public class CacheMetricsConfigurationTests { this.contextRunner.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.gets").tags("name", "cache1") .tags("cacheManager", "cacheManager").meter()).isNull(); - assertThat(registry.find("cache.requests").tags("name", "cache2") + assertThat(registry.find("cache.gets").tags("name", "cache2") .tags("cacheManager", "cacheManager").meter()).isNull(); }); } 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 f850b4d57a..3d44d5d64f 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 @@ -35,10 +35,9 @@ public interface CacheMeterBinderProvider { * Return the {@link MeterBinder} managing the specified {@link Cache} or {@code null} * if the specified {@link Cache} is not supported. * @param cache the cache to instrument - * @param name the name prefix of the metrics * @param tags tags to apply to all recorded metrics * @return a {@link MeterBinder} handling the specified {@link Cache} or {@code null} */ - MeterBinder getMeterBinder(C cache, String name, Iterable tags); + MeterBinder getMeterBinder(C cache, Iterable tags); } 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 bf00ec205e..d3e717e9c8 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 @@ -37,21 +37,17 @@ public class CacheMetricsRegistrar { private final MeterRegistry registry; - private final String metricName; - private final Collection> binderProviders; /** * Creates a new registrar. * @param registry the {@link MeterRegistry} to use - * @param metricName the name of the metric * @param binderProviders the {@link CacheMeterBinderProvider} instances that should * be used to detect compatible caches */ - public CacheMetricsRegistrar(MeterRegistry registry, String metricName, + public CacheMetricsRegistrar(MeterRegistry registry, Collection> binderProviders) { this.registry = registry; - this.metricName = metricName; this.binderProviders = binderProviders; } @@ -78,7 +74,7 @@ public class CacheMetricsRegistrar { .callbacks(CacheMeterBinderProvider.class, this.binderProviders, cache) .withLogger(CacheMetricsRegistrar.class) .invokeAnd((binderProvider) -> binderProvider.getMeterBinder(cache, - this.metricName, cacheTags)) + cacheTags)) .filter(Objects::nonNull).findFirst().orElse(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 index 11a3968fd3..71bcf63a14 100644 --- 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 @@ -32,9 +32,8 @@ public class CaffeineCacheMeterBinderProvider implements CacheMeterBinderProvider { @Override - public MeterBinder getMeterBinder(CaffeineCache cache, String name, - Iterable tags) { - return new CaffeineCacheMetrics(cache.getNativeCache(), name, tags); + public MeterBinder getMeterBinder(CaffeineCache cache, Iterable tags) { + return new CaffeineCacheMetrics(cache.getNativeCache(), cache.getName(), tags); } } 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 index 0bf3ce1d39..f7867f7693 100644 --- 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 @@ -32,8 +32,7 @@ public class EhCache2CacheMeterBinderProvider implements CacheMeterBinderProvider { @Override - public MeterBinder getMeterBinder(EhCacheCache cache, String name, - Iterable tags) { + public MeterBinder getMeterBinder(EhCacheCache cache, Iterable tags) { return new EhCache2Metrics(cache.getNativeCache(), 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 index 1503ea0a0b..a3aa556004 100644 --- 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 @@ -33,8 +33,7 @@ public class HazelcastCacheMeterBinderProvider @Override @SuppressWarnings("unchecked") - public MeterBinder getMeterBinder(HazelcastCache cache, String name, - Iterable tags) { + public MeterBinder getMeterBinder(HazelcastCache cache, Iterable tags) { return new HazelcastCacheMetrics((IMap) cache.getNativeCache(), 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 index 95dddd0e24..f323e73768 100644 --- 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 @@ -32,8 +32,7 @@ public class JCacheCacheMeterBinderProvider implements CacheMeterBinderProvider { @Override - public MeterBinder getMeterBinder(JCacheCache cache, String name, - Iterable tags) { + public MeterBinder getMeterBinder(JCacheCache cache, Iterable tags) { return new JCacheMetrics(cache.getNativeCache(), tags); } 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 c9278c57cb..84a41e06e4 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 @@ -39,7 +39,7 @@ public class CacheMetricsRegistrarTests { @Test public void bindToSupportedCache() { CacheMetricsRegistrar registrar = new CacheMetricsRegistrar(this.meterRegistry, - "root", Collections.singleton(new CaffeineCacheMeterBinderProvider())); + Collections.singleton(new CaffeineCacheMeterBinderProvider())); assertThat(registrar.bindCacheToRegistry( new CaffeineCache("test", Caffeine.newBuilder().build()))).isTrue(); assertThat(this.meterRegistry.get("cache.gets").tags("name", "test").meter()) @@ -49,7 +49,7 @@ public class CacheMetricsRegistrarTests { @Test public void bindToUnsupportedCache() { CacheMetricsRegistrar registrar = new CacheMetricsRegistrar(this.meterRegistry, - "root", Collections.emptyList()); + Collections.emptyList()); assertThat(registrar.bindCacheToRegistry( new CaffeineCache("test", Caffeine.newBuilder().build()))).isFalse(); assertThat(this.meterRegistry.find("cache.gets").tags("name", "test").meter()) 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 index e81f00f7ad..e41f4190ab 100644 --- 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 @@ -38,7 +38,7 @@ public class CaffeineCacheMeterBinderProviderTests { public void caffeineCacheProvider() { CaffeineCache cache = new CaffeineCache("test", Caffeine.newBuilder().build()); MeterBinder meterBinder = new CaffeineCacheMeterBinderProvider() - .getMeterBinder(cache, "test", Collections.emptyList()); + .getMeterBinder(cache, 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 index f26f500fbe..9a055ffbdb 100644 --- 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 @@ -47,7 +47,7 @@ public class EhCache2CacheMeterBinderProviderTests { cacheManager.addCache(nativeCache); EhCacheCache cache = new EhCacheCache(nativeCache); MeterBinder meterBinder = new EhCache2CacheMeterBinderProvider() - .getMeterBinder(cache, "test", Collections.emptyList()); + .getMeterBinder(cache, Collections.emptyList()); assertThat(meterBinder).isInstanceOf(EhCache2Metrics.class); } finally { 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 index 65ad47461e..126ebd3a1e 100644 --- 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 @@ -24,10 +24,11 @@ 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.Mockito; import org.mockito.junit.MockitoJUnitRunner; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; /** * Tests for {@link HazelcastCacheMeterBinderProvider}. @@ -40,16 +41,11 @@ public class HazelcastCacheMeterBinderProviderTests { @SuppressWarnings("unchecked") @Test public void hazelcastCacheProvider() { - IMap nativeCache = Mockito.mock(IMap.class); - - // It is not possible to create a real Hazelcast cache with a null name, - // so Micrometer's Hazelcast binder uses the name from the cache for its tag value. - Mockito.when(nativeCache.getName()).thenReturn("test"); - + IMap nativeCache = mock(IMap.class); + given(nativeCache.getName()).willReturn("test"); HazelcastCache cache = new HazelcastCache(nativeCache); - MeterBinder meterBinder = new HazelcastCacheMeterBinderProvider() - .getMeterBinder(cache, "test", Collections.emptyList()); + .getMeterBinder(cache, 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 index 7a5ca9add6..c1da849424 100644 --- 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 @@ -52,7 +52,7 @@ public class JCacheCacheMeterBinderProviderTests { given(this.nativeCache.getName()).willReturn("test"); JCacheCache cache = new JCacheCache(this.nativeCache); MeterBinder meterBinder = new JCacheCacheMeterBinderProvider() - .getMeterBinder(cache, "test", Collections.emptyList()); + .getMeterBinder(cache, Collections.emptyList()); assertThat(meterBinder).isInstanceOf(JCacheMetrics.class); } diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index 79d318ab47..9eb3393c74 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -1297,7 +1297,6 @@ content into your application. Rather, pick only the properties that you need. management.metrics.binders.logback.enabled=true # Whether to enable Logback metrics. management.metrics.binders.processor.enabled=true # Whether to enable processor metrics. management.metrics.binders.uptime.enabled=true # Whether to enable uptime metrics. - management.metrics.cache.metric-name=cache # Name of the metric for cache usage. management.metrics.cache.instrument=true # Instrument all available caches. management.metrics.export.atlas.batch-size= # Number of measurements per request to use for the backend. If more measurements are found, then multiple requests will be made. management.metrics.export.atlas.config-refresh-frequency= # Frequency for refreshing config settings from the LWC service. diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc index 0235a51d5a..261f1d3823 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc @@ -1194,10 +1194,9 @@ following information: [[production-ready-metrics-cache]] === Cache metrics Auto-configuration will enable the instrumentation of all available ``Cache``s on startup -with a metric named `cache`. The prefix can be customized by using the -`management.metrics.cache.metric-name` property. Cache instrumentation is specific -to each cache library, refer to https://micrometer.io/docs[the micrometer documentation] -for more details. +with metrics prefixed with `cache.`. Cache instrumentation is standardized for a basic set +of metrics. Additional, cache-specific metrics are also available. Please refer to +https://micrometer.io/docs[the Micrometer documentation] for more details. The following cache libraries are supported: @@ -1206,8 +1205,8 @@ The following cache libraries are supported: * Hazelcast * Any compliant JCache (JSR-107) implementation -Metrics will also be tagged by the name of the `CacheManager` computed based on the bean -name. +Metrics are tagged by the name of the cache and by the name of the `CacheManager` that is +derived from the bean name. NOTE: Only caches that are available on startup are bound to the registry. For caches created on-the-fly or programmatically after the startup phase, an explicit registration