From d7930f536dddb8519e5576fe0ac713af679354a3 Mon Sep 17 00:00:00 2001 From: Jean Hominal Date: Sat, 18 Jan 2025 02:07:34 +0100 Subject: [PATCH] Add option collectWeightedSize to enable collection of caffeine_cache_weighted_size Signed-off-by: Jean Hominal --- .../caffeine/CacheMetricsCollector.java | 43 ++++++++++++++----- .../caffeine/CacheMetricsCollectorTest.java | 27 +++++++++--- 2 files changed, 53 insertions(+), 17 deletions(-) diff --git a/prometheus-metrics-instrumentation-caffeine/src/main/java/io/prometheus/metrics/instrumentation/caffeine/CacheMetricsCollector.java b/prometheus-metrics-instrumentation-caffeine/src/main/java/io/prometheus/metrics/instrumentation/caffeine/CacheMetricsCollector.java index df2b30783..a1b8782a3 100644 --- a/prometheus-metrics-instrumentation-caffeine/src/main/java/io/prometheus/metrics/instrumentation/caffeine/CacheMetricsCollector.java +++ b/prometheus-metrics-instrumentation-caffeine/src/main/java/io/prometheus/metrics/instrumentation/caffeine/CacheMetricsCollector.java @@ -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. @@ -85,6 +86,7 @@ public class CacheMetricsCollector implements MultiCollector { protected final ConcurrentMap> children = new ConcurrentHashMap<>(); private final boolean collectEvictionWeightAsCounter; + private final boolean collectWeightedSize; /** * Instantiates a {@link CacheMetricsCollector}, with the legacy parameters. @@ -96,7 +98,7 @@ public class CacheMetricsCollector implements MultiCollector { */ @Deprecated public CacheMetricsCollector() { - this(false); + this(false, false); } /** @@ -104,9 +106,12 @@ public 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; } /** @@ -225,13 +230,15 @@ public MetricSnapshots collect() { // EvictionWeight metric is unavailable, newer version of Caffeine is needed. } - final Optional> 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> 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( @@ -286,6 +293,10 @@ public MetricSnapshots collect() { } } + if (collectWeightedSize) { + metricSnapshotsBuilder.metricSnapshot(cacheWeightedSize.build()); + } + return metricSnapshotsBuilder .metricSnapshot(cacheHitTotal.build()) .metricSnapshot(cacheMissTotal.build()) @@ -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 getPrometheusNames() { + if (!collectWeightedSize) { + return ALL_METRIC_NAMES.stream() + .filter(s -> !METRIC_NAME_CACHE_WEIGHTED_SIZE.equals(s)) + .collect(Collectors.toList()); + } return ALL_METRIC_NAMES; } @@ -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); } } } diff --git a/prometheus-metrics-instrumentation-caffeine/src/test/java/io/prometheus/metrics/instrumentation/caffeine/CacheMetricsCollectorTest.java b/prometheus-metrics-instrumentation-caffeine/src/test/java/io/prometheus/metrics/instrumentation/caffeine/CacheMetricsCollectorTest.java index 55c13a42c..ac5ec9e2a 100644 --- a/prometheus-metrics-instrumentation-caffeine/src/test/java/io/prometheus/metrics/instrumentation/caffeine/CacheMetricsCollectorTest.java +++ b/prometheus-metrics-instrumentation-caffeine/src/test/java/io/prometheus/metrics/instrumentation/caffeine/CacheMetricsCollectorTest.java @@ -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; } } @@ -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); @@ -122,6 +127,7 @@ public void weightedCacheExposesMetricsForHitMissAndEvictionWeightedSize(Options final CacheMetricsCollector collector = CacheMetricsCollector.builder() .collectEvictionWeightAsCounter(options.collectEvictionWeightAsCounter) + .collectWeightedSize(options.collectWeightedSize) .build(); collector.addCache("users", cache); @@ -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" @@ -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); @@ -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(); @@ -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();