Class CacheMetricsCollector
java.lang.Object
io.prometheus.metrics.instrumentation.guava.CacheMetricsCollector
- All Implemented Interfaces:
MultiCollector
Collect metrics from Guava's com.google.common.cache.Cache.
// Note that `recordStats()` is required to gather non-zero statistics
Cache<String, String> cache = CacheBuilder.newBuilder().recordStats().build();
CacheMetricsCollector cacheMetrics = new CacheMetricsCollector();
PrometheusRegistry.defaultRegistry.register(cacheMetrics);
cacheMetrics.addCache("mycache", cache);
Exposed metrics are labeled with the provided cache name.
With the example above, sample metric names would be:
guava_cache_hit_total{cache="mycache"} 10.0
guava_cache_miss_total{cache="mycache"} 3.0
guava_cache_requests_total{cache="mycache"} 13.0
guava_cache_eviction_total{cache="mycache"} 1.0
guava_cache_size{cache="mycache"} 5.0
Additionally, if the cache includes a loader, the following metrics would be provided:
guava_cache_load_failure_total{cache="mycache"} 2.0
guava_cache_loads_total{cache="mycache"} 7.0
guava_cache_load_duration_seconds_count{cache="mycache"} 7.0
guava_cache_load_duration_seconds_sum{cache="mycache"} 0.0034
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final ConcurrentMap<String,com.google.common.cache.Cache<?, ?>> private static final Stringprivate static final Stringprivate static final Stringprivate static final Stringprivate static final Stringprivate static final Stringprivate static final Stringprivate static final Stringprivate static final double -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdd or replace the cache with the given name.voidclear()Remove all caches.collect()Called when the Prometheus server scrapes metrics.This is called in two places: During registration to check if a metric with that name already exists.com.google.common.cache.Cache<?,?> removeCache(String cacheName) Remove the cache with the given name.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.prometheus.metrics.model.registry.MultiCollector
collect, collect, collect, getLabelNames, getMetadata, getMetricType
-
Field Details
-
NANOSECONDS_PER_SECOND
private static final double NANOSECONDS_PER_SECOND- See Also:
-
METRIC_NAME_CACHE_HIT
- See Also:
-
METRIC_NAME_CACHE_MISS
- See Also:
-
METRIC_NAME_CACHE_REQUESTS
- See Also:
-
METRIC_NAME_CACHE_EVICTION
- See Also:
-
METRIC_NAME_CACHE_LOAD_FAILURE
- See Also:
-
METRIC_NAME_CACHE_LOADS
- See Also:
-
METRIC_NAME_CACHE_SIZE
- See Also:
-
METRIC_NAME_CACHE_LOAD_DURATION_SECONDS
- See Also:
-
ALL_METRIC_NAMES
-
children
-
-
Constructor Details
-
CacheMetricsCollector
public CacheMetricsCollector()
-
-
Method Details
-
addCache
Add or replace the cache with the given name.Any references any previous cache with this name is invalidated.
- Parameters:
cacheName- The name of the cache, will be the metrics label valuecache- The cache being monitored
-
removeCache
Remove the cache with the given name.Any references to the cache are invalidated.
- Parameters:
cacheName- cache to be removed
-
clear
public void clear()Remove all caches.Any references to all caches are invalidated.
-
collect
Description copied from interface:MultiCollectorCalled when the Prometheus server scrapes metrics.- Specified by:
collectin interfaceMultiCollector
-
getPrometheusNames
Description copied from interface:MultiCollectorThis is called in two places:- During registration to check if a metric with that name already exists.
- During scrape to check if the collector can be skipped because a name filter is present and all names are excluded.
Returning an empty list means checks are omitted (registration metric always succeeds), and the collector is always scraped (if a name filter is present and all names are excluded the result is dropped).
If your collector returns a constant list of metrics that have names that do not change at runtime it is a good idea to overwrite this and return the names.
- Specified by:
getPrometheusNamesin interfaceMultiCollector
-