Skip to content

Commit

Permalink
Remove gauges & simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
schlosna committed Mar 7, 2024
1 parent 683c166 commit 2eb59bc
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 126 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,12 @@ Service instrumentedService = Tritium.instrument(Service.class,
import com.palantir.tritium.metrics.caffeine.CacheStats;

TaggedMetricRegistry taggedMetricRegistry = ...
CacheStats cacheStats = CacheStats.of(taggedMetricRegistry, "unique-cache-name");
Cache<Integer, String> cache = cacheStats.register(Caffeine.newBuilder()
.recordStats(cacheStats)
.recordStats(CacheStats.of(taggedMetricRegistry, "unique-cache-name"))
.build());

CacheStats loadingCacheStats = CacheStats.of(taggedMetricRegistry, "unique-loading-cache-name");
LoadingCache<String, Integer> loadingCache = loadingCacheStats.register(Caffeine.newBuilder()
.recordStats(loadingCacheStats)
.recordStats(CacheStats.of(taggedMetricRegistry, "unique-loading-cache-name"))
.build(key::length));
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
package com.palantir.tritium.metrics.caffeine;

import com.codahale.metrics.Counter;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.github.benmanes.caffeine.cache.Policy;
import com.github.benmanes.caffeine.cache.RemovalCause;
import com.github.benmanes.caffeine.cache.stats.StatsCounter;
import com.google.common.collect.ImmutableMap;
Expand All @@ -32,7 +29,6 @@
import org.checkerframework.checker.index.qual.NonNegative;

public final class CacheStats implements StatsCounter, Supplier<StatsCounter> {
private final CacheMetrics metrics;
private final String name;
private final Counter hitCounter;
private final Counter missCounter;
Expand Down Expand Up @@ -67,7 +63,6 @@ public static CacheStats of(CacheMetrics metrics, String name) {
}

private CacheStats(CacheMetrics metrics, String name) {
this.metrics = metrics;
this.name = name;
this.hitCounter = metrics.hitCount(name);
this.missCounter = metrics.missCount(name);
Expand All @@ -79,37 +74,6 @@ private CacheStats(CacheMetrics metrics, String name) {
.cache(name)
.cause(cause.toString())
.build()));
metrics.requestCount().cache(name).build(() -> hitCounter.getCount() + missCounter.getCount());
}

public <K, V, C extends Cache<K, V>> C register(C cache) {
metrics.estimatedSize().cache(name).build(cache::estimatedSize);
metrics.maximumSize().cache(name).build(() -> cache.policy()
.eviction()
.map(Policy.Eviction::getMaximum)
.orElse(-1L));
metrics.weightedSize().cache(name).build(() -> cache.policy()
.eviction()
.flatMap(e -> e.weightedSize().stream().boxed().findFirst())
.orElse(0L));
metrics.hitRatio().cache(name).build(() -> {
double hitCount = hitCounter.getCount();
return hitCount / (hitCount + missCounter.getCount());
});
metrics.missRatio().cache(name).build(() -> {
double missCount = missCounter.getCount();
return missCount / (hitCounter.getCount() + missCount);
});
metrics.loadAverageMillis()
.cache(name)
.build(() ->
// convert nanoseconds to milliseconds
totalLoadNanos.sum() / 1_000_000.0d);
return cache;
}

public <K, V> LoadingCache<K, V> register(LoadingCache<K, V> cache) {
return (LoadingCache<K, V>) register((Cache<K, V>) cache);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,97 +213,69 @@ void registerCacheWithoutRecordingStatsTagged() {

@Test
void registerTaggedMetrics() {
CacheStats cacheStats = CacheStats.of(taggedMetricRegistry, "test");
Cache<Integer, String> cache = cacheStats.register(
Caffeine.newBuilder().recordStats(cacheStats).maximumSize(2).build());
Cache<Integer, String> cache = Caffeine.newBuilder()
.recordStats(CacheStats.of(taggedMetricRegistry, "test"))
.maximumSize(2)
.build();
assertThat(taggedMetricRegistry.getMetrics().keySet())
.extracting(MetricName::safeName)
.contains(
"cache.estimated.size",
"cache.maximum.size",
"cache.weighted.size",
"cache.request.count",
"cache.hit.count",
"cache.hit.ratio",
"cache.miss.count",
"cache.miss.ratio",
"cache.eviction.count",
"cache.load.success.count",
"cache.load.failure.count",
"cache.load.average.millis");
"cache.load.failure.count");

await().atMost(Duration.ofSeconds(5)).untilAsserted(() -> {
assertGauge(taggedMetricRegistry, "cache.request.count").isEqualTo(0L);
assertCounter(taggedMetricRegistry, "cache.hit.count").isEqualTo(0L);
assertCounter(taggedMetricRegistry, "cache.miss.count").isEqualTo(0L);
});
assertCounter(taggedMetricRegistry, "cache.hit.count").isEqualTo(0L);
assertCounter(taggedMetricRegistry, "cache.miss.count").isEqualTo(0L);

assertThat(cache.get(0, mapping)).isEqualTo("0");
assertThat(cache.get(1, mapping)).isEqualTo("1");
assertThat(cache.get(2, mapping)).isEqualTo("2");
assertThat(cache.get(1, mapping)).isEqualTo("1");

await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
// await to avoid flakes as gauges may be memoized
assertGauge(taggedMetricRegistry, "cache.request.count").isEqualTo(4L);
assertCounter(taggedMetricRegistry, "cache.hit.count").isEqualTo(1L);
assertCounter(taggedMetricRegistry, "cache.miss.count").isEqualTo(3L);
assertGauge(taggedMetricRegistry, "cache.hit.ratio").isEqualTo(0.25);
assertCounter(taggedMetricRegistry, "cache.hit.count").isEqualTo(1L);
assertCounter(taggedMetricRegistry, "cache.miss.count").isEqualTo(3L);

assertThat(taggedMetricRegistry.getMetrics())
.extractingByKey(MetricName.builder()
.safeName("cache.stats.disabled")
.putSafeTags("cache", "test")
.build())
.isNull();
});
assertThat(taggedMetricRegistry.getMetrics())
.extractingByKey(MetricName.builder()
.safeName("cache.stats.disabled")
.putSafeTags("cache", "test")
.build())
.isNull();
}

