Skip to content

Commit

Permalink
Add option collectWeightedSize to enable collection of caffeine_cache…
Browse files Browse the repository at this point in the history
…_weighted_size

Signed-off-by: Jean Hominal <[email protected]>
  • Loading branch information
jhominal committed Jan 18, 2025
1 parent 5515d62 commit d7930f5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;

/**
* Collect metrics from Caffeine's com.github.benmanes.caffeine.cache.Cache.
Expand Down Expand Up @@ -85,6 +86,7 @@ public class CacheMetricsCollector implements MultiCollector {

protected final ConcurrentMap<String, Cache<?, ?>> children = new ConcurrentHashMap<>();
private final boolean collectEvictionWeightAsCounter;
private final boolean collectWeightedSize;

/**
* Instantiates a {@link CacheMetricsCollector}, with the legacy parameters.
Expand All @@ -96,17 +98,20 @@ public class CacheMetricsCollector implements MultiCollector {
*/
@Deprecated
public CacheMetricsCollector() {
this(false);
this(false, false);
}

/**
* Instantiate a {@link CacheMetricsCollector}
*
* @param collectEvictionWeightAsCounter If true, {@code caffeine_cache_eviction_weight} will be
* observed as an incrementing counter instead of a gauge.
* @param collectWeightedSize If true, {@code caffeine_cache_weighted_size} will be observed.
*/
protected CacheMetricsCollector(boolean collectEvictionWeightAsCounter) {
protected CacheMetricsCollector(
boolean collectEvictionWeightAsCounter, boolean collectWeightedSize) {
this.collectEvictionWeightAsCounter = collectEvictionWeightAsCounter;
this.collectWeightedSize = collectWeightedSize;
}

/**
Expand Down Expand Up @@ -225,13 +230,15 @@ public MetricSnapshots collect() {
// EvictionWeight metric is unavailable, newer version of Caffeine is needed.
}

final Optional<? extends Policy.Eviction<?, ?>> eviction = c.getValue().policy().eviction();
if (eviction.isPresent() && eviction.get().weightedSize().isPresent()) {
cacheWeightedSize.dataPoint(
GaugeSnapshot.GaugeDataPointSnapshot.builder()
.labels(labels)
.value(eviction.get().weightedSize().getAsLong())
.build());
if (collectWeightedSize) {
final Optional<? extends Policy.Eviction<?, ?>> eviction = c.getValue().policy().eviction();
if (eviction.isPresent() && eviction.get().weightedSize().isPresent()) {
cacheWeightedSize.dataPoint(
GaugeSnapshot.GaugeDataPointSnapshot.builder()
.labels(labels)
.value(eviction.get().weightedSize().getAsLong())
.build());
}
}

cacheHitTotal.dataPoint(
Expand Down Expand Up @@ -286,6 +293,10 @@ public MetricSnapshots collect() {
}
}

if (collectWeightedSize) {
metricSnapshotsBuilder.metricSnapshot(cacheWeightedSize.build());
}

return metricSnapshotsBuilder
.metricSnapshot(cacheHitTotal.build())
.metricSnapshot(cacheMissTotal.build())
Expand All @@ -298,13 +309,17 @@ public MetricSnapshots collect() {
.metricSnapshot(cacheLoadFailure.build())
.metricSnapshot(cacheLoadTotal.build())
.metricSnapshot(cacheSize.build())
.metricSnapshot(cacheWeightedSize.build())
.metricSnapshot(cacheLoadSummary.build())
.build();
}

@Override
public List<String> getPrometheusNames() {
if (!collectWeightedSize) {
return ALL_METRIC_NAMES.stream()
.filter(s -> !METRIC_NAME_CACHE_WEIGHTED_SIZE.equals(s))
.collect(Collectors.toList());
}
return ALL_METRIC_NAMES;
}

Expand All @@ -315,14 +330,20 @@ public static Builder builder() {
public static class Builder {

private boolean collectEvictionWeightAsCounter = true;
private boolean collectWeightedSize = true;

public Builder collectEvictionWeightAsCounter(boolean collectEvictionWeightAsCounter) {
this.collectEvictionWeightAsCounter = collectEvictionWeightAsCounter;
return this;
}

public Builder collectWeightedSize(boolean collectWeightedSize) {
this.collectWeightedSize = collectWeightedSize;
return this;
}

public CacheMetricsCollector build() {
return new CacheMetricsCollector(collectEvictionWeightAsCounter);
return new CacheMetricsCollector(collectEvictionWeightAsCounter, collectWeightedSize);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,17 @@
class CacheMetricsCollectorTest {
// This enum was added to simplify test parametrization on argument options.
public enum Options {
LEGACY(false),
COLLECT_EVICTION_WEIGHT_AS_COUNTER(true);
LEGACY(false, false),
COLLECT_EVICTION_WEIGHT_AS_COUNTER(true, false),
COLLECT_WEIGHTED_SIZE(false, true),
BUILDER_DEFAULT(true, true);

private final boolean collectEvictionWeightAsCounter;
private final boolean collectWeightedSize;

Options(boolean collectEvictionWeightAsCounter) {
Options(boolean collectEvictionWeightAsCounter, boolean collectWeightedSize) {
this.collectEvictionWeightAsCounter = collectEvictionWeightAsCounter;
this.collectWeightedSize = collectWeightedSize;
}
}

Expand All @@ -50,6 +54,7 @@ public void cacheExposesMetricsForHitMissAndEviction(Options options) {
final CacheMetricsCollector collector =
CacheMetricsCollector.builder()
.collectEvictionWeightAsCounter(options.collectEvictionWeightAsCounter)
.collectWeightedSize(options.collectWeightedSize)
.build();
collector.addCache("users", cache);

Expand Down Expand Up @@ -122,6 +127,7 @@ public void weightedCacheExposesMetricsForHitMissAndEvictionWeightedSize(Options
final CacheMetricsCollector collector =
CacheMetricsCollector.builder()
.collectEvictionWeightAsCounter(options.collectEvictionWeightAsCounter)
.collectWeightedSize(options.collectWeightedSize)
.build();
collector.addCache("users", cache);

Expand Down Expand Up @@ -156,6 +162,15 @@ public void weightedCacheExposesMetricsForHitMissAndEvictionWeightedSize(Options
+ "# HELP caffeine_cache_eviction_weight Weight of evicted cache entries, doesn't include manually removed entries\n"
+ "caffeine_cache_eviction_weight{cache=\"users\"} 31.0\n";
}
String openMetricWeightedSizeExpectedText;
if (options.collectWeightedSize) {
openMetricWeightedSizeExpectedText =
"# TYPE caffeine_cache_weighted_size gauge\n"
+ "# HELP caffeine_cache_weighted_size Approximate accumulated weight of cache entries\n"
+ "caffeine_cache_weighted_size{cache=\"users\"} 31.0\n";
} else {
openMetricWeightedSizeExpectedText = "";
}

final String expected =
"# TYPE caffeine_cache_estimated_size gauge\n"
Expand All @@ -174,9 +189,7 @@ public void weightedCacheExposesMetricsForHitMissAndEvictionWeightedSize(Options
+ "# TYPE caffeine_cache_requests counter\n"
+ "# HELP caffeine_cache_requests Cache request totals, hits + misses\n"
+ "caffeine_cache_requests_total{cache=\"users\"} 3.0\n"
+ "# TYPE caffeine_cache_weighted_size gauge\n"
+ "# HELP caffeine_cache_weighted_size Approximate accumulated weight of cache entries\n"
+ "caffeine_cache_weighted_size{cache=\"users\"} 31.0\n"
+ openMetricWeightedSizeExpectedText
+ "# EOF\n";

assertThat(convertToOpenMetricsFormat(registry)).isEqualTo(expected);
Expand Down Expand Up @@ -228,6 +241,7 @@ public void getPrometheusNamesHasSameSizeAsMetricSizeWhenScraping(Options option
final CacheMetricsCollector collector =
CacheMetricsCollector.builder()
.collectEvictionWeightAsCounter(options.collectEvictionWeightAsCounter)
.collectWeightedSize(options.collectWeightedSize)
.build();

final PrometheusRegistry registry = new PrometheusRegistry();
Expand All @@ -245,6 +259,7 @@ public void collectedMetricNamesAreKnownPrometheusNames(Options options) {
final CacheMetricsCollector collector =
CacheMetricsCollector.builder()
.collectEvictionWeightAsCounter(options.collectEvictionWeightAsCounter)
.collectWeightedSize(options.collectWeightedSize)
.build();

final PrometheusRegistry registry = new PrometheusRegistry();
Expand Down

0 comments on commit d7930f5

Please sign in to comment.