Skip to content

Commit

Permalink
Change CacheState.register to a fluent API (#1920)
Browse files Browse the repository at this point in the history
  • Loading branch information
pkoenig10 authored Mar 27, 2024
1 parent 7c1cc3f commit d781dee
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 22 deletions.
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-1920.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: '`CacheState.register` has been changed to be a more fluent API.'
links:
- https://github.com/palantir/tritium/pull/1920
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.codahale.metrics.Meter;
import com.codahale.metrics.Timer;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.Policy;
import com.github.benmanes.caffeine.cache.RemovalCause;
import com.github.benmanes.caffeine.cache.stats.StatsCounter;
Expand All @@ -31,6 +32,7 @@
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.LongAdder;
import java.util.function.Function;
import java.util.function.Supplier;
import org.checkerframework.checker.index.qual.NonNegative;

Expand All @@ -47,16 +49,14 @@ public final class CacheStats implements StatsCounter, Supplier<StatsCounter> {
private final LongAdder totalLoadTime = new LongAdder();

/**
* Creates a {@link CacheStats} instance that registers metrics for Caffeine cache statistics.
* Creates a {@link CacheStats} instance that can be used to record metrics for Caffeine cache statistics.
* <p>
* Example usage for a {@link com.github.benmanes.caffeine.cache.Cache} or
* {@link com.github.benmanes.caffeine.cache.LoadingCache}:
* Example usage:
* <pre>
* CacheStats cacheStats = CacheStats.of(taggedMetricRegistry, "your-cache-name")
* LoadingCache&lt;Integer, String&gt; cache = Caffeine.newBuilder()
* .recordStats(cacheStats)
* .build(key -&gt; computeSomethingExpensive(key));
* cacheStats.register(cache);
* Cache&lt;Integer, String&gt; cache = CacheStats.of(taggedMetricRegistry, "your-cache-name")
* .register(stats -> Caffeine.newBuilder()
* .recordStats(stats)
* .build());
* </pre>
* @param taggedMetricRegistry tagged metric registry to add cache metrics
* @param name cache name
Expand All @@ -68,12 +68,24 @@ public static CacheStats of(TaggedMetricRegistry taggedMetricRegistry, @Safe Str
}

/**
* Registers additional metrics for a Caffeine cache.
*
* @param cache cache for which to register metrics
* @return the given cache instance
* Constructs and registers metrics for Caffeine cache statistics.
* <p>
* In order to record metrics, the {@code cacheFactory} must use the provided {@link CacheStats} with
* {@link Caffeine#recordStats(Supplier)})}.
* <p>
* Example usage:
* <pre>
* Cache&lt;Integer, String&gt; cache = CacheStats.of(taggedMetricRegistry, "your-cache-name")
* .register(stats -> Caffeine.newBuilder()
* .recordStats(stats)
* .build());
* </pre>
* @param cacheFactory method which will be invoked to construct the cache
* @return the constructed cache instance
*/
public <K, V, C extends Cache<K, V>> C register(C cache) {
public <K, V, C extends Cache<K, V>> C register(Function<CacheStats, C> cacheFactory) {
C cache = cacheFactory.apply(this);

metrics.estimatedSize().cache(name).build(cache::estimatedSize);
metrics.weightedSize().cache(name).build(() -> cache.policy()
.eviction()
Expand All @@ -83,6 +95,7 @@ public <K, V, C extends Cache<K, V>> C register(C cache) {
.eviction()
.map(Policy.Eviction::getMaximum)
.orElse(null));

return cache;
}

Expand Down
2 changes: 1 addition & 1 deletion tritium-caffeine/src/main/metrics/cache-metrics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ namespaces:
maximum.size:
type: gauge
tags: [cache]
docs: Maximum number of cache entries cache can hold if limited, otherwise -1
docs: Maximum number of cache entries cache can hold if limited
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,9 @@ void registerCacheWithoutRecordingStatsTagged() {

@Test
void registerTaggedMetrics() {
CacheStats cacheStats = CacheStats.of(taggedMetricRegistry, "test");
Cache<Integer, String> cache =
Caffeine.newBuilder().recordStats(cacheStats).maximumSize(2).build();
cacheStats.register(cache);
Cache<Integer, String> cache = CacheStats.of(taggedMetricRegistry, "test")
.register(stats ->
Caffeine.newBuilder().recordStats(stats).maximumSize(2).build());

assertThat(taggedMetricRegistry.getMetrics().keySet())
.extracting(MetricName::safeName)
Expand Down Expand Up @@ -279,10 +278,9 @@ void registerTaggedMetrics() {

@Test
void registerLoadingTaggedMetrics() {
CacheStats cacheStats = CacheStats.of(taggedMetricRegistry, "test");
LoadingCache<Integer, String> cache =
Caffeine.newBuilder().recordStats(cacheStats).maximumSize(2).build(mapping::apply);
cacheStats.register(cache);
LoadingCache<Integer, String> cache = CacheStats.of(taggedMetricRegistry, "test")
.register(stats ->
Caffeine.newBuilder().recordStats(stats).maximumSize(2).build(mapping::apply));
assertThat(taggedMetricRegistry.getMetrics().keySet())
.extracting(MetricName::safeName)
.containsExactlyInAnyOrder(
Expand Down

0 comments on commit d781dee

Please sign in to comment.