对于可以使用静态方法访问的单个缓存,这相当容易。在自定义探测向导中,将元数据脚本设置为
metaData.recordOnStartup(true);
metaData.telemetry(true);
metaData.addCustomTelemetry("Request count", Unit.PLAIN, 1f);
metaData.addCustomTelemetry("Hit count", Unit.PLAIN, 1f);
metaData.addCustomTelemetry("Hit rate", Unit.PERCENT, 1f);
metaData.addCustomTelemetry("Miss count", Unit.PLAIN, 1f);
metaData.addCustomTelemetry("Miss rate", Unit.PERCENT, 1f);
metaData.addCustomTelemetry("Load success count", Unit.PLAIN, 1f);
metaData.addCustomTelemetry("Load exception count", Unit.PLAIN, 1f);
metaData.addCustomTelemetry("Load exception rate", Unit.PERCENT, 1f);
metaData.addCustomTelemetry("Eviction count", Unit.PLAIN, 1f);
metaData.addCustomTelemetry("Total load time", Unit.MICROSECONDS, 1f);
metaData.addCustomTelemetry("Average load penalty", Unit.MICROSECONDS, 1f);
和遥测脚本
import test.CacheTest;
import com.google.common.cache.*;
Cache cache = CacheTest.getSingleCache();
if (cache == null) {
return;
}
CacheStats stats = cache.stats();
data[0] = (int)stats.requestCount();
data[1] = (int)stats.hitCount();
data[2] = (int)(stats.hitRate() * 100);
data[3] = (int)stats.missCount();
data[4] = (int)(stats.missRate() * 100);
data[5] = (int)stats.loadSuccessCount();
data[6] = (int)stats.loadExceptionCount();
data[7] = (int)(stats.loadExceptionRate() * 100);
data[8] = (int)stats.evictionCount();
data[9] = (int)stats.totalLoadTime() / 1000;
data[10] = (int)stats.averageLoadPenalty() / 1000;
CacheTest.getSingleCache()
获取缓存的钩子在哪里。
这将为您提供所有缓存统计测量的遥测数据,如下面的屏幕截图所示: