From d6062e8a66e4d419e288ca2afc29fdb42b63469e Mon Sep 17 00:00:00 2001 From: kishansairam9 Date: Wed, 31 Jan 2024 12:40:06 +0530 Subject: [PATCH 1/2] report cache max size, change interface --- .../metrics/PlatformMetricsRegistry.java | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) 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..32bb6dd 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,7 @@ import com.codahale.metrics.Metric; import com.codahale.metrics.MetricRegistry; import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; import com.typesafe.config.Config; import io.github.mweirauch.micrometer.jvm.extras.ProcessMemoryMetrics; import io.github.mweirauch.micrometer.jvm.extras.ProcessThreadMetrics; @@ -39,6 +40,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 +75,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 @@ -443,14 +446,33 @@ public static DistributionSummary registerDistributionSummary( } /** - * Registers metrics for GuavaCaches using micrometer's GuavaCacheMetrics under the given - * cacheName for the given guavaCache + * @Deprecated. Use overload which takes in builder and returns instance instead */ + @Deprecated(forRemoval = true) public static void registerCache( String cacheName, Cache guavaCache, Map tags) { GuavaCacheMetrics.monitor(meterRegistry, guavaCache, cacheName, toIterable(tags)); } + /** + * Registers metrics for GuavaCaches using micrometer's GuavaCacheMetrics under the given + * cacheName for the given guavaCache + */ + public static Cache registerCache( + String cacheName, CacheBuilder guavaCacheBuilder, Map tags) { + 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 + } + Cache guavaCache = guavaCacheBuilder.build(); + GuavaCacheMetrics.monitor(meterRegistry, guavaCache, cacheName, toIterable(tags)); + return guavaCache; + } + /** * 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 From 1d955c8bf1791c3cf42a354420561be570bd7970 Mon Sep 17 00:00:00 2001 From: kishansairam9 Date: Wed, 31 Jan 2024 14:41:08 +0530 Subject: [PATCH 2/2] update --- .../metrics/PlatformMetricsRegistry.java | 37 +++++++++++++++---- 1 file changed, 29 insertions(+), 8 deletions(-) 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 32bb6dd..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 @@ -5,6 +5,7 @@ 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; @@ -446,9 +447,9 @@ public static DistributionSummary registerDistributionSummary( } /** - * @Deprecated. Use overload which takes in builder and returns instance instead + * Registers metrics for GuavaCaches using micrometer's GuavaCacheMetrics under the given + * cacheName for the given guavaCache */ - @Deprecated(forRemoval = true) public static void registerCache( String cacheName, Cache guavaCache, Map tags) { GuavaCacheMetrics.monitor(meterRegistry, guavaCache, cacheName, toIterable(tags)); @@ -456,10 +457,33 @@ public static void registerCache( /** * Registers metrics for GuavaCaches using micrometer's GuavaCacheMetrics under the given - * cacheName for the given guavaCache + * 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 registerCache( - String cacheName, CacheBuilder guavaCacheBuilder, Map tags) { + 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); @@ -468,9 +492,6 @@ public static Cache registerCache( } catch (NoSuchFieldException | IllegalAccessException e) { // ignore } - Cache guavaCache = guavaCacheBuilder.build(); - GuavaCacheMetrics.monitor(meterRegistry, guavaCache, cacheName, toIterable(tags)); - return guavaCache; } /**