client_java icon indicating copy to clipboard operation
client_java copied to clipboard

Caffeine cache_puts_total counter is always zero

Open KazzmanK opened this issue 6 months ago • 5 comments

Hi!

Please take a look into issue. Total put counter is constantly zero, other counters are alive.

prometheus-metrics-instrumentation-caffeine 1.3.8 caffeine 3.2.0

KazzmanK avatar Jun 17 '25 15:06 KazzmanK

Please include steps to reproduce and the metric name that you'd expect.

zeitlinger avatar Jun 24 '25 10:06 zeitlinger

Hi! Thank you for response

I have a Boot 3.5 project with a configuration class


@Slf4j
@Configuration
public class CaffeineConfig {

    @Value("${lsl.CACHE_DURATION:15}")
    private int cacheDuration;

    @Value("${lsl.MAX_CACHE_SIZE:1000}")
    private int maxCacheSize;

    @Bean
    public Cache<String, Tpe> tCache() {
        return Caffeine.newBuilder()
                .recordStats()
                .expireAfterWrite(cacheDuration, TimeUnit.SECONDS)
                .initialCapacity(100)
                .maximumSize(maxCacheSize)
                .build();
    }

    @Bean
    public CaffeineCacheMetrics<String, Tpe, Cache<String, Tpe>> caffeineCacheMetrics(
            Cache<String, Tpe> tCache) {
        return new CaffeineCacheMetrics<>(tCache, "tCache", null);
    }
}

after, I`m autowire beans in service

    @Autowired
    Cache<String, Tpe> tCache;

.....

   Tpe t = tCache.getIfPresent(key);
   tCache.put(key, t);

but after a while stats are /actuator/prometheus

# HELP cache_eviction_weight_total The sum of weights of evicted entries. This total does not include manual invalidations.
# TYPE cache_eviction_weight_total counter
cache_eviction_weight_total{cache="tCache"} 19327.0
# HELP cache_evictions_total The number of times the cache was evicted.
# TYPE cache_evictions_total counter
cache_evictions_total{cache="tCache"} 19327.0
# HELP cache_gets_total The number of times cache lookup methods have returned a cached (hit) or uncached (newly loaded or null) value (miss).
# TYPE cache_gets_total counter
cache_gets_total{cache="tCache",result="hit"} 12655.0
cache_gets_total{cache="tCache",result="miss"} 19329.0
# HELP cache_puts_total The number of entries added to the cache
# TYPE cache_puts_total counter
cache_puts_total{cache="tCache"} 0.0
# HELP cache_size The number of entries in this cache. This may be an approximation, depending on the type of cache.
# TYPE cache_size gauge
cache_size{cache="tCache"} 2.0

as you can see cache_puts_total is zero

KazzmanK avatar Jun 26 '25 12:06 KazzmanK

heads up: it will take some time until I can look into the issue - help is very welcome 😄

zeitlinger avatar Jun 26 '25 13:06 zeitlinger

Hey,

CaffeineCacheMetrics is not the collector in prometheus-metrics-instrumentation-caffeine, but coming from micrometer.

The one provided by prometheus-metrics-instrumentation-caffeine is CacheMetricsCollector. The counters are also prefixed with caffeine_

pheyken avatar Jul 04 '25 08:07 pheyken

Hey @KazzmanK, as @pheyken points out, it doesn't look like you are using prometheus for your caffeine metrics, so you might want to open an issue with micrometer instead.

jaydeluca avatar Aug 08 '25 13:08 jaydeluca