Skip to content

Commit d7930f5

Browse files
committed
Add option collectWeightedSize to enable collection of caffeine_cache_weighted_size
Signed-off-by: Jean Hominal <[email protected]>
1 parent 5515d62 commit d7930f5

File tree

2 files changed

+53
-17
lines changed

2 files changed

+53
-17
lines changed

prometheus-metrics-instrumentation-caffeine/src/main/java/io/prometheus/metrics/instrumentation/caffeine/CacheMetricsCollector.java

+32-11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Optional;
1919
import java.util.concurrent.ConcurrentHashMap;
2020
import java.util.concurrent.ConcurrentMap;
21+
import java.util.stream.Collectors;
2122

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

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

8991
/**
9092
* Instantiates a {@link CacheMetricsCollector}, with the legacy parameters.
@@ -96,17 +98,20 @@ public class CacheMetricsCollector implements MultiCollector {
9698
*/
9799
@Deprecated
98100
public CacheMetricsCollector() {
99-
this(false);
101+
this(false, false);
100102
}
101103

102104
/**
103105
* Instantiate a {@link CacheMetricsCollector}
104106
*
105107
* @param collectEvictionWeightAsCounter If true, {@code caffeine_cache_eviction_weight} will be
106108
* observed as an incrementing counter instead of a gauge.
109+
* @param collectWeightedSize If true, {@code caffeine_cache_weighted_size} will be observed.
107110
*/
108-
protected CacheMetricsCollector(boolean collectEvictionWeightAsCounter) {
111+
protected CacheMetricsCollector(
112+
boolean collectEvictionWeightAsCounter, boolean collectWeightedSize) {
109113
this.collectEvictionWeightAsCounter = collectEvictionWeightAsCounter;
114+
this.collectWeightedSize = collectWeightedSize;
110115
}
111116

112117
/**
@@ -225,13 +230,15 @@ public MetricSnapshots collect() {
225230
// EvictionWeight metric is unavailable, newer version of Caffeine is needed.
226231
}
227232

228-
final Optional<? extends Policy.Eviction<?, ?>> eviction = c.getValue().policy().eviction();
229-
if (eviction.isPresent() && eviction.get().weightedSize().isPresent()) {
230-
cacheWeightedSize.dataPoint(
231-
GaugeSnapshot.GaugeDataPointSnapshot.builder()
232-
.labels(labels)
233-
.value(eviction.get().weightedSize().getAsLong())
234-
.build());
233+
if (collectWeightedSize) {
234+
final Optional<? extends Policy.Eviction<?, ?>> eviction = c.getValue().policy().eviction();
235+
if (eviction.isPresent() && eviction.get().weightedSize().isPresent()) {
236+
cacheWeightedSize.dataPoint(
237+
GaugeSnapshot.GaugeDataPointSnapshot.builder()
238+
.labels(labels)
239+
.value(eviction.get().weightedSize().getAsLong())
240+
.build());
241+
}
235242
}
236243

237244
cacheHitTotal.dataPoint(
@@ -286,6 +293,10 @@ public MetricSnapshots collect() {
286293
}
287294
}
288295

296+
if (collectWeightedSize) {
297+
metricSnapshotsBuilder.metricSnapshot(cacheWeightedSize.build());
298+
}
299+
289300
return metricSnapshotsBuilder
290301
.metricSnapshot(cacheHitTotal.build())
291302
.metricSnapshot(cacheMissTotal.build())
@@ -298,13 +309,17 @@ public MetricSnapshots collect() {
298309
.metricSnapshot(cacheLoadFailure.build())
299310
.metricSnapshot(cacheLoadTotal.build())
300311
.metricSnapshot(cacheSize.build())
301-
.metricSnapshot(cacheWeightedSize.build())
302312
.metricSnapshot(cacheLoadSummary.build())
303313
.build();
304314
}
305315

306316
@Override
307317
public List<String> getPrometheusNames() {
318+
if (!collectWeightedSize) {
319+
return ALL_METRIC_NAMES.stream()
320+
.filter(s -> !METRIC_NAME_CACHE_WEIGHTED_SIZE.equals(s))
321+
.collect(Collectors.toList());
322+
}
308323
return ALL_METRIC_NAMES;
309324
}
310325

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

317332
private boolean collectEvictionWeightAsCounter = true;
333+
private boolean collectWeightedSize = true;
318334

319335
public Builder collectEvictionWeightAsCounter(boolean collectEvictionWeightAsCounter) {
320336
this.collectEvictionWeightAsCounter = collectEvictionWeightAsCounter;
321337
return this;
322338
}
323339

340+
public Builder collectWeightedSize(boolean collectWeightedSize) {
341+
this.collectWeightedSize = collectWeightedSize;
342+
return this;
343+
}
344+
324345
public CacheMetricsCollector build() {
325-
return new CacheMetricsCollector(collectEvictionWeightAsCounter);
346+
return new CacheMetricsCollector(collectEvictionWeightAsCounter, collectWeightedSize);
326347
}
327348
}
328349
}

prometheus-metrics-instrumentation-caffeine/src/test/java/io/prometheus/metrics/instrumentation/caffeine/CacheMetricsCollectorTest.java

+21-6
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,17 @@
3030
class CacheMetricsCollectorTest {
3131
// This enum was added to simplify test parametrization on argument options.
3232
public enum Options {
33-
LEGACY(false),
34-
COLLECT_EVICTION_WEIGHT_AS_COUNTER(true);
33+
LEGACY(false, false),
34+
COLLECT_EVICTION_WEIGHT_AS_COUNTER(true, false),
35+
COLLECT_WEIGHTED_SIZE(false, true),
36+
BUILDER_DEFAULT(true, true);
3537

3638
private final boolean collectEvictionWeightAsCounter;
39+
private final boolean collectWeightedSize;
3740

38-
Options(boolean collectEvictionWeightAsCounter) {
41+
Options(boolean collectEvictionWeightAsCounter, boolean collectWeightedSize) {
3942
this.collectEvictionWeightAsCounter = collectEvictionWeightAsCounter;
43+
this.collectWeightedSize = collectWeightedSize;
4044
}
4145
}
4246

@@ -50,6 +54,7 @@ public void cacheExposesMetricsForHitMissAndEviction(Options options) {
5054
final CacheMetricsCollector collector =
5155
CacheMetricsCollector.builder()
5256
.collectEvictionWeightAsCounter(options.collectEvictionWeightAsCounter)
57+
.collectWeightedSize(options.collectWeightedSize)
5358
.build();
5459
collector.addCache("users", cache);
5560

@@ -122,6 +127,7 @@ public void weightedCacheExposesMetricsForHitMissAndEvictionWeightedSize(Options
122127
final CacheMetricsCollector collector =
123128
CacheMetricsCollector.builder()
124129
.collectEvictionWeightAsCounter(options.collectEvictionWeightAsCounter)
130+
.collectWeightedSize(options.collectWeightedSize)
125131
.build();
126132
collector.addCache("users", cache);
127133

@@ -156,6 +162,15 @@ public void weightedCacheExposesMetricsForHitMissAndEvictionWeightedSize(Options
156162
+ "# HELP caffeine_cache_eviction_weight Weight of evicted cache entries, doesn't include manually removed entries\n"
157163
+ "caffeine_cache_eviction_weight{cache=\"users\"} 31.0\n";
158164
}
165+
String openMetricWeightedSizeExpectedText;
166+
if (options.collectWeightedSize) {
167+
openMetricWeightedSizeExpectedText =
168+
"# TYPE caffeine_cache_weighted_size gauge\n"
169+
+ "# HELP caffeine_cache_weighted_size Approximate accumulated weight of cache entries\n"
170+
+ "caffeine_cache_weighted_size{cache=\"users\"} 31.0\n";
171+
} else {
172+
openMetricWeightedSizeExpectedText = "";
173+
}
159174

160175
final String expected =
161176
"# TYPE caffeine_cache_estimated_size gauge\n"
@@ -174,9 +189,7 @@ public void weightedCacheExposesMetricsForHitMissAndEvictionWeightedSize(Options
174189
+ "# TYPE caffeine_cache_requests counter\n"
175190
+ "# HELP caffeine_cache_requests Cache request totals, hits + misses\n"
176191
+ "caffeine_cache_requests_total{cache=\"users\"} 3.0\n"
177-
+ "# TYPE caffeine_cache_weighted_size gauge\n"
178-
+ "# HELP caffeine_cache_weighted_size Approximate accumulated weight of cache entries\n"
179-
+ "caffeine_cache_weighted_size{cache=\"users\"} 31.0\n"
192+
+ openMetricWeightedSizeExpectedText
180193
+ "# EOF\n";
181194

182195
assertThat(convertToOpenMetricsFormat(registry)).isEqualTo(expected);
@@ -228,6 +241,7 @@ public void getPrometheusNamesHasSameSizeAsMetricSizeWhenScraping(Options option
228241
final CacheMetricsCollector collector =
229242
CacheMetricsCollector.builder()
230243
.collectEvictionWeightAsCounter(options.collectEvictionWeightAsCounter)
244+
.collectWeightedSize(options.collectWeightedSize)
231245
.build();
232246

233247
final PrometheusRegistry registry = new PrometheusRegistry();
@@ -245,6 +259,7 @@ public void collectedMetricNamesAreKnownPrometheusNames(Options options) {
245259
final CacheMetricsCollector collector =
246260
CacheMetricsCollector.builder()
247261
.collectEvictionWeightAsCounter(options.collectEvictionWeightAsCounter)
262+
.collectWeightedSize(options.collectWeightedSize)
248263
.build();
249264

250265
final PrometheusRegistry registry = new PrometheusRegistry();

0 commit comments

Comments
 (0)