Skip to content

Commit 8049458

Browse files
kmclaughlinben-manes
authored andcommitted
Add 'record-native-stats' option to jcache (fixes #460)
When true, this enables stats collection via Caffeine.recordStats() on the underlying cache. The statistics can be accessed by using JCache's unwrap method, thereby registering into another metrics system (e.g. micrometer).
1 parent c571207 commit 8049458

File tree

6 files changed

+39
-5
lines changed

6 files changed

+39
-5
lines changed

jcache/src/main/java/com/github/benmanes/caffeine/jcache/CacheFactory.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ public CacheProxy<K, V> build() {
160160
evicts |= configureExpireAfterAccess();
161161
evicts |= configureExpireVariably();
162162

163+
if (config.isNativeStatisticsEnabled()) {
164+
caffeine.recordStats();
165+
}
166+
163167
JCacheEvictionListener<K, V> evictionListener = null;
164168
if (evicts) {
165169
evictionListener = new JCacheEvictionListener<>(dispatcher, statistics);

jcache/src/main/java/com/github/benmanes/caffeine/jcache/configuration/CaffeineConfiguration.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.concurrent.Executor;
2424
import java.util.concurrent.ForkJoinPool;
2525

26-
import org.checkerframework.checker.nullness.qual.Nullable;
2726
import javax.cache.configuration.CacheEntryListenerConfiguration;
2827
import javax.cache.configuration.CompleteConfiguration;
2928
import javax.cache.configuration.Factory;
@@ -32,6 +31,8 @@
3231
import javax.cache.integration.CacheLoader;
3332
import javax.cache.integration.CacheWriter;
3433

34+
import org.checkerframework.checker.nullness.qual.Nullable;
35+
3536
import com.github.benmanes.caffeine.cache.Expiry;
3637
import com.github.benmanes.caffeine.cache.Ticker;
3738
import com.github.benmanes.caffeine.cache.Weigher;
@@ -68,6 +69,7 @@ public final class CaffeineConfiguration<K, V> implements CompleteConfiguration<
6869
private @Nullable Long expireAfterWriteNanos;
6970
private @Nullable Long maximumWeight;
7071
private @Nullable Long maximumSize;
72+
private boolean nativeStatistics;
7173

7274
public CaffeineConfiguration() {
7375
delegate = new MutableConfiguration<>();
@@ -84,6 +86,7 @@ public CaffeineConfiguration(CompleteConfiguration<K, V> configuration) {
8486
refreshAfterWriteNanos = config.refreshAfterWriteNanos;
8587
expireAfterAccessNanos = config.expireAfterAccessNanos;
8688
expireAfterWriteNanos = config.expireAfterWriteNanos;
89+
nativeStatistics = config.nativeStatistics;
8790
executorFactory = config.executorFactory;
8891
expiryFactory = config.expiryFactory;
8992
copierFactory = config.copierFactory;
@@ -205,6 +208,26 @@ public void setStoreByValue(boolean isStoreByValue) {
205208
delegate.setStoreByValue(isStoreByValue);
206209
}
207210

211+
/**
212+
* Checks whether native statistics collection is enabled in this cache.
213+
* <p>
214+
* The default value is <code>false</code>.
215+
*
216+
* @return true if native statistics collection is enabled
217+
*/
218+
public boolean isNativeStatisticsEnabled() {
219+
return nativeStatistics;
220+
}
221+
222+
/**
223+
* Sets whether native statistics gathering is enabled on a cache.
224+
*
225+
* @param enabled true to enable native statistics, false to disable.
226+
*/
227+
public void setNativeStatisticsEnabled(boolean enabled) {
228+
this.nativeStatistics = enabled;
229+
}
230+
208231
@Override
209232
public boolean isStatisticsEnabled() {
210233
return delegate.isStatisticsEnabled();

jcache/src/main/java/com/github/benmanes/caffeine/jcache/configuration/TypesafeConfigurator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,9 @@ private void addWriteThrough() {
229229
}
230230
}
231231

232-
/** Adds the JMX monitoring settings. */
232+
/** Adds the monitoring settings. */
233233
private void addMonitoring() {
234+
configuration.setNativeStatisticsEnabled(merged.getBoolean("monitoring.native-statistics"));
234235
configuration.setStatisticsEnabled(merged.getBoolean("monitoring.statistics"));
235236
configuration.setManagementEnabled(merged.getBoolean("monitoring.management"));
236237
}

jcache/src/main/resources/reference.conf

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,15 @@ caffeine.jcache {
4646
writer = null
4747
}
4848

49-
# The JMX monitoring configuration
49+
# The monitoring configuration
5050
monitoring {
51-
# If cache statistics should be recorded and externalized
51+
# If Caffeine's statistics should be recorded (manually externalized via Cache#unwrap)
52+
native-statistics = false
53+
54+
# If JCache statistics should be recorded and externalized via JMX
5255
statistics = false
5356

54-
# If the configuration should be externalized
57+
# If the configuration should be externalized via JMX
5558
management = false
5659
}
5760

jcache/src/test/java/com/github/benmanes/caffeine/jcache/configuration/TypesafeConfigurationTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public void testCache2() {
8383

8484
assertThat(config2.get().getKeyType(), is(String.class));
8585
assertThat(config2.get().getValueType(), is(Integer.class));
86+
assertThat(config2.get().isNativeStatisticsEnabled(), is(false));
8687
assertThat(config2.get().getExecutorFactory().create(), is(ForkJoinPool.commonPool()));
8788
}
8889

@@ -107,6 +108,7 @@ static void checkTestCache(CaffeineConfiguration<?, ?> config) {
107108
assertThat(config.getExecutorFactory().create(), is(instanceOf(TestExecutor.class)));
108109
assertThat(config.getCacheLoaderFactory().create(), is(instanceOf(TestCacheLoader.class)));
109110
assertThat(config.getCacheWriter(), is(instanceOf(TestCacheWriter.class)));
111+
assertThat(config.isNativeStatisticsEnabled(), is(true));
110112
assertThat(config.isStatisticsEnabled(), is(true));
111113
assertThat(config.isManagementEnabled(), is(true));
112114

jcache/src/test/resources/application.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ caffeine.jcache {
2424
monitoring {
2525
statistics = true
2626
management = true
27+
native-statistics = true
2728
}
2829

2930
policy {

0 commit comments

Comments
 (0)