public class CacheMetricsCollector extends io.prometheus.client.Collector
// Note that `recordStats()` is required to gather non-zero statistics
Cache<String, String> cache = Caffeine.newBuilder().recordStats().build();
CacheMetricsCollector cacheMetrics = new CacheMetricsCollector().register();
cacheMetrics.addCache("mycache", cache);
Exposed metrics are labeled with the provided cache name.
With the example above, sample metric names would be:
caffeine_cache_hit_total{cache="mycache"} 10.0
caffeine_cache_miss_total{cache="mycache"} 3.0
caffeine_cache_requests_total{cache="mycache"} 13.0
caffeine_cache_eviction_total{cache="mycache"} 1.0
caffeine_cache_estimated_size{cache="mycache"} 5.0
Additionally if the cache includes a loader, the following metrics would be provided:
caffeine_cache_load_failure_total{cache="mycache"} 2.0
caffeine_cache_loads_total{cache="mycache"} 7.0
caffeine_cache_load_duration_seconds_count{cache="mycache"} 7.0
caffeine_cache_load_duration_seconds_sum{cache="mycache"} 0.0034
| Modifier and Type | Field and Description |
|---|---|
protected ConcurrentMap<String,com.github.benmanes.caffeine.cache.Cache> |
children |
| Constructor and Description |
|---|
CacheMetricsCollector() |
| Modifier and Type | Method and Description |
|---|---|
void |
addCache(String cacheName,
com.github.benmanes.caffeine.cache.AsyncLoadingCache cache)
Add or replace the cache with the given name.
|
void |
addCache(String cacheName,
com.github.benmanes.caffeine.cache.Cache cache)
Add or replace the cache with the given name.
|
void |
clear()
Remove all caches.
|
List<io.prometheus.client.Collector.MetricFamilySamples> |
collect() |
com.github.benmanes.caffeine.cache.Cache |
removeCache(String cacheName)
Remove the cache with the given name.
|
protected final ConcurrentMap<String,com.github.benmanes.caffeine.cache.Cache> children
public CacheMetricsCollector()
public void addCache(String cacheName, com.github.benmanes.caffeine.cache.Cache cache)
Any references any previous cache with this name is invalidated.
cacheName - The name of the cache, will be the metrics label valuecache - The cache being monitoredpublic void addCache(String cacheName, com.github.benmanes.caffeine.cache.AsyncLoadingCache cache)
Any references any previous cache with this name is invalidated.
cacheName - The name of the cache, will be the metrics label valuecache - The cache being monitoredpublic com.github.benmanes.caffeine.cache.Cache removeCache(String cacheName)
Any references to the cache are invalidated.
cacheName - cache to be removedpublic void clear()
Any references to all caches are invalidated.
Copyright © 2019. All rights reserved.