All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yammer.metrics.ehcache.InstrumentedEhcache Maven / Gradle / Ivy

package com.yammer.metrics.ehcache;

import com.yammer.metrics.Metrics;
import com.yammer.metrics.core.Gauge;
import com.yammer.metrics.core.Timer;
import net.sf.ehcache.CacheException;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import net.sf.ehcache.Statistics;
import net.sf.ehcache.constructs.EhcacheDecoratorAdapter;

import java.io.Serializable;
import java.util.concurrent.TimeUnit;

/**
 * An instrumented {@link Ehcache} instance.
 */
public class InstrumentedEhcache extends EhcacheDecoratorAdapter {
    /**
     * Instruments the given {@link Ehcache} instance with get and put timers
     * and a set of gauges for Ehcache's built-in statistics:
     * 

*

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
{@code hits}The number of times a requested item was found in the * cache.
{@code in-memory-hits}Number of times a requested item was found in the memory * store.
{@code off-heap-hits}Number of times a requested item was found in the off-heap * store.
{@code on-disk-hits}Number of times a requested item was found in the disk * store.
{@code misses}Number of times a requested item was not found in the * cache.
{@code in-memory-misses}Number of times a requested item was not found in the memory * store.
{@code off-heap-misses}Number of times a requested item was not found in the * off-heap store.
{@code on-disk-misses}Number of times a requested item was not found in the disk * store.
{@code objects}Number of elements stored in the cache.
{@code in-memory-objects}Number of objects in the memory store.
{@code off-heap-objects}Number of objects in the off-heap store.
{@code on-disk-objects}Number of objects in the disk store.
{@code mean-get-time}The average get time. Because ehcache support JDK1.4.2, each * get time uses {@link System#currentTimeMillis()}, rather than * nanoseconds. The accuracy is thus limited.
{@code mean-search-time}The average execution time (in milliseconds) within the last * sample period.
{@code eviction-count}The number of cache evictions, since the cache was created, * or statistics were cleared.
{@code searches-per-second}The number of search executions that have completed in the * last second.
{@code accuracy}A human readable description of the accuracy setting. One of * "None", "Best Effort" or "Guaranteed".
* * N.B.: This enables Ehcache's sampling statistics with an accuracy * level of "none." * * @param cache an {@link Ehcache} instance * @return an instrumented decorator for {@code cache} * @see Statistics */ public static Ehcache instrument(final Ehcache cache) { cache.setSampledStatisticsEnabled(true); cache.setStatisticsAccuracy(Statistics.STATISTICS_ACCURACY_NONE); Metrics.newGauge(cache.getClass(), "hits", cache.getName(), new Gauge() { @Override public Long value() { return cache.getStatistics().getCacheHits(); } }); Metrics.newGauge(cache.getClass(), "in-memory-hits", cache.getName(), new Gauge() { @Override public Long value() { return cache.getStatistics().getInMemoryHits(); } }); Metrics.newGauge(cache.getClass(), "off-heap-hits", cache.getName(), new Gauge() { @Override public Long value() { return cache.getStatistics().getOffHeapHits(); } }); Metrics.newGauge(cache.getClass(), "on-disk-hits", cache.getName(), new Gauge() { @Override public Long value() { return cache.getStatistics().getOnDiskHits(); } }); Metrics.newGauge(cache.getClass(), "misses", cache.getName(), new Gauge() { @Override public Long value() { return cache.getStatistics().getCacheMisses(); } }); Metrics.newGauge(cache.getClass(), "in-memory-misses", cache.getName(), new Gauge() { @Override public Long value() { return cache.getStatistics().getInMemoryMisses(); } }); Metrics.newGauge(cache.getClass(), "off-heap-misses", cache.getName(), new Gauge() { @Override public Long value() { return cache.getStatistics().getOffHeapMisses(); } }); Metrics.newGauge(cache.getClass(), "on-disk-misses", cache.getName(), new Gauge() { @Override public Long value() { return cache.getStatistics().getOnDiskMisses(); } }); Metrics.newGauge(cache.getClass(), "objects", cache.getName(), new Gauge() { @Override public Long value() { return cache.getStatistics().getObjectCount(); } }); Metrics.newGauge(cache.getClass(), "in-memory-objects", cache.getName(), new Gauge() { @Override public Long value() { return cache.getStatistics().getMemoryStoreObjectCount(); } }); Metrics.newGauge(cache.getClass(), "off-heap-objects", cache.getName(), new Gauge() { @Override public Long value() { return cache.getStatistics().getOffHeapStoreObjectCount(); } }); Metrics.newGauge(cache.getClass(), "on-disk-objects", cache.getName(), new Gauge() { @Override public Long value() { return cache.getStatistics().getDiskStoreObjectCount(); } }); Metrics.newGauge(cache.getClass(), "mean-get-time", cache.getName(), new Gauge() { @Override public Float value() { return cache.getStatistics().getAverageGetTime(); } }); Metrics.newGauge(cache.getClass(), "mean-search-time", cache.getName(), new Gauge() { @Override public Long value() { return cache.getStatistics().getAverageSearchTime(); } }); Metrics.newGauge(cache.getClass(), "eviction-count", cache.getName(), new Gauge() { @Override public Long value() { return cache.getStatistics().getEvictionCount(); } }); Metrics.newGauge(cache.getClass(), "searches-per-second", cache.getName(), new Gauge() { @Override public Long value() { return cache.getStatistics().getSearchesPerSecond(); } }); Metrics.newGauge(cache.getClass(), "writer-queue-size", cache.getName(), new Gauge() { @Override public Long value() { return cache.getStatistics().getWriterQueueSize(); } }); Metrics.newGauge(cache.getClass(), "accuracy", cache.getName(), new Gauge() { @Override public String value() { return cache.getStatistics().getStatisticsAccuracyDescription(); } }); return new InstrumentedEhcache(cache); } private final Timer getTimer, putTimer; private InstrumentedEhcache(Ehcache cache) { super(cache); this.getTimer = Metrics.newTimer(cache.getClass(), "get", cache.getName(), TimeUnit.MICROSECONDS, TimeUnit.SECONDS); this.putTimer = Metrics.newTimer(cache.getClass(), "put", cache.getName(), TimeUnit.MICROSECONDS, TimeUnit.SECONDS); } @Override public Element get(Object key) throws IllegalStateException, CacheException { final long start = System.nanoTime(); try { return underlyingCache.get(key); } finally { getTimer.update(System.nanoTime() - start, TimeUnit.NANOSECONDS); } } @Override public Element get(Serializable key) throws IllegalStateException, CacheException { final long start = System.nanoTime(); try { return underlyingCache.get(key); } finally { getTimer.update(System.nanoTime() - start, TimeUnit.NANOSECONDS); } } @Override public void put(Element element) throws IllegalArgumentException, IllegalStateException, CacheException { final long start = System.nanoTime(); try { underlyingCache.put(element); } finally { putTimer.update(System.nanoTime() - start, TimeUnit.NANOSECONDS); } } @Override public void put(Element element, boolean doNotNotifyCacheReplicators) throws IllegalArgumentException, IllegalStateException, CacheException { final long start = System.nanoTime(); try { underlyingCache.put(element, doNotNotifyCacheReplicators); } finally { putTimer.update(System.nanoTime() - start, TimeUnit.NANOSECONDS); } } @Override public Element putIfAbsent(Element element) throws NullPointerException { final long start = System.nanoTime(); try { return underlyingCache.putIfAbsent(element); } finally { putTimer.update(System.nanoTime() - start, TimeUnit.NANOSECONDS); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy