@SuppressWarnings("unchecked") @Test public void loadingCacheExposesMetricsForLoadsAndExceptions() throws Exception { CacheLoader<String, String> loader = mock(CacheLoader.class); when(loader.load(anyString())) .thenReturn("First User") .thenThrow(new RuntimeException("Seconds time fails")) .thenReturn("Third User"); LoadingCache<String, String> cache = Caffeine.newBuilder().recordStats().build(loader); CollectorRegistry registry = new CollectorRegistry(); CacheMetricsCollector collector = new CacheMetricsCollector().register(registry); collector.addCache("loadingusers", cache); cache.get("user1"); cache.get("user1"); try { cache.get("user2"); } catch (Exception e) { } cache.get("user3"); assertMetric(registry, "caffeine_cache_hit_total", "loadingusers", 1.0); assertMetric(registry, "caffeine_cache_miss_total", "loadingusers", 3.0); assertMetric(registry, "caffeine_cache_load_failure_total", "loadingusers", 1.0); assertMetric(registry, "caffeine_cache_loads_total", "loadingusers", 3.0); assertMetric(registry, "caffeine_cache_load_duration_seconds_count", "loadingusers", 3.0); assertMetricGreatThan(registry, "caffeine_cache_load_duration_seconds_sum", "loadingusers", 0.0); }
public void addCache(String cacheName, Cache cache) { children.put(cacheName, cache); }
CacheMetricsCollector extends Collector { public void addCache(String cacheName, Cache cache) { children.put(cacheName, cache); } }
CacheMetricsCollector extends Collector { public void addCache(String cacheName, Cache cache) { children.put(cacheName, cache); }  }
CacheMetricsCollector extends Collector { public void addCache(String cacheName, Cache cache) { children.put(cacheName, cache); }  void addCache(String cacheName, Cache cache); void addCache(String cacheName, AsyncCache cache); Cache removeCache(String cacheName); void clear(); @Override List<MetricFamilySamples> collect(); }
CacheMetricsCollector extends Collector { public void addCache(String cacheName, Cache cache) { children.put(cacheName, cache); }  void addCache(String cacheName, Cache cache); void addCache(String cacheName, AsyncCache cache); Cache removeCache(String cacheName); void clear(); @Override List<MetricFamilySamples> collect();  }