Skip to content

Commit 0329e38

Browse files
author
Kishan Sairam Adapa
authored
change cache max size reporting api (#89)
1 parent 437a15f commit 0329e38

File tree

1 file changed

+6
-38
lines changed

1 file changed

+6
-38
lines changed

platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import com.codahale.metrics.Metric;
55
import com.codahale.metrics.MetricRegistry;
66
import com.google.common.cache.Cache;
7-
import com.google.common.cache.CacheBuilder;
8-
import com.google.common.cache.CacheLoader;
97
import com.typesafe.config.Config;
108
import io.github.mweirauch.micrometer.jvm.extras.ProcessMemoryMetrics;
119
import io.github.mweirauch.micrometer.jvm.extras.ProcessThreadMetrics;
@@ -42,7 +40,6 @@
4240
import io.prometheus.client.CollectorRegistry;
4341
import io.prometheus.client.dropwizard.DropwizardExports;
4442
import io.prometheus.client.exporter.PushGateway;
45-
import java.lang.reflect.Field;
4643
import java.time.Duration;
4744
import java.util.ArrayList;
4845
import java.util.HashMap;
@@ -458,43 +455,14 @@ public static <K, V> void registerCache(
458455

459456
/**
460457
* Registers metrics for GuavaCaches using micrometer's GuavaCacheMetrics under the given
461-
* cacheName for the cache built using builder and also reports maximum size configured
458+
* cacheName for the given guavaCache and also reports maximum size configured
462459
*/
463-
public static <K, V> Cache<K, V> registerAndGetCache(
464-
String cacheName,
465-
CacheBuilder<Object, Object> guavaCacheBuilder,
466-
CacheLoader<? super K, V> loader,
467-
Map<String, String> tags) {
468-
reportCacheMaxSize(cacheName, guavaCacheBuilder, tags);
469-
Cache<K, V> guavaCache = guavaCacheBuilder.build(loader);
460+
public static <K, V> void registerCacheTrackingOccupancy(
461+
String cacheName, Cache<K, V> guavaCache, Map<String, String> tags, long maxSize) {
470462
GuavaCacheMetrics.monitor(meterRegistry, guavaCache, cacheName, toIterable(tags));
471-
return guavaCache;
472-
}
473-
474-
/**
475-
* Registers metrics for GuavaCaches using micrometer's GuavaCacheMetrics under the given
476-
* cacheName for the cache built using builder and also reports maximum size configured
477-
*/
478-
public static <K, V> Cache<K, V> registerAndGetCache(
479-
String cacheName, CacheBuilder<Object, Object> guavaCacheBuilder, Map<String, String> tags) {
480-
reportCacheMaxSize(cacheName, guavaCacheBuilder, tags);
481-
Cache<K, V> guavaCache = guavaCacheBuilder.build();
482-
GuavaCacheMetrics.monitor(meterRegistry, guavaCache, cacheName, toIterable(tags));
483-
return guavaCache;
484-
}
485-
486-
private static <K, V> void reportCacheMaxSize(
487-
String cacheName, CacheBuilder<K, V> guavaCacheBuilder, Map<String, String> tags) {
488-
try {
489-
Field maximumSizeField = guavaCacheBuilder.getClass().getDeclaredField("maximumSize");
490-
maximumSizeField.setAccessible(true);
491-
long maximumSize = maximumSizeField.getLong(guavaCacheBuilder);
492-
Map<String, String> tagsCopy = new HashMap<>(tags);
493-
tagsCopy.put("cache", cacheName);
494-
registerGauge(CACHE_MAX_SIZE_GAUGE, tagsCopy, maximumSize);
495-
} catch (NoSuchFieldException | IllegalAccessException e) {
496-
// ignore
497-
}
463+
Map<String, String> tagsForGauge = new HashMap<>(tags);
464+
tagsForGauge.put("cache", cacheName);
465+
registerGauge(CACHE_MAX_SIZE_GAUGE, tagsForGauge, maxSize);
498466
}
499467

500468
/**

0 commit comments

Comments
 (0)