Codahale Metrics is a wonderful library to collect and report various types of metrics from your Java server-side applications. Google Guava is the swiss army knife of libraries that every Java developer should have in their toolbox.

If you use both these libraries, then at some point of time, you may find the need to capture statistics from Guava Caches and report them via Metrics. When you do, perhaps this class would be useful.

packagenet.antrix.utils;importstaticcom.codahale.metrics.MetricRegistry.name;importjava.util.HashMap;importjava.util.Map;importcom.codahale.metrics.Gauge;importcom.codahale.metrics.Metric;importcom.codahale.metrics.MetricSet;importcom.google.common.cache.Cache;publicclassGuavaCacheMetricsextendsHashMap<String,Metric>implementsMetricSet{/** * Wraps the provided Guava cache's statistics into Gauges suitable for reporting via Codahale Metrics * <p/> * The returned MetricSet is suitable for registration with a MetricRegistry like so: * <p/> * <code>registry.registerAll( GuavaCacheMetrics.metricsFor( "MyCache", cache ) );</code> * * @param cacheName This will be prefixed to all the reported metrics * @param cache The cache from which to report the statistics * @return MetricSet suitable for registration with a MetricRegistry */publicstaticMetricSetmetricsFor(StringcacheName,finalCachecache){GuavaCacheMetricsmetrics=newGuavaCacheMetrics();metrics.put(name(cacheName,"hitRate"),newGauge<Double>(){@OverridepublicDoublegetValue(){returncache.stats().hitRate();}});metrics.put(name(cacheName,"hitCount"),newGauge<Long>(){@OverridepublicLonggetValue(){returncache.stats().hitCount();}});metrics.put(name(cacheName,"missCount"),newGauge<Long>(){@OverridepublicLonggetValue(){returncache.stats().missCount();}});metrics.put(name(cacheName,"loadExceptionCount"),newGauge<Long>(){@OverridepublicLonggetValue(){returncache.stats().loadExceptionCount();}});metrics.put(name(cacheName,"evictionCount"),newGauge<Long>(){@OverridepublicLonggetValue(){returncache.stats().evictionCount();}});returnmetrics;}privateGuavaCacheMetrics(){}@OverridepublicMap<String,Metric>getMetrics(){returnthis;}}

packagenet.antrix.utilsimportcom.codahale.metrics.MetricRegistryimportcom.google.common.cache.Cacheimportcom.google.common.cache.CacheBuilderimportspock.lang.SpecificationclassGuavaCacheMetricsTestextendsSpecification{def"ensure guava cache metrics are reported to the registry"(){given:"a guava cache registered with a Metrics registry"defcache=CacheBuilder.newBuilder().recordStats().build()defregistry=newMetricRegistry()registry.registerAll(GuavaCacheMetrics.metricsFor("MyCache",cache))when:"various read/write operations are performed on the cache"cache.put("k1","v1")cache.put("k2","v2")cache.getIfPresent("k1")cache.getIfPresent("k2")cache.getIfPresent("k3")cache.get("k4",{"v4"})try{cache.get"k5",{thrownewException()}}catch(Exceptionexpected){}then:"the metrics registry records them correctly"defgauges=registry.gauges2==gauges["MyCache.hitCount"].value3==gauges["MyCache.missCount"].value0.4==gauges["MyCache.hitRate"].value1==gauges["MyCache.loadExceptionCount"].value0==gauges["MyCache.evictionCount"].value}}