diff --git a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java index 272da72..e6bc24e 100644 --- a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java +++ b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java @@ -4,6 +4,8 @@ import com.codahale.metrics.Metric; import com.codahale.metrics.MetricRegistry; import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; import com.typesafe.config.Config; import io.github.mweirauch.micrometer.jvm.extras.ProcessMemoryMetrics; import io.github.mweirauch.micrometer.jvm.extras.ProcessThreadMetrics; @@ -39,6 +41,7 @@ import io.prometheus.client.CollectorRegistry; import io.prometheus.client.dropwizard.DropwizardExports; import io.prometheus.client.exporter.PushGateway; +import java.lang.reflect.Field; import java.time.Duration; import java.util.ArrayList; import java.util.HashMap; @@ -73,6 +76,7 @@ public class PlatformMetricsRegistry { private static final String LOGGING_REPORTER_NAME = "logging"; private static final String TESTING_REPORTER_NAME = "testing"; private static final String CONSOLE_REPORTER_NAME = "console"; + private static final String CACHE_MAX_SIZE_GAUGE = "cache.max.size"; /** * List of tags that need to be reported for all the metrics reported by this service. The tags @@ -451,6 +455,45 @@ public static void registerCache( GuavaCacheMetrics.monitor(meterRegistry, guavaCache, cacheName, toIterable(tags)); } + /** + * Registers metrics for GuavaCaches using micrometer's GuavaCacheMetrics under the given + * cacheName for the cache built using builder and also reports maximum size configured + */ + public static Cache registerAndGetCache( + String cacheName, + CacheBuilder guavaCacheBuilder, + CacheLoader loader, + Map tags) { + reportCacheMaxSize(cacheName, guavaCacheBuilder); + Cache guavaCache = guavaCacheBuilder.build(loader); + GuavaCacheMetrics.monitor(meterRegistry, guavaCache, cacheName, toIterable(tags)); + return guavaCache; + } + + /** + * Registers metrics for GuavaCaches using micrometer's GuavaCacheMetrics under the given + * cacheName for the cache built using builder and also reports maximum size configured + */ + public static Cache registerAndGetCache( + String cacheName, CacheBuilder guavaCacheBuilder, Map tags) { + reportCacheMaxSize(cacheName, guavaCacheBuilder); + Cache guavaCache = guavaCacheBuilder.build(); + GuavaCacheMetrics.monitor(meterRegistry, guavaCache, cacheName, toIterable(tags)); + return guavaCache; + } + + private static void reportCacheMaxSize( + String cacheName, CacheBuilder guavaCacheBuilder) { + try { + Field maximumSizeField = guavaCacheBuilder.getClass().getDeclaredField("maximumSize"); + maximumSizeField.setAccessible(true); + long maximumSize = maximumSizeField.getLong(guavaCacheBuilder); + registerGauge(CACHE_MAX_SIZE_GAUGE, Map.of("cache", cacheName), maximumSize); + } catch (NoSuchFieldException | IllegalAccessException e) { + // ignore + } + } + /** * Registers metrics for the given executor service with the service's metric registry and reports * them periodically to the configured reporters. Apart from the given tags, the reporting