Skip to content

Commit 50cd4ae

Browse files
authored
Restore cache.size in CaffeineCacheMetrics without enabling recordStats() (#6136)
Closes gh-6128 Signed-off-by: Johnny Lim <[email protected]>
1 parent 277606b commit 50cd4ae

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

micrometer-core/src/main/java/io/micrometer/core/instrument/binder/cache/CaffeineCacheMetrics.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public static <K, V, C extends AsyncCache<K, V>> C monitor(MeterRegistry registr
150150

151151
@Override
152152
protected Long size() {
153-
return getOrDefault(Cache::estimatedSize, null);
153+
return getOrDefault(Cache::estimatedSize, null, false);
154154
}
155155

156156
@Override
@@ -208,9 +208,14 @@ protected void bindImplementationSpecificMetrics(MeterRegistry registry) {
208208

209209
@Nullable
210210
private Long getOrDefault(Function<C, Long> function, @Nullable Long defaultValue) {
211+
return getOrDefault(function, defaultValue, true);
212+
}
213+
214+
@Nullable
215+
private Long getOrDefault(Function<C, Long> function, @Nullable Long defaultValue, boolean recordingStatsRequired) {
211216
C cache = getCache();
212217
if (cache != null) {
213-
if (!cache.policy().isRecordingStats()) {
218+
if (recordingStatsRequired && !cache.policy().isRecordingStats()) {
214219
return null;
215220
}
216221

micrometer-core/src/test/java/io/micrometer/core/instrument/binder/cache/CaffeineCacheMetricsTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@
2525
import io.micrometer.core.instrument.TimeGauge;
2626
import io.micrometer.core.instrument.search.MeterNotFoundException;
2727
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
28+
import io.micrometer.core.testsupport.system.CapturedOutput;
29+
import io.micrometer.core.testsupport.system.OutputCaptureExtension;
2830
import org.assertj.core.data.Offset;
2931
import org.junit.jupiter.api.Test;
32+
import org.junit.jupiter.api.extension.ExtendWith;
3033

3134
import java.util.concurrent.TimeUnit;
3235

@@ -38,6 +41,7 @@
3841
* @author Oleksii Bondar
3942
* @author Johnny Lim
4043
*/
44+
@ExtendWith(OutputCaptureExtension.class)
4145
class CaffeineCacheMetricsTest extends AbstractCacheMetricsTest {
4246

4347
// tag::setup[]
@@ -103,6 +107,22 @@ void returnCacheSize() {
103107
assertThat(metrics.size()).isEqualTo(cache.estimatedSize());
104108
}
105109

110+
@Test
111+
void returnCacheSizeWithoutRecordStats(CapturedOutput output) {
112+
LoadingCache<String, String> cache = Caffeine.newBuilder().build(key -> "");
113+
CaffeineCacheMetrics<String, String, Cache<String, String>> metrics = new CaffeineCacheMetrics<>(cache,
114+
"testCache", Tags.empty());
115+
116+
MeterRegistry meterRegistry = new SimpleMeterRegistry();
117+
metrics.bindTo(meterRegistry);
118+
119+
cache.put("a", "1");
120+
assertThat(metrics.size()).isOne();
121+
assertThat(meterRegistry.get("cache.size").gauge().value()).isOne();
122+
assertThat(output).contains(
123+
"The cache 'testCache' is not recording statistics. No meters except 'cache.size' will be registered. Call 'Caffeine#recordStats()' prior to building the cache for metrics to be recorded.");
124+
}
125+
106126
@Test
107127
void returnHitCount() {
108128
MeterRegistry meterRegistry = new SimpleMeterRegistry();

0 commit comments

Comments
 (0)