0

I'm using spring boot 1.5 and I cannot expose guava cache statistics in /prometheus endpoint. Eventually I can expose it as JMX but then I dont know how to wire these things up. Is there some easier way how to do it?

I am creating the cache with cache builder as following:

    @Bean
    @Primary
    public CacheManager cacheManager() {
        final GuavaCacheManager cacheManager = new GuavaCacheManager();
        final CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder()
                .maximumSize(5000)
                .recordStats()
                .expireAfterWrite(1, TimeUnit.HOURS);
        cacheManager.setCacheBuilder(cacheBuilder);
        cacheManager.setCacheNames(Lists.newArrayList("TEST_CACHE"));
        return cacheManager;
    }

I am using .recordStats() method as well.

Mejmo
  • 2,363
  • 9
  • 35
  • 54

1 Answers1

1

You can monitor the Guava chache metrics by using the GuavaCacheMetrics binder

GuavaCacheBuilder.monitor(meterRegistry, aCache, "myCacheName")

Notice since you are using a cache builder with the CacheManager you would need to make that monitor call for each individual cache.

There are other ways to approach this, and the CacheManager is automatically instrumented in SpringBot 2.x I believe. So upgrading will give you a simpler integration.

checketts
  • 14,167
  • 10
  • 53
  • 82
  • Thing is, it does need a cache which is extended from com.google.common.cache.Cache, but the cache class got from CacheManager is GuavaCache from org.springframework package. So I am not able to use this method. – Mejmo May 15 '19 at 08:43
  • Thanks, just want to mention that it works in conjunction with `recordStats()` method in `CacheBuilder`, otherwise all metrics will be reported as zeros. – alex_bondar Sep 24 '19 at 20:54