|
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; |
9 | 7 | import com.typesafe.config.Config; |
10 | 8 | import io.github.mweirauch.micrometer.jvm.extras.ProcessMemoryMetrics; |
11 | 9 | import io.github.mweirauch.micrometer.jvm.extras.ProcessThreadMetrics; |
|
42 | 40 | import io.prometheus.client.CollectorRegistry; |
43 | 41 | import io.prometheus.client.dropwizard.DropwizardExports; |
44 | 42 | import io.prometheus.client.exporter.PushGateway; |
45 | | -import java.lang.reflect.Field; |
46 | 43 | import java.time.Duration; |
47 | 44 | import java.util.ArrayList; |
48 | 45 | import java.util.HashMap; |
@@ -458,43 +455,14 @@ public static <K, V> void registerCache( |
458 | 455 |
|
459 | 456 | /** |
460 | 457 | * 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 |
462 | 459 | */ |
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) { |
470 | 462 | 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); |
498 | 466 | } |
499 | 467 |
|
500 | 468 | /** |
|
0 commit comments