|
| 1 | +package io.prometheus.client.cache.caffeine; |
| 2 | + |
| 3 | +import com.github.benmanes.caffeine.cache.AsyncLoadingCache; |
| 4 | +import com.github.benmanes.caffeine.cache.Cache; |
| 5 | +import com.github.benmanes.caffeine.cache.LoadingCache; |
| 6 | +import com.github.benmanes.caffeine.cache.stats.CacheStats; |
| 7 | +import io.prometheus.client.Collector; |
| 8 | +import io.prometheus.client.CounterMetricFamily; |
| 9 | +import io.prometheus.client.GaugeMetricFamily; |
| 10 | +import io.prometheus.client.SummaryMetricFamily; |
| 11 | + |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.Arrays; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.concurrent.ConcurrentHashMap; |
| 17 | +import java.util.concurrent.ConcurrentMap; |
| 18 | + |
| 19 | + |
| 20 | +/** |
| 21 | + * Collect metrics from Caffiene's com.github.benmanes.caffeine.cache.Cache. |
| 22 | + * <p> |
| 23 | + * <pre>{@code |
| 24 | + * |
| 25 | + * // Note that `recordStats()` is required to gather non-zero statistics |
| 26 | + * Cache<String, String> cache = Caffeine.newBuilder().recordStats().build(); |
| 27 | + * CacheMetricsCollector cacheMetrics = new CacheMetricsCollector().register(); |
| 28 | + * cacheMetrics.addCache("mycache", cache); |
| 29 | + * |
| 30 | + * }</pre> |
| 31 | + * |
| 32 | + * Exposed metrics are labeled with the provided cache name. |
| 33 | + * |
| 34 | + * With the example above, sample metric names would be: |
| 35 | + * <pre> |
| 36 | + * caffeine_cache_hit_total{cache="mycache"} 10.0 |
| 37 | + * caffeine_cache_miss_total{cache="mycache"} 3.0 |
| 38 | + * caffeine_cache_requests_total{cache="mycache"} 13.0 |
| 39 | + * caffeine_cache_eviction_total{cache="mycache"} 1.0 |
| 40 | + * </pre> |
| 41 | + * |
| 42 | + * Additionally if the cache includes a loader, the following metrics would be provided: |
| 43 | + * <pre> |
| 44 | + * caffeine_cache_load_failure_total{cache="mycache"} 2.0 |
| 45 | + * caffeine_cache_loads_total{cache="mycache"} 7.0 |
| 46 | + * caffeine_cache_load_duration_seconds_count{cache="mycache"} 7.0 |
| 47 | + * caffeine_cache_load_duration_seconds_sum{cache="mycache"} 0.0034 |
| 48 | + * </pre> |
| 49 | + * |
| 50 | + */ |
| 51 | +public class CacheMetricsCollector extends Collector { |
| 52 | + protected final ConcurrentMap<String, Cache> children = new ConcurrentHashMap<String, Cache>(); |
| 53 | + |
| 54 | + /** |
| 55 | + * Add or replace the cache with the given name. |
| 56 | + * <p> |
| 57 | + * Any references any previous cache with this name is invalidated. |
| 58 | + * |
| 59 | + * @param cacheName The name of the cache, will be the metrics label value |
| 60 | + * @param cache The cache being monitored |
| 61 | + */ |
| 62 | + public void addCache(String cacheName, Cache cache) { |
| 63 | + children.put(cacheName, cache); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Add or replace the cache with the given name. |
| 68 | + * <p> |
| 69 | + * Any references any previous cache with this name is invalidated. |
| 70 | + * |
| 71 | + * @param cacheName The name of the cache, will be the metrics label value |
| 72 | + * @param cache The cache being monitored |
| 73 | + */ |
| 74 | + public void addCache(String cacheName, AsyncLoadingCache cache) { |
| 75 | + children.put(cacheName, cache.synchronous()); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Remove the cache with the given name. |
| 80 | + * <p> |
| 81 | + * Any references to the cache are invalidated. |
| 82 | + * |
| 83 | + * @param cacheName cache to be removed |
| 84 | + */ |
| 85 | + public Cache removeCache(String cacheName) { |
| 86 | + return children.remove(cacheName); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Remove all caches. |
| 91 | + * <p> |
| 92 | + * Any references to all caches are invalidated. |
| 93 | + */ |
| 94 | + public void clear(){ |
| 95 | + children.clear(); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public List<MetricFamilySamples> collect() { |
| 100 | + List<MetricFamilySamples> mfs = new ArrayList<MetricFamilySamples>(); |
| 101 | + List<String> labelNames = Arrays.asList("cache"); |
| 102 | + |
| 103 | + CounterMetricFamily cacheHitTotal = new CounterMetricFamily("caffeine_cache_hit_total", |
| 104 | + "Cache hit totals", labelNames); |
| 105 | + mfs.add(cacheHitTotal); |
| 106 | + |
| 107 | + CounterMetricFamily cacheMissTotal = new CounterMetricFamily("caffeine_cache_miss_total", |
| 108 | + "Cache miss totals", labelNames); |
| 109 | + mfs.add(cacheMissTotal); |
| 110 | + |
| 111 | + CounterMetricFamily cacheRequestsTotal = new CounterMetricFamily("caffeine_cache_requests_total", |
| 112 | + "Cache request totals, hits + misses", labelNames); |
| 113 | + mfs.add(cacheRequestsTotal); |
| 114 | + |
| 115 | + CounterMetricFamily cacheEvictionTotal = new CounterMetricFamily("caffeine_cache_eviction_total", |
| 116 | + "Cache eviction totals, doesn't include manually removed entries", labelNames); |
| 117 | + mfs.add(cacheEvictionTotal); |
| 118 | + |
| 119 | + GaugeMetricFamily cacheEvictionWeight = new GaugeMetricFamily("caffeine_cache_eviction_weight", |
| 120 | + "Cache eviction weight", labelNames); |
| 121 | + mfs.add(cacheEvictionWeight); |
| 122 | + |
| 123 | + CounterMetricFamily cacheLoadFailure = new CounterMetricFamily("caffeine_cache_load_failure_total", |
| 124 | + "Cache load failures", labelNames); |
| 125 | + mfs.add(cacheLoadFailure); |
| 126 | + |
| 127 | + CounterMetricFamily cacheLoadTotal = new CounterMetricFamily("caffeine_cache_loads_total", |
| 128 | + "Cache loads: both success and failures", labelNames); |
| 129 | + mfs.add(cacheLoadTotal); |
| 130 | + |
| 131 | + SummaryMetricFamily cacheLoadSummary = new SummaryMetricFamily("caffeine_cache_load_duration_seconds", |
| 132 | + "Cache load duration: both success and failures", labelNames); |
| 133 | + mfs.add(cacheLoadSummary); |
| 134 | + |
| 135 | + for(Map.Entry<String, Cache> c: children.entrySet()) { |
| 136 | + List<String> cacheName = Arrays.asList(c.getKey()); |
| 137 | + CacheStats stats = c.getValue().stats(); |
| 138 | + |
| 139 | + try{ |
| 140 | + cacheEvictionWeight.addMetric(cacheName, stats.evictionWeight()); |
| 141 | + } catch (Exception e) { |
| 142 | + // EvictionWeight metric is unavailable, newer version of Caffeine is needed. |
| 143 | + } |
| 144 | + |
| 145 | + cacheHitTotal.addMetric(cacheName, stats.hitCount()); |
| 146 | + cacheMissTotal.addMetric(cacheName, stats.missCount()); |
| 147 | + cacheRequestsTotal.addMetric(cacheName, stats.requestCount()); |
| 148 | + cacheEvictionTotal.addMetric(cacheName, stats.evictionCount()); |
| 149 | + |
| 150 | + if(c.getValue() instanceof LoadingCache) { |
| 151 | + cacheLoadFailure.addMetric(cacheName, stats.loadFailureCount()); |
| 152 | + cacheLoadTotal.addMetric(cacheName, stats.loadCount()); |
| 153 | + |
| 154 | + cacheLoadSummary.addMetric(cacheName, stats.loadCount(), stats.totalLoadTime() / Collector.NANOSECONDS_PER_SECOND); |
| 155 | + } |
| 156 | + } |
| 157 | + return mfs; |
| 158 | + } |
| 159 | +} |
0 commit comments