CaffeineMetricSupport { public static void setup(MeterRegistry registry, MeterIdPrefix idPrefix, Cache<?, ?> cache) { setup(registry, idPrefix, cache, Ticker.systemTicker()); } private  CaffeineMetricSupport(); static void setup(MeterRegistry registry, MeterIdPrefix idPrefix, Cache<?, ?> cache); static void setup(MeterRegistry registry, MeterIdPrefix idPrefix, Cache<?, ?> cache, Ticker ticker);  }
@Test public void test() { final MockLoadingCache cache = new MockLoadingCache(1, 2, 3, 4, 5, 6, 7, 8); final AtomicLong ticker = new AtomicLong(); final MeterRegistry registry = PrometheusMeterRegistries.newRegistry(); CaffeineMetricSupport.setup(registry, new MeterIdPrefix("foo"), cache, ticker::get); assertThat(registry.getMeters()).isNotEmpty(); assertThat(cache.statsCalls()).isOne(); assertThat(cache.estimatedSizeCalls()).isOne(); assertThat(MoreMeters.measureAll(registry)) .containsEntry("foo.requests#count{result=hit}", 1.0) .containsEntry("foo.requests#count{result=miss}", 2.0) .containsEntry("foo.loads#count{result=success}", 3.0) .containsEntry("foo.loads#count{result=failure}", 4.0) .containsEntry("foo.load.duration#count", 5.0) .containsEntry("foo.evictions#count", 6.0) .containsEntry("foo.eviction.weight#count", 7.0) .containsEntry("foo.estimated.size#value", 8.0); assertThat(cache.statsCalls()).isOne(); assertThat(cache.estimatedSizeCalls()).isOne(); ticker.addAndGet(CaffeineMetricSupport.UPDATE_INTERVAL_NANOS); cache.update(9, 10, 11, 12, 13, 14, 15, 16); assertThat(MoreMeters.measureAll(registry)) .containsEntry("foo.requests#count{result=hit}", 9.0) .containsEntry("foo.requests#count{result=miss}", 10.0) .containsEntry("foo.loads#count{result=success}", 11.0) .containsEntry("foo.loads#count{result=failure}", 12.0) .containsEntry("foo.load.duration#count", 13.0) .containsEntry("foo.evictions#count", 14.0) .containsEntry("foo.eviction.weight#count", 15.0) .containsEntry("foo.estimated.size#value", 16.0); assertThat(cache.statsCalls()).isEqualTo(2); assertThat(cache.estimatedSizeCalls()).isEqualTo(2); }
@Test public void testNonLoadingCache() { final MockCache cache = new MockCache(1, 2, 3, 4, 5); final AtomicLong ticker = new AtomicLong(); final MeterRegistry registry = PrometheusMeterRegistries.newRegistry(); CaffeineMetricSupport.setup(registry, new MeterIdPrefix("bar"), cache, ticker::get); assertThat(cache.statsCalls()).isOne(); assertThat(cache.estimatedSizeCalls()).isOne(); assertThat(MoreMeters.measureAll(registry)) .containsEntry("bar.requests#count{result=hit}", 1.0) .containsEntry("bar.requests#count{result=miss}", 2.0) .containsEntry("bar.evictions#count", 3.0) .containsEntry("bar.eviction.weight#count", 4.0) .containsEntry("bar.estimated.size#value", 5.0); assertThat(MoreMeters.measureAll(registry)).doesNotContainKeys( "bar.loads#count{result=success}", "bar.loads#count{result=failure}", "bar.load.duration#count"); }
@Test public void aggregation() { final MockLoadingCache cache1 = new MockLoadingCache(1, 2, 3, 4, 5, 6, 7, 8); final MockLoadingCache cache2 = new MockLoadingCache(9, 10, 11, 12, 13, 14, 15, 16); final MeterRegistry registry = PrometheusMeterRegistries.newRegistry(); final MeterIdPrefix idPrefix = new MeterIdPrefix("baz"); CaffeineMetricSupport.setup(registry, idPrefix, cache1); CaffeineMetricSupport.setup(registry, idPrefix, cache2); assertThat(MoreMeters.measureAll(registry)) .containsEntry("baz.requests#count{result=hit}", 10.0) .containsEntry("baz.requests#count{result=miss}", 12.0) .containsEntry("baz.loads#count{result=success}", 14.0) .containsEntry("baz.loads#count{result=failure}", 16.0) .containsEntry("baz.load.duration#count", 18.0) .containsEntry("baz.evictions#count", 20.0) .containsEntry("baz.eviction.weight#count", 22.0) .containsEntry("baz.estimated.size#value", 24.0); }
@Test public void aggregationAfterGC() throws Exception { final MockCache cache1 = new MockCache(1, 2, 3, 4, 5); Object cache2 = new MockLoadingCache(6, 7, 8, 9, 10, 11, 12, 13); final MeterRegistry registry = PrometheusMeterRegistries.newRegistry(); final AtomicLong ticker = new AtomicLong(); final MeterIdPrefix idPrefix = new MeterIdPrefix("baz"); CaffeineMetricSupport.setup(registry, idPrefix, cache1, ticker::get); CaffeineMetricSupport.setup(registry, idPrefix, (Cache<?, ?>) cache2, ticker::get); assertThat(MoreMeters.measureAll(registry)) .containsEntry("baz.requests#count{result=hit}", 7.0) .containsEntry("baz.requests#count{result=miss}", 9.0) .containsEntry("baz.loads#count{result=success}", 8.0) .containsEntry("baz.loads#count{result=failure}", 9.0) .containsEntry("baz.load.duration#count", 10.0) .containsEntry("baz.evictions#count", 14.0) .containsEntry("baz.eviction.weight#count", 16.0) .containsEntry("baz.estimated.size#value", 18.0); ticker.addAndGet(CaffeineMetricSupport.UPDATE_INTERVAL_NANOS); cache2 = new WeakReference<>(cache2); System.gc(); Thread.sleep(1000); assertThat(((Reference<?>) cache2).get()).isNull(); assertThat(MoreMeters.measureAll(registry)) .containsEntry("baz.requests#count{result=hit}", 7.0) .containsEntry("baz.requests#count{result=miss}", 9.0) .containsEntry("baz.loads#count{result=success}", 8.0) .containsEntry("baz.loads#count{result=failure}", 9.0) .containsEntry("baz.load.duration#count", 10.0) .containsEntry("baz.evictions#count", 14.0) .containsEntry("baz.eviction.weight#count", 16.0) .containsEntry("baz.estimated.size#value", 5.0); }
@Test public void sameCacheTwice() { final MockCache cache = new MockCache(1, 2, 3, 4, 5); final MeterRegistry registry = PrometheusMeterRegistries.newRegistry(); final MeterIdPrefix idPrefix = new MeterIdPrefix("baz"); CaffeineMetricSupport.setup(registry, idPrefix, cache); CaffeineMetricSupport.setup(registry, idPrefix, cache); assertThat(MoreMeters.measureAll(registry)) .containsEntry("baz.requests#count{result=hit}", 1.0) .containsEntry("baz.requests#count{result=miss}", 2.0) .containsEntry("baz.evictions#count", 3.0) .containsEntry("baz.eviction.weight#count", 4.0) .containsEntry("baz.estimated.size#value", 5.0); }
@Test public void notRecording() { when(policy.isRecordingStats()).thenReturn(false); final MockLoadingCache cache = new MockLoadingCache(1, 2, 3, 4, 5, 6, 7, 8); final AtomicLong ticker = new AtomicLong(); final MeterRegistry registry = PrometheusMeterRegistries.newRegistry(); CaffeineMetricSupport.setup(registry, new MeterIdPrefix("foo"), cache, ticker::get); assertThat(registry.getMeters()).isEmpty(); }