Skip to content

Commit

Permalink
Polish (#3375)
Browse files Browse the repository at this point in the history
  • Loading branch information
izeye committed Aug 29, 2022
1 parent ecada0e commit f7e58b6
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 67 deletions.
Expand Up @@ -32,7 +32,7 @@

public abstract class AbstractTimer extends AbstractMeter implements Timer {

private static Map<PauseDetector, Object> pauseDetectorCache = new ConcurrentHashMap<>();
private static final Map<PauseDetector, Object> pauseDetectorCache = new ConcurrentHashMap<>();

protected final Clock clock;

Expand Down
Expand Up @@ -78,7 +78,7 @@ default <T> T recordCallable(Callable<T> f) throws Exception {
}

/**
* Executes the callable {@code f} and records the time taken.
* Executes the supplier {@code f} and records the time taken.
* @param f Function to execute and measure the execution time.
* @param <T> The return type of the {@link Supplier}.
* @return The return value of {@code f}.
Expand Down
Expand Up @@ -38,6 +38,8 @@
@NonNullFields
public abstract class CacheMeterBinder<C> implements MeterBinder {

private static final String DESCRIPTION_CACHE_GETS = "The number of times cache lookup methods have returned a cached (hit) or uncached (newly loaded or null) value (miss).";

private final WeakReference<C> cacheRef;

private final Iterable<Tag> tags;
Expand Down Expand Up @@ -68,14 +70,11 @@ public final void bindTo(MeterRegistry registry) {
FunctionCounter.builder("cache.gets", cache, c -> {
Long misses = missCount();
return misses == null ? 0 : misses;
}).tags(tags).tag("result", "miss").description(
"The number of times cache lookup methods have returned a cached (hit) or uncached (newly loaded or null) value (miss).")
.register(registry);
}).tags(tags).tag("result", "miss").description(DESCRIPTION_CACHE_GETS).register(registry);
}

FunctionCounter.builder("cache.gets", cache, c -> hitCount()).tags(tags).tag("result", "hit").description(
"The number of times cache lookup methods have returned a cached (hit) or uncached (newly loaded or null) value (miss).")
.register(registry);
FunctionCounter.builder("cache.gets", cache, c -> hitCount()).tags(tags).tag("result", "hit")
.description(DESCRIPTION_CACHE_GETS).register(registry);

FunctionCounter.builder("cache.puts", cache, c -> putCount()).tags(tags)
.description("The number of entries added to the cache").register(registry);
Expand Down
Expand Up @@ -46,6 +46,8 @@
@NonNullFields
public class CaffeineCacheMetrics<K, V, C extends Cache<K, V>> extends CacheMeterBinder<C> {

private static final String DESCRIPTION_CACHE_LOAD = "The number of times cache lookup methods have successfully loaded a new value or failed to load a new value, either because no value was found or an exception was thrown while loading";

/**
* Creates a new {@link CaffeineCacheMetrics} instance.
* @param cache The cache to be instrumented. You must call
Expand Down Expand Up @@ -176,16 +178,10 @@ protected void bindImplementationSpecificMetrics(MeterRegistry registry) {
.register(registry);

FunctionCounter.builder("cache.load", cache, c -> c.stats().loadSuccessCount()).tags(getTagsWithCacheName())
.tags("result", "success")
.description(
"The number of times cache lookup methods have successfully loaded a new value or failed to load a new value, either because no value was found or an exception was thrown while loading")
.register(registry);
.tags("result", "success").description(DESCRIPTION_CACHE_LOAD).register(registry);

FunctionCounter.builder("cache.load", cache, c -> c.stats().loadFailureCount()).tags(getTagsWithCacheName())
.tags("result", "failure")
.description(
"The number of times cache lookup methods have successfully loaded a new value or failed to load a new value, either because no value was found or an exception was thrown while loading")
.register(registry);
.tags("result", "failure").description(DESCRIPTION_CACHE_LOAD).register(registry);
}
}

Expand Down
Expand Up @@ -55,6 +55,10 @@
@NonNullFields
public final class CaffeineStatsCounter implements StatsCounter {

private static final String DESCRIPTION_CACHE_GETS = "The number of times cache lookup methods have returned a cached (hit) or uncached (newly loaded) value (miss).";

private static final String DESCRIPTION_CACHE_LOADS = "The number of times cache lookup methods have successfully loaded a new value or failed to load a new value, either because no value was found or an exception was thrown while loading";

private final MeterRegistry registry;

private final Tags tags;
Expand Down Expand Up @@ -91,18 +95,14 @@ public CaffeineStatsCounter(MeterRegistry registry, String cacheName, Iterable<T
this.registry = registry;
this.tags = Tags.concat(extraTags, "cache", cacheName);

hitCount = Counter.builder("cache.gets").tag("result", "hit").tags(tags).description(
"The number of times cache lookup methods have returned a cached (hit) or uncached (newly loaded) value (miss).")
.register(registry);
missCount = Counter.builder("cache.gets").tag("result", "miss").tags(tags).description(
"The number of times cache lookup methods have returned a cached (hit) or uncached (newly loaded) value (miss).")
.register(registry);
loadSuccesses = Timer.builder("cache.loads").tag("result", "success").tags(tags).description(
"The number of times cache lookup methods have successfully loaded a new value or failed to load a new value, either because no value was found or an exception was thrown while loading")
hitCount = Counter.builder("cache.gets").tag("result", "hit").tags(tags).description(DESCRIPTION_CACHE_GETS)
.register(registry);
loadFailures = Timer.builder("cache.loads").tag("result", "failure").tags(tags).description(
"The number of times cache lookup methods have successfully loaded a new value or failed to load a new value, either because no value was found or an exception was thrown while loading")
missCount = Counter.builder("cache.gets").tag("result", "miss").tags(tags).description(DESCRIPTION_CACHE_GETS)
.register(registry);
loadSuccesses = Timer.builder("cache.loads").tag("result", "success").tags(tags)
.description(DESCRIPTION_CACHE_LOADS).register(registry);
loadFailures = Timer.builder("cache.loads").tag("result", "failure").tags(tags)
.description(DESCRIPTION_CACHE_LOADS).register(registry);

evictionMetrics = new EnumMap<>(RemovalCause.class);
Arrays.stream(RemovalCause.values())
Expand Down
Expand Up @@ -36,6 +36,16 @@
@NonNullFields
public class EhCache2Metrics extends CacheMeterBinder<Ehcache> {

private static final String DESCRIPTION_CACHE_PUTS_ADDED = "Cache puts (added or updated)";

private static final String DESCRIPTION_CACHE_MISSES = "The number of times cache lookup methods have not returned a value, due to expiry or because the key was not found";

private static final String DESCRIPTION_CACHE_XA_COMMITS = "The number of transaction commits";

private static final String DESCRIPTION_CACHE_XA_ROLLBACKS = "The number of transaction rollbacks";

private static final String DESCRIPTION_CACHE_XA_RECOVERIES = "The number of transaction recoveries";

public EhCache2Metrics(Ehcache cache, Iterable<Tag> tags) {
super(cache, cache.getName(), tags);
}
Expand Down Expand Up @@ -101,11 +111,11 @@ protected void bindImplementationSpecificMetrics(MeterRegistry registry) {
.tags(getTagsWithCacheName()).description("Cache removals").register(registry);

FunctionCounter.builder("cache.puts.added", stats, StatisticsGateway::cachePutAddedCount)
.tags(getTagsWithCacheName()).tags("result", "added").description("Cache puts (added or updated)")
.tags(getTagsWithCacheName()).tags("result", "added").description(DESCRIPTION_CACHE_PUTS_ADDED)
.register(registry);

FunctionCounter.builder("cache.puts.added", stats, StatisticsGateway::cachePutUpdatedCount)
.tags(getTagsWithCacheName()).tags("result", "updated").description("Cache puts (added or updated)")
.tags(getTagsWithCacheName()).tags("result", "updated").description(DESCRIPTION_CACHE_PUTS_ADDED)
.register(registry);

missMetrics(registry);
Expand Down Expand Up @@ -135,53 +145,49 @@ private StatisticsGateway getStats() {
private void missMetrics(MeterRegistry registry) {
StatisticsGateway stats = getStats();
FunctionCounter.builder("cache.misses", stats, StatisticsGateway::cacheMissExpiredCount)
.tags(getTagsWithCacheName()).tags("reason", "expired")
.description(
"The number of times cache lookup methods have not returned a value, due to expiry or because the key was not found")
.tags(getTagsWithCacheName()).tags("reason", "expired").description(DESCRIPTION_CACHE_MISSES)
.register(registry);

FunctionCounter.builder("cache.misses", stats, StatisticsGateway::cacheMissNotFoundCount)
.tags(getTagsWithCacheName()).tags("reason", "notFound")
.description(
"The number of times cache lookup methods have not returned a value, due to expiry or because the key was not found")
.tags(getTagsWithCacheName()).tags("reason", "notFound").description(DESCRIPTION_CACHE_MISSES)
.register(registry);
}

private void commitTransactionMetrics(MeterRegistry registry) {
StatisticsGateway stats = getStats();
FunctionCounter.builder("cache.xa.commits", stats, StatisticsGateway::xaCommitReadOnlyCount)
.tags(getTagsWithCacheName()).tags("result", "readOnly")
.description("The number of transaction commits").register(registry);
.tags(getTagsWithCacheName()).tags("result", "readOnly").description(DESCRIPTION_CACHE_XA_COMMITS)
.register(registry);

FunctionCounter.builder("cache.xa.commits", stats, StatisticsGateway::xaCommitExceptionCount)
.tags(getTagsWithCacheName()).tags("result", "exception")
.description("The number of transaction commits").register(registry);
.tags(getTagsWithCacheName()).tags("result", "exception").description(DESCRIPTION_CACHE_XA_COMMITS)
.register(registry);

FunctionCounter.builder("cache.xa.commits", stats, StatisticsGateway::xaCommitCommittedCount)
.tags(getTagsWithCacheName()).tags("result", "committed")
.description("The number of transaction commits").register(registry);
.tags(getTagsWithCacheName()).tags("result", "committed").description(DESCRIPTION_CACHE_XA_COMMITS)
.register(registry);
}

private void rollbackTransactionMetrics(MeterRegistry registry) {
StatisticsGateway stats = getStats();
FunctionCounter.builder("cache.xa.rollbacks", stats, StatisticsGateway::xaRollbackExceptionCount)
.tags(getTagsWithCacheName()).tags("result", "exception")
.description("The number of transaction rollbacks").register(registry);
.tags(getTagsWithCacheName()).tags("result", "exception").description(DESCRIPTION_CACHE_XA_ROLLBACKS)
.register(registry);

FunctionCounter.builder("cache.xa.rollbacks", stats, StatisticsGateway::xaRollbackSuccessCount)
.tags(getTagsWithCacheName()).tags("result", "success")
.description("The number of transaction rollbacks").register(registry);
.tags(getTagsWithCacheName()).tags("result", "success").description(DESCRIPTION_CACHE_XA_ROLLBACKS)
.register(registry);
}

private void recoveryTransactionMetrics(MeterRegistry registry) {
StatisticsGateway stats = getStats();
FunctionCounter.builder("cache.xa.recoveries", stats, StatisticsGateway::xaRecoveryNothingCount)
.tags(getTagsWithCacheName()).tags("result", "nothing")
.description("The number of transaction recoveries").register(registry);
.tags(getTagsWithCacheName()).tags("result", "nothing").description(DESCRIPTION_CACHE_XA_RECOVERIES)
.register(registry);

FunctionCounter.builder("cache.xa.recoveries", stats, StatisticsGateway::xaRecoveryRecoveredCount)
.tags(getTagsWithCacheName()).tags("result", "success")
.description("The number of transaction recoveries").register(registry);
.tags(getTagsWithCacheName()).tags("result", "success").description(DESCRIPTION_CACHE_XA_RECOVERIES)
.register(registry);
}

@Nullable
Expand Down
Expand Up @@ -34,6 +34,8 @@
@NonNullFields
public class GuavaCacheMetrics<K, V, C extends Cache<K, V>> extends CacheMeterBinder<C> {

private static final String DESCRIPTION_CACHE_LOAD = "The number of times cache lookup methods have successfully loaded a new value or failed to load a new value because an exception was thrown while loading";

/**
* Record metrics on a Guava cache. You must call {@link CacheBuilder#recordStats()}
* prior to building the cache for metrics to be recorded.
Expand Down Expand Up @@ -113,15 +115,10 @@ protected void bindImplementationSpecificMetrics(MeterRegistry registry) {
.register(registry);

FunctionCounter.builder("cache.load", cache, c -> c.stats().loadSuccessCount()).tags(getTagsWithCacheName())
.tags("result", "success")
.description(
"The number of times cache lookup methods have successfully loaded a new value or failed to load a new value because an exception was thrown while loading")
.register(registry);
.tags("result", "success").description(DESCRIPTION_CACHE_LOAD).register(registry);

FunctionCounter.builder("cache.load", cache, c -> c.stats().loadExceptionCount())
.tags(getTagsWithCacheName()).tags("result", "failure")
.description(
"The number of times cache lookup methods have successfully loaded a new value or failed to load a new value because an exception was thrown while loading")
.tags(getTagsWithCacheName()).tags("result", "failure").description(DESCRIPTION_CACHE_LOAD)
.register(registry);
}
}
Expand Down
Expand Up @@ -36,6 +36,12 @@
@NonNullFields
public class HazelcastCacheMetrics extends CacheMeterBinder<Object> {

private static final String DESCRIPTION_CACHE_ENTRIES = "The number of entries held by this member";

private static final String DESCRIPTION_CACHE_ENTRY_MEMORY = "Memory cost of entries held by this member";

private static final String DESCRIPTION_CACHE_NEAR_REQUESTS = "The number of requests (hits or misses) of near cache entries owned by this member";

private final HazelcastIMapAdapter cache;

/**
Expand Down Expand Up @@ -129,23 +135,23 @@ protected long putCount() {
protected void bindImplementationSpecificMetrics(MeterRegistry registry) {
Gauge.builder("cache.entries", cache,
cache -> getDouble(cache.getLocalMapStats(), LocalMapStats::getBackupEntryCount))
.tags(getTagsWithCacheName()).tag("ownership", "backup")
.description("The number of entries held by this member").register(registry);
.tags(getTagsWithCacheName()).tag("ownership", "backup").description(DESCRIPTION_CACHE_ENTRIES)
.register(registry);

Gauge.builder("cache.entries", cache,
cache -> getDouble(cache.getLocalMapStats(), LocalMapStats::getOwnedEntryCount))
.tags(getTagsWithCacheName()).tag("ownership", "owned")
.description("The number of entries held by this member").register(registry);
.tags(getTagsWithCacheName()).tag("ownership", "owned").description(DESCRIPTION_CACHE_ENTRIES)
.register(registry);

Gauge.builder("cache.entry.memory", cache,
cache -> getDouble(cache.getLocalMapStats(), LocalMapStats::getBackupEntryMemoryCost))
.tags(getTagsWithCacheName()).tag("ownership", "backup")
.description("Memory cost of entries held by this member").baseUnit(BaseUnits.BYTES).register(registry);
.tags(getTagsWithCacheName()).tag("ownership", "backup").description(DESCRIPTION_CACHE_ENTRY_MEMORY)
.baseUnit(BaseUnits.BYTES).register(registry);

Gauge.builder("cache.entry.memory", cache,
cache -> getDouble(cache.getLocalMapStats(), LocalMapStats::getOwnedEntryMemoryCost))
.tags(getTagsWithCacheName()).tag("ownership", "owned")
.description("Memory cost of entries held by this member").baseUnit(BaseUnits.BYTES).register(registry);
.tags(getTagsWithCacheName()).tag("ownership", "owned").description(DESCRIPTION_CACHE_ENTRY_MEMORY)
.baseUnit(BaseUnits.BYTES).register(registry);

FunctionCounter
.builder("cache.partition.gets", cache,
Expand All @@ -166,14 +172,12 @@ private void nearCacheMetrics(MeterRegistry registry) {
if (localMapStats != null && localMapStats.getNearCacheStats() != null) {
Gauge.builder("cache.near.requests", cache,
cache -> getDouble(cache.getLocalMapStats(), (stats) -> stats.getNearCacheStats().getHits()))
.tags(getTagsWithCacheName()).tag("result", "hit")
.description("The number of requests (hits or misses) of near cache entries owned by this member")
.tags(getTagsWithCacheName()).tag("result", "hit").description(DESCRIPTION_CACHE_NEAR_REQUESTS)
.register(registry);

Gauge.builder("cache.near.requests", cache,
cache -> getDouble(cache.getLocalMapStats(), (stats) -> stats.getNearCacheStats().getMisses()))
.tags(getTagsWithCacheName()).tag("result", "miss")
.description("The number of requests (hits or misses) of near cache entries owned by this member")
.tags(getTagsWithCacheName()).tag("result", "miss").description(DESCRIPTION_CACHE_NEAR_REQUESTS)
.register(registry);

Gauge.builder("cache.near.evictions", cache,
Expand Down

0 comments on commit f7e58b6

Please sign in to comment.