Skip to content

Commit

Permalink
self supply
Browse files Browse the repository at this point in the history
  • Loading branch information
schlosna committed Mar 7, 2024
1 parent 0af16d2 commit 683c166
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ 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.recorder())
.recordStats(cacheStats)
.build());

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import java.util.function.Supplier;
import org.checkerframework.checker.index.qual.NonNegative;

public final class CacheStats implements StatsCounter {
public final class CacheStats implements StatsCounter, Supplier<StatsCounter> {
private final CacheMetrics metrics;
private final String name;
private final Counter hitCounter;
Expand Down Expand Up @@ -112,8 +112,9 @@ public <K, V> LoadingCache<K, V> register(LoadingCache<K, V> cache) {
return (LoadingCache<K, V>) register((Cache<K, V>) cache);
}

public Supplier<? extends StatsCounter> recorder() {
return () -> this;
@Override
public StatsCounter get() {
return this;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,8 @@ void registerCacheWithoutRecordingStatsTagged() {
@Test
void registerTaggedMetrics() {
CacheStats cacheStats = CacheStats.of(taggedMetricRegistry, "test");
Cache<Integer, String> cache = cacheStats.register(Caffeine.newBuilder()
.recordStats(cacheStats.recorder())
.maximumSize(2)
.build());
Cache<Integer, String> cache = cacheStats.register(
Caffeine.newBuilder().recordStats(cacheStats).maximumSize(2).build());
assertThat(taggedMetricRegistry.getMetrics().keySet())
.extracting(MetricName::safeName)
.contains(
Expand Down Expand Up @@ -264,10 +262,8 @@ void registerTaggedMetrics() {
@Test
void registerLoadingTaggedMetrics() {
CacheStats cacheStats = CacheStats.of(taggedMetricRegistry, "test");
LoadingCache<Integer, String> cache = cacheStats.register(Caffeine.newBuilder()
.recordStats(cacheStats.recorder())
.maximumSize(2)
.build(mapping::apply));
LoadingCache<Integer, String> cache = cacheStats.register(
Caffeine.newBuilder().recordStats(cacheStats).maximumSize(2).build(mapping::apply));
assertThat(taggedMetricRegistry.getMetrics().keySet())
.extracting(MetricName::safeName)
.contains(
Expand Down

0 comments on commit 683c166

Please sign in to comment.