Skip to content

Commit

Permalink
Add Builder pattern for caffeine CacheMetricsCollector
Browse files Browse the repository at this point in the history
Signed-off-by: Jean Hominal <[email protected]>
  • Loading branch information
jhominal committed Jan 17, 2025
1 parent 2bb8477 commit fa68e3e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* <pre>{@code
* // Note that `recordStats()` is required to gather non-zero statistics
* Cache<String, String> cache = Caffeine.newBuilder().recordStats().build();
* CacheMetricsCollector cacheMetrics = new CacheMetricsCollector();
* CacheMetricsCollector cacheMetrics = CacheMetricsCollector.builder().build();
* PrometheusRegistry.defaultRegistry.register(cacheMetrics);
* cacheMetrics.addCache("mycache", cache);
*
Expand Down Expand Up @@ -271,4 +271,14 @@ public MetricSnapshots collect() {
public List<String> getPrometheusNames() {
return ALL_METRIC_NAMES;
}

public static Builder builder() {
return new Builder();
}

public static class Builder {
public CacheMetricsCollector build() {
return new CacheMetricsCollector();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void cacheExposesMetricsForHitMissAndEviction() {
final Cache<String, String> cache =
Caffeine.newBuilder().maximumSize(2).recordStats().executor(Runnable::run).build();

final CacheMetricsCollector collector = new CacheMetricsCollector();
final CacheMetricsCollector collector = CacheMetricsCollector.builder().build();
collector.addCache("users", cache);

final PrometheusRegistry registry = new PrometheusRegistry();
Expand Down Expand Up @@ -89,7 +89,7 @@ public void weightedCacheExposesMetricsForHitMissAndEvictionWeightedSize() {
.executor(Runnable::run)
.build();

final CacheMetricsCollector collector = new CacheMetricsCollector();
final CacheMetricsCollector collector = CacheMetricsCollector.builder().build();
collector.addCache("users", cache);

final PrometheusRegistry registry = new PrometheusRegistry();
Expand Down Expand Up @@ -148,7 +148,7 @@ public void loadingCacheExposesMetricsForLoadsAndExceptions() throws Exception {
.thenReturn("Third User");

final LoadingCache<String, String> cache = Caffeine.newBuilder().recordStats().build(loader);
final CacheMetricsCollector collector = new CacheMetricsCollector();
final CacheMetricsCollector collector = CacheMetricsCollector.builder().build();

collector.addCache("loadingusers", cache);

Expand Down Expand Up @@ -180,7 +180,7 @@ public void loadingCacheExposesMetricsForLoadsAndExceptions() throws Exception {

@Test
public void getPrometheusNamesHasSameSizeAsMetricSizeWhenScraping() {
final CacheMetricsCollector collector = new CacheMetricsCollector();
final CacheMetricsCollector collector = CacheMetricsCollector.builder().build();

final PrometheusRegistry registry = new PrometheusRegistry();
registry.register(collector);
Expand All @@ -193,7 +193,7 @@ public void getPrometheusNamesHasSameSizeAsMetricSizeWhenScraping() {

@Test
public void collectedMetricNamesAreKnownPrometheusNames() {
final CacheMetricsCollector collector = new CacheMetricsCollector();
final CacheMetricsCollector collector = CacheMetricsCollector.builder().build();

final PrometheusRegistry registry = new PrometheusRegistry();
registry.register(collector);
Expand Down

0 comments on commit fa68e3e

Please sign in to comment.