|
4 | 4 | import com.codahale.metrics.Metric; |
5 | 5 | import com.codahale.metrics.MetricRegistry; |
6 | 6 | import com.google.common.cache.Cache; |
| 7 | +import com.google.common.cache.CacheBuilder; |
| 8 | +import com.google.common.cache.CacheLoader; |
7 | 9 | import com.typesafe.config.Config; |
8 | 10 | import io.github.mweirauch.micrometer.jvm.extras.ProcessMemoryMetrics; |
9 | 11 | import io.github.mweirauch.micrometer.jvm.extras.ProcessThreadMetrics; |
|
39 | 41 | import io.prometheus.client.CollectorRegistry; |
40 | 42 | import io.prometheus.client.dropwizard.DropwizardExports; |
41 | 43 | import io.prometheus.client.exporter.PushGateway; |
| 44 | +import java.lang.reflect.Field; |
42 | 45 | import java.time.Duration; |
43 | 46 | import java.util.ArrayList; |
44 | 47 | import java.util.HashMap; |
@@ -73,6 +76,7 @@ public class PlatformMetricsRegistry { |
73 | 76 | private static final String LOGGING_REPORTER_NAME = "logging"; |
74 | 77 | private static final String TESTING_REPORTER_NAME = "testing"; |
75 | 78 | private static final String CONSOLE_REPORTER_NAME = "console"; |
| 79 | + private static final String CACHE_MAX_SIZE_GAUGE = "cache.max.size"; |
76 | 80 |
|
77 | 81 | /** |
78 | 82 | * 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( |
451 | 455 | GuavaCacheMetrics.monitor(meterRegistry, guavaCache, cacheName, toIterable(tags)); |
452 | 456 | } |
453 | 457 |
|
| 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 | + |
454 | 497 | /** |
455 | 498 | * Registers metrics for the given executor service with the service's metric registry and reports |
456 | 499 | * them periodically to the configured reporters. Apart from the given tags, the reporting |
|
0 commit comments