Skip to content

Commit 4275cb1

Browse files
author
Kishan Sairam Adapa
authored
Cache max size metric (#87)
* report cache max size, change interface * update
1 parent d0f574a commit 4275cb1

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
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;
79
import com.typesafe.config.Config;
810
import io.github.mweirauch.micrometer.jvm.extras.ProcessMemoryMetrics;
911
import io.github.mweirauch.micrometer.jvm.extras.ProcessThreadMetrics;
@@ -39,6 +41,7 @@
3941
import io.prometheus.client.CollectorRegistry;
4042
import io.prometheus.client.dropwizard.DropwizardExports;
4143
import io.prometheus.client.exporter.PushGateway;
44+
import java.lang.reflect.Field;
4245
import java.time.Duration;
4346
import java.util.ArrayList;
4447
import java.util.HashMap;
@@ -73,6 +76,7 @@ public class PlatformMetricsRegistry {
7376
private static final String LOGGING_REPORTER_NAME = "logging";
7477
private static final String TESTING_REPORTER_NAME = "testing";
7578
private static final String CONSOLE_REPORTER_NAME = "console";
79+
private static final String CACHE_MAX_SIZE_GAUGE = "cache.max.size";
7680

7781
/**
7882
* List of tags that need to be reported for all the metrics reported by this service. The tags
@@ -451,6 +455,45 @@ public static <K, V> void registerCache(
451455
GuavaCacheMetrics.monitor(meterRegistry, guavaCache, cacheName, toIterable(tags));
452456
}
453457

458+
/**
459+
* Registers metrics for GuavaCaches using micrometer's GuavaCacheMetrics under the given
460+
* cacheName for the cache built using builder and also reports maximum size configured
461+
*/
462+
public static <K, V> Cache<K, V> registerAndGetCache(
463+
String cacheName,
464+
CacheBuilder<Object, Object> guavaCacheBuilder,
465+
CacheLoader<? super K, V> loader,
466+
Map<String, String> tags) {
467+
reportCacheMaxSize(cacheName, guavaCacheBuilder);
468+
Cache<K, V> guavaCache = guavaCacheBuilder.build(loader);
469+
GuavaCacheMetrics.monitor(meterRegistry, guavaCache, cacheName, toIterable(tags));
470+
return guavaCache;
471+
}
472+
473+
/**
474+
* Registers metrics for GuavaCaches using micrometer's GuavaCacheMetrics under the given
475+
* cacheName for the cache built using builder and also reports maximum size configured
476+
*/
477+
public static <K, V> Cache<K, V> registerAndGetCache(
478+
String cacheName, CacheBuilder<Object, Object> guavaCacheBuilder, Map<String, String> tags) {
479+
reportCacheMaxSize(cacheName, guavaCacheBuilder);
480+
Cache<K, V> guavaCache = guavaCacheBuilder.build();
481+
GuavaCacheMetrics.monitor(meterRegistry, guavaCache, cacheName, toIterable(tags));
482+
return guavaCache;
483+
}
484+
485+
private static <K, V> void reportCacheMaxSize(
486+
String cacheName, CacheBuilder<K, V> guavaCacheBuilder) {
487+
try {
488+
Field maximumSizeField = guavaCacheBuilder.getClass().getDeclaredField("maximumSize");
489+
maximumSizeField.setAccessible(true);
490+
long maximumSize = maximumSizeField.getLong(guavaCacheBuilder);
491+
registerGauge(CACHE_MAX_SIZE_GAUGE, Map.of("cache", cacheName), maximumSize);
492+
} catch (NoSuchFieldException | IllegalAccessException e) {
493+
// ignore
494+
}
495+
}
496+
454497
/**
455498
* Registers metrics for the given executor service with the service's metric registry and reports
456499
* them periodically to the configured reporters. Apart from the given tags, the reporting

0 commit comments

Comments
 (0)