com.yammer.metrics.ehcache.InstrumentedEhcache Maven / Gradle / Ivy
package com.yammer.metrics.ehcache;
import java.beans.PropertyChangeListener;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import net.sf.ehcache.CacheException;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import net.sf.ehcache.Statistics;
import net.sf.ehcache.Status;
import net.sf.ehcache.bootstrap.BootstrapCacheLoader;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.event.RegisteredEventListeners;
import net.sf.ehcache.exceptionhandler.CacheExceptionHandler;
import net.sf.ehcache.extension.CacheExtension;
import net.sf.ehcache.loader.CacheLoader;
import net.sf.ehcache.search.Attribute;
import net.sf.ehcache.search.Query;
import net.sf.ehcache.statistics.CacheUsageListener;
import net.sf.ehcache.statistics.LiveCacheStatistics;
import net.sf.ehcache.statistics.sampled.SampledCacheStatistics;
import net.sf.ehcache.terracotta.TerracottaNotRunningException;
import net.sf.ehcache.transaction.manager.TransactionManagerLookup;
import net.sf.ehcache.writer.CacheWriter;
import net.sf.ehcache.writer.CacheWriterManager;
import com.yammer.metrics.Metrics;
import com.yammer.metrics.core.GaugeMetric;
import com.yammer.metrics.core.TimerMetric;
/**
* An instrumented {@link Ehcache} instance.
*/
@SuppressWarnings({"deprecation", "CloneDoesntCallSuperClone"})
public class InstrumentedEhcache implements Ehcache {
/**
* 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 GaugeMetric() {
@Override
public Long value() {
return cache.getStatistics().getCacheHits();
}
});
Metrics.newGauge(cache.getClass(), "in-memory-hits", cache.getName(), new GaugeMetric() {
@Override
public Long value() {
return cache.getStatistics().getInMemoryHits();
}
});
Metrics.newGauge(cache.getClass(), "off-heap-hits", cache.getName(), new GaugeMetric() {
@Override
public Long value() {
return cache.getStatistics().getOffHeapHits();
}
});
Metrics.newGauge(cache.getClass(), "on-disk-hits", cache.getName(), new GaugeMetric() {
@Override
public Long value() {
return cache.getStatistics().getOnDiskHits();
}
});
Metrics.newGauge(cache.getClass(), "misses", cache.getName(), new GaugeMetric() {
@Override
public Long value() {
return cache.getStatistics().getCacheMisses();
}
});
Metrics.newGauge(cache.getClass(), "in-memory-misses", cache.getName(), new GaugeMetric() {
@Override
public Long value() {
return cache.getStatistics().getInMemoryMisses();
}
});
Metrics.newGauge(cache.getClass(), "off-heap-misses", cache.getName(), new GaugeMetric() {
@Override
public Long value() {
return cache.getStatistics().getOffHeapMisses();
}
});
Metrics.newGauge(cache.getClass(), "on-disk-misses", cache.getName(), new GaugeMetric() {
@Override
public Long value() {
return cache.getStatistics().getOnDiskMisses();
}
});
Metrics.newGauge(cache.getClass(), "objects", cache.getName(), new GaugeMetric() {
@Override
public Long value() {
return cache.getStatistics().getObjectCount();
}
});
Metrics.newGauge(cache.getClass(), "in-memory-objects", cache.getName(), new GaugeMetric() {
@Override
public Long value() {
return cache.getStatistics().getMemoryStoreObjectCount();
}
});
Metrics.newGauge(cache.getClass(), "off-heap-objects", cache.getName(), new GaugeMetric() {
@Override
public Long value() {
return cache.getStatistics().getOffHeapStoreObjectCount();
}
});
Metrics.newGauge(cache.getClass(), "on-disk-objects", cache.getName(), new GaugeMetric() {
@Override
public Long value() {
return cache.getStatistics().getDiskStoreObjectCount();
}
});
Metrics.newGauge(cache.getClass(), "mean-get-time", cache.getName(), new GaugeMetric() {
@Override
public Float value() {
return cache.getStatistics().getAverageGetTime();
}
});
Metrics.newGauge(cache.getClass(), "mean-search-time", cache.getName(), new GaugeMetric() {
@Override
public Long value() {
return cache.getStatistics().getAverageSearchTime();
}
});
Metrics.newGauge(cache.getClass(), "eviction-count", cache.getName(), new GaugeMetric() {
@Override
public Long value() {
return cache.getStatistics().getEvictionCount();
}
});
Metrics.newGauge(cache.getClass(), "searches-per-second", cache.getName(), new GaugeMetric() {
@Override
public Long value() {
return cache.getStatistics().getSearchesPerSecond();
}
});
Metrics.newGauge(cache.getClass(), "writer-queue-size", cache.getName(), new GaugeMetric() {
@Override
public Long value() {
return cache.getStatistics().getWriterQueueSize();
}
});
Metrics.newGauge(cache.getClass(), "accuracy", cache.getName(), new GaugeMetric() {
@Override
public String value() {
return cache.getStatistics().getStatisticsAccuracyDescription();
}
});
return new InstrumentedEhcache(cache);
}
private final TimerMetric getTimer, putTimer;
private final Ehcache cache;
private InstrumentedEhcache(Ehcache cache) {
this.cache = 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 void unregisterCacheLoader(CacheLoader cacheLoader) {
cache.unregisterCacheLoader(cacheLoader);
}
@Override
public void acquireReadLockOnKey(Object key) {
cache.acquireReadLockOnKey(key);
}
@Override
public void acquireWriteLockOnKey(Object key) {
cache.acquireWriteLockOnKey(key);
}
@Override
public void addPropertyChangeListener(PropertyChangeListener listener) {
cache.addPropertyChangeListener(listener);
}
@Override
public void bootstrap() {
cache.bootstrap();
}
@Override
public long calculateInMemorySize() throws IllegalStateException, CacheException {
return cache.calculateInMemorySize();
}
@Override
public long calculateOffHeapSize() throws IllegalStateException, CacheException {
return cache.calculateOffHeapSize();
}
@Override
public void clearStatistics() {
cache.clearStatistics();
}
@Override
public Object clone() throws CloneNotSupportedException {
return InstrumentedEhcache.instrument((Ehcache) cache.clone());
}
@Override
public Query createQuery() {
return cache.createQuery();
}
@Override
public void disableDynamicFeatures() {
cache.disableDynamicFeatures();
}
@Override
public void dispose() throws IllegalStateException {
cache.dispose();
}
@Override
public void evictExpiredElements() {
cache.evictExpiredElements();
}
@Override
public void flush() throws IllegalStateException, CacheException {
cache.flush();
}
@Override
public Element get(Object key) throws IllegalStateException, CacheException {
final long start = System.nanoTime();
try {
return cache.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 cache.get(key);
} finally {
getTimer.update(System.nanoTime() - start, TimeUnit.NANOSECONDS);
}
}
@Override
public Map getAllWithLoader(Collection keys, Object loaderArgument) throws CacheException {
return cache.getAllWithLoader(keys, loaderArgument);
}
@Override
public float getAverageGetTime() {
return cache.getAverageGetTime();
}
@Override
public long getAverageSearchTime() {
return cache.getAverageSearchTime();
}
@Override
public BootstrapCacheLoader getBootstrapCacheLoader() {
return cache.getBootstrapCacheLoader();
}
@Override
public CacheConfiguration getCacheConfiguration() {
return cache.getCacheConfiguration();
}
@Override
public RegisteredEventListeners getCacheEventNotificationService() {
return cache.getCacheEventNotificationService();
}
@Override
public CacheExceptionHandler getCacheExceptionHandler() {
return cache.getCacheExceptionHandler();
}
@Override
public CacheManager getCacheManager() {
return cache.getCacheManager();
}
@Override
public int getDiskStoreSize() throws IllegalStateException {
return cache.getDiskStoreSize();
}
@Override
public String getGuid() {
return cache.getGuid();
}
@Override
public Object getInternalContext() {
return cache.getInternalContext();
}
@Override
public List getKeys() throws IllegalStateException, CacheException {
return cache.getKeys();
}
@Override
public List getKeysNoDuplicateCheck() throws IllegalStateException {
return cache.getKeysNoDuplicateCheck();
}
@Override
public List getKeysWithExpiryCheck() throws IllegalStateException, CacheException {
return cache.getKeysWithExpiryCheck();
}
@Override
public LiveCacheStatistics getLiveCacheStatistics() throws IllegalStateException {
return cache.getLiveCacheStatistics();
}
@Override
public long getMemoryStoreSize() throws IllegalStateException {
return cache.getMemoryStoreSize();
}
@Override
public String getName() {
return cache.getName();
}
@Override
public long getOffHeapStoreSize() throws IllegalStateException {
return cache.getOffHeapStoreSize();
}
@Override
public Element getQuiet(Object key) throws IllegalStateException, CacheException {
return cache.getQuiet(key);
}
@Override
public Element getQuiet(Serializable key) throws IllegalStateException, CacheException {
return cache.getQuiet(key);
}
@Override
public List getRegisteredCacheExtensions() {
return cache.getRegisteredCacheExtensions();
}
@Override
public List getRegisteredCacheLoaders() {
return cache.getRegisteredCacheLoaders();
}
@Override
public CacheWriter getRegisteredCacheWriter() {
return cache.getRegisteredCacheWriter();
}
@Override
public SampledCacheStatistics getSampledCacheStatistics() {
return cache.getSampledCacheStatistics();
}
@Override
public Attribute getSearchAttribute(String attributeName) throws CacheException {
return cache.getSearchAttribute(attributeName);
}
@Override
public long getSearchesPerSecond() {
return cache.getSearchesPerSecond();
}
@Override
public int getSize() throws IllegalStateException, CacheException {
return cache.getSize();
}
@Override
public int getSizeBasedOnAccuracy(int statisticsAccuracy) throws IllegalArgumentException, IllegalStateException, CacheException {
return cache.getSizeBasedOnAccuracy(statisticsAccuracy);
}
@Override
public Statistics getStatistics() throws IllegalStateException {
return cache.getStatistics();
}
@Override
public int getStatisticsAccuracy() {
return cache.getStatisticsAccuracy();
}
@Override
public Status getStatus() {
return cache.getStatus();
}
@Override
public Element getWithLoader(Object key, CacheLoader loader, Object loaderArgument) throws CacheException {
return cache.getWithLoader(key, loader, loaderArgument);
}
@Override
public CacheWriterManager getWriterManager() {
return cache.getWriterManager();
}
@Override
public void initialise() {
cache.initialise();
}
@Override
public boolean isClusterBulkLoadEnabled() throws UnsupportedOperationException, TerracottaNotRunningException {
return cache.isClusterBulkLoadEnabled();
}
@Override
@Deprecated
public boolean isClusterCoherent() throws TerracottaNotRunningException {
return cache.isClusterCoherent();
}
@Override
public boolean isDisabled() {
return cache.isDisabled();
}
@Override
public boolean isElementInMemory(Object key) {
return cache.isElementInMemory(key);
}
@Override
public boolean isElementInMemory(Serializable key) {
return cache.isElementInMemory(key);
}
@Override
public boolean isElementOnDisk(Object key) {
return cache.isElementOnDisk(key);
}
@Override
public boolean isElementOnDisk(Serializable key) {
return cache.isElementOnDisk(key);
}
@Override
public boolean isExpired(Element element) throws IllegalStateException, NullPointerException {
return cache.isExpired(element);
}
@Override
public boolean isKeyInCache(Object key) {
return cache.isKeyInCache(key);
}
@Override
public boolean isNodeBulkLoadEnabled() throws UnsupportedOperationException, TerracottaNotRunningException {
return cache.isNodeBulkLoadEnabled();
}
@Override
@Deprecated
public boolean isNodeCoherent() throws TerracottaNotRunningException {
return cache.isNodeCoherent();
}
@Override
public boolean isReadLockedByCurrentThread(Object key) {
return cache.isReadLockedByCurrentThread(key);
}
@Override
public boolean isSampledStatisticsEnabled() {
return cache.isSampledStatisticsEnabled();
}
@Override
public boolean isSearchable() {
return cache.isSearchable();
}
@Override
public boolean isStatisticsEnabled() {
return cache.isStatisticsEnabled();
}
@Override
public boolean isValueInCache(Object value) {
return cache.isValueInCache(value);
}
@Override
public boolean isWriteLockedByCurrentThread(Object key) {
return cache.isWriteLockedByCurrentThread(key);
}
@Override
public void load(Object key) throws CacheException {
cache.load(key);
}
@Override
public void loadAll(Collection keys, Object argument) throws CacheException {
cache.loadAll(keys, argument);
}
@Override
public void unpinAll() {
cache.unpinAll();
}
@Override
public boolean isPinned(Object o) {
return cache.isPinned(o);
}
@Override
public void setPinned(Object o, boolean b) {
cache.setPinned(o, b);
}
@Override
public void putAll(Collection elements) throws IllegalArgumentException, IllegalStateException, CacheException {
cache.putAll(elements);
}
@Override
public Map © 2015 - 2025 Weber Informatics LLC | Privacy Policy