Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -451,6 +455,45 @@ public static <K, V> 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 <K, V> Cache<K, V> registerAndGetCache(
String cacheName,
CacheBuilder<Object, Object> guavaCacheBuilder,
CacheLoader<? super K, V> loader,
Map<String, String> tags) {
reportCacheMaxSize(cacheName, guavaCacheBuilder);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we be propagating the tags to cache max size metric as well?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed offline. will have an immediate followup PR to fix this.

Cache<K, V> 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 <K, V> Cache<K, V> registerAndGetCache(
String cacheName, CacheBuilder<Object, Object> guavaCacheBuilder, Map<String, String> tags) {
reportCacheMaxSize(cacheName, guavaCacheBuilder);
Cache<K, V> guavaCache = guavaCacheBuilder.build();
GuavaCacheMetrics.monitor(meterRegistry, guavaCache, cacheName, toIterable(tags));
return guavaCache;
}

private static <K, V> void reportCacheMaxSize(
String cacheName, CacheBuilder<K, V> 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
Expand Down