|
| 1 | +--- |
| 2 | +title: Caffeine Cache |
| 3 | +weight: 1 |
| 4 | +--- |
| 5 | + |
| 6 | +The Caffeine instrumentation module, added in version 1.3.2, translates observability data |
| 7 | +provided by caffeine `Cache` objects into prometheus metrics. |
| 8 | + |
| 9 | +{{< tabs "uniqueid" >}} |
| 10 | +{{< tab "Gradle" >}} |
| 11 | +``` |
| 12 | +implementation 'io.prometheus:prometheus-metrics-instrumentation-caffeine:1.3.2' |
| 13 | +``` |
| 14 | +{{< /tab >}} |
| 15 | +{{< tab "Maven" >}} |
| 16 | +```xml |
| 17 | +<dependency> |
| 18 | + <groupId>io.prometheus</groupId> |
| 19 | + <artifactId>prometheus-metrics-instrumentation-caffeine</artifactId> |
| 20 | + <version>1.3.2</version> |
| 21 | +</dependency> |
| 22 | +``` |
| 23 | +{{< /tab >}} |
| 24 | +{{< /tabs >}} |
| 25 | + |
| 26 | +In order to collect metrics: |
| 27 | + |
| 28 | + * A single `CacheMetricsCollector` instance must be registered with the registry; |
| 29 | + * Multiple `CacheMetricsCollector` instances cannot be registered with the same registry; |
| 30 | + * The `Cache` object must be instantiated with the `recordStats()` option, and then added to the |
| 31 | + `CacheMetricsCollector` instance with a unique name, which will be used as the value of the |
| 32 | + `cache` label on the exported metrics; |
| 33 | + * If the `recordStats` option is not set, most metrics will only return zero values; |
| 34 | + |
| 35 | +```java |
| 36 | +var cache = Caffeine.newBuilder().recordStats().build(); |
| 37 | +var cacheMetrics = new CacheMetricsCollector(); |
| 38 | +PrometheusRegistry.defaultRegistry.register(cacheMetrics); |
| 39 | +cacheMetrics.addCache("mycache", cache); |
| 40 | +``` |
| 41 | + |
| 42 | +All example metrics on this page will use the `mycache` label value. |
| 43 | + |
| 44 | +Generic Cache Metrics |
| 45 | +--------------------- |
| 46 | + |
| 47 | +For all cache instances, the following metrics will be available: |
| 48 | + |
| 49 | +``` |
| 50 | +# TYPE caffeine_cache_hit counter |
| 51 | +# HELP caffeine_cache_hit Cache hit totals |
| 52 | +caffeine_cache_hit_total{cache="mycache"} 10.0 |
| 53 | +# TYPE caffeine_cache_miss counter |
| 54 | +# HELP caffeine_cache_miss Cache miss totals |
| 55 | +caffeine_cache_miss_total{cache="mycache"} 3.0 |
| 56 | +# TYPE caffeine_cache_requests counter |
| 57 | +# HELP caffeine_cache_requests Cache request totals, hits + misses |
| 58 | +caffeine_cache_requests_total{cache="mycache"} 13.0 |
| 59 | +# TYPE caffeine_cache_eviction counter |
| 60 | +# HELP caffeine_cache_eviction Cache eviction totals, doesn't include manually removed entries |
| 61 | +caffeine_cache_eviction_total{cache="mycache"} 1.0 |
| 62 | +# TYPE caffeine_cache_estimated_size |
| 63 | +# HELP caffeine_cache_estimated_size Estimated cache size |
| 64 | +caffeine_cache_estimated_size{cache="mycache"} 5.0 |
| 65 | +``` |
| 66 | + |
| 67 | +Loading Cache Metrics |
| 68 | +--------------------- |
| 69 | + |
| 70 | +If the cache is an instance of `LoadingCache`, i.e. it is built with a `loader` function that is |
| 71 | +managed by the cache library, then metrics for observing load time and load failures become |
| 72 | +available: |
| 73 | + |
| 74 | +``` |
| 75 | +# TYPE caffeine_cache_load_failure counter |
| 76 | +# HELP caffeine_cache_load_failure Cache load failures |
| 77 | +caffeine_cache_load_failure_total{cache="mycache"} 10.0 |
| 78 | +# TYPE caffeine_cache_loads counter |
| 79 | +# HELP caffeine_cache_loads Cache loads: both success and failures |
| 80 | +caffeine_cache_loads_total{cache="mycache"} 3.0 |
| 81 | +# TYPE caffeine_cache_load_duration_seconds summary |
| 82 | +# HELP caffeine_cache_load_duration_seconds Cache load duration: both success and failures |
| 83 | +caffeine_cache_load_duration_seconds_count{cache="mycache"} 7.0 |
| 84 | +caffeine_cache_load_duration_seconds_sum{cache="mycache"} 0.0034 |
| 85 | +``` |
| 86 | + |
| 87 | +Weighted Cache Metrics |
| 88 | +---------------------- |
| 89 | + |
| 90 | +If the cache is weighted, i.e. it defines a `weigher` function, then the following metrics |
| 91 | +become interesting: |
| 92 | + |
| 93 | +``` |
| 94 | +# TYPE caffeine_cache_eviction_weight gauge |
| 95 | +# HELP caffeine_cache_eviction_weight Cache eviction weight |
| 96 | +caffeine_cache_eviction_weight{cache="mycache"} 5.0 |
| 97 | +``` |
| 98 | + |
| 99 | +Note: while `caffeine_cache_eviction_weight` is exported as a `gauge` metric, it represents |
| 100 | +a monotonicaly increasing value. Also, in the case where the cache does not define a `weigher` |
| 101 | +function, it will return the same values as `caffeine_cache_eviction_total`. |
0 commit comments