@Test
void registerLoadingTaggedMetrics() {
CacheStats cacheStats = CacheStats.of(taggedMetricRegistry, "test");
LoadingCache<Integer, String> cache = cacheStats.register(
Caffeine.newBuilder().recordStats(cacheStats).maximumSize(2).build(mapping::apply));
LoadingCache<Integer, String> cache = Caffeine.newBuilder()
.recordStats(CacheStats.of(taggedMetricRegistry, "test"))
.maximumSize(2)
.build(mapping::apply);
assertThat(taggedMetricRegistry.getMetrics().keySet())
.extracting(MetricName::safeName)
.contains(
"cache.estimated.size",
"cache.maximum.size",
"cache.weighted.size",
"cache.request.count",
"cache.hit.count",
"cache.hit.ratio",
"cache.miss.count",
"cache.miss.ratio",
"cache.eviction.count",
"cache.load.success.count",
"cache.load.failure.count",
"cache.load.average.millis");
"cache.load.failure.count");

await().atMost(Duration.ofSeconds(5)).untilAsserted(() -> {
assertGauge(taggedMetricRegistry, "cache.request.count").isEqualTo(0L);
assertCounter(taggedMetricRegistry, "cache.hit.count").isEqualTo(0L);
assertCounter(taggedMetricRegistry, "cache.miss.count").isEqualTo(0L);
});
assertCounter(taggedMetricRegistry, "cache.hit.count").isEqualTo(0L);
assertCounter(taggedMetricRegistry, "cache.miss.count").isEqualTo(0L);

assertThat(cache.get(0)).isEqualTo("0");
assertThat(cache.get(1)).isEqualTo("1");
assertThat(cache.get(2)).isEqualTo("2");
assertThat(cache.get(1)).isEqualTo("1");

await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
// await to avoid flakes as gauges may be memoized
assertGauge(taggedMetricRegistry, "cache.request.count").isEqualTo(4L);
assertCounter(taggedMetricRegistry, "cache.hit.count").isEqualTo(1L);
assertCounter(taggedMetricRegistry, "cache.miss.count").isEqualTo(3L);
assertGauge(taggedMetricRegistry, "cache.hit.ratio").isEqualTo(0.25);
assertThat(taggedMetricRegistry.getMetrics())
.extractingByKey(MetricName.builder()
.safeName("cache.stats.disabled")
.putSafeTags("cache", "test")
.build())
.isNull();
});
assertCounter(taggedMetricRegistry, "cache.hit.count").isEqualTo(1L);
assertCounter(taggedMetricRegistry, "cache.miss.count").isEqualTo(3L);
assertThat(taggedMetricRegistry.getMetrics())
.extractingByKey(MetricName.builder()
.safeName("cache.stats.disabled")
.putSafeTags("cache", "test")
.build())
.isNull();
}

static AbstractObjectAssert<?, Object> assertGauge(TaggedMetricRegistry taggedMetricRegistry, String name) {
Expand Down
28 changes: 0 additions & 28 deletions tritium-metrics/src/main/metrics/cache-metrics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,14 @@ namespaces:
cache:
docs: Cache statistic metrics
metrics:
estimated.size:
type: gauge
tags: [cache]
docs: Approximate number of entries in this cache
weighted.size:
type: gauge
tags: [cache]
docs: Approximate accumulated weight of entries in this cache
maximum.size:
type: gauge
tags: [cache]
docs: Maximum number of cache entries cache can hold if limited, otherwise -1
request.count:
type: gauge
tags: [cache]
docs: Count of cache requests
hit.count:
type: counter
tags: [cache]
docs: Count of cache hits
hit.ratio:
type: gauge
tags: [cache]
docs: Percentage of cache hits
miss.count:
type: counter
tags: [cache]
docs: Count of cache misses
miss.ratio:
type: gauge
tags: [cache]
docs: Percentage of cache misses
load.success.count:
type: counter
tags: [cache]
Expand All @@ -45,10 +21,6 @@ namespaces:
type: counter
tags: [cache]
docs: Count of failed cache loads
load.average.millis:
type: gauge
tags: [cache]
docs: Average duration of cache load in milliseconds
evictions:
type: counter
tags: [cache, cause]
Expand Down

0 comments on commit 2eb59bc

Please sign in to comment.