Quantcast
Channel: Active questions tagged redis+java - Stack Overflow
Viewing all articles
Browse latest Browse all 2204

Redis cache metrics doesn't work in SpringBoot version 2.4.2

$
0
0

We are trying to expose our redis cache metrics to Prometheus. Below are what we've done.

We have a class CachingConfig as below,

@Configuration@EnableCachingpublic class CachingConfig {  private final Duration cacheEntryTtl;  public CachingConfig(      @Value("${spring.cache.redis.entryTtl}")      final Duration cacheEntryTtl  ) {    this.cacheEntryTtl = cacheEntryTtl;  }  @Bean  public CacheManager cacheManager(final RedisConnectionFactory redisConnectionFactory) {    final Map<String, RedisCacheConfiguration> cacheConfigurations = new HashMap<>();    cacheConfigurations.put("cacheA",cacheConfiguration(cacheEntryTtl));    cacheConfigurations.put("cacheB",cacheConfiguration(cacheEntryTtl));    return RedisCacheManager.builder(redisConnectionFactory)        .cacheDefaults(cacheConfiguration(cacheEntryTtl))        .withInitialCacheConfigurations(cacheConfigurations)        .build();  }}

And then we are using the Redis cache in our class like below.

public class BusinessService {    public static final String CACHE_A_NAME = "cacheA"    private final BusinessServiceClient businessServiceClient;    private final CacheManager cacheManager;    private final CacheMetricsRegistrar cacheMetricsRegistrar;    @PostConstruct    public void postConstruct() {        final Cache cache = cacheManager.getCache(CACHE_A_NAME);        cacheMetricsRegistrar.bindCacheToRegistry(cache);    }    @Cacheable(cacheNames = CACHE_A_NAME)    public Set<String> getOwnersOfProviderAccount(String para1, String para2) {        return businessServiceClient.getResonponse(para1, para2);    }}

And according to this, I also added following lines in our application.properties file.

spring.cache.type=redisspring.cache.redis.enable-statistics=true

So in theory, the Redis cache metrics should be able to work, however when I check our cache metrics from following URLs.

GET .../actuator/metrics/cache.gets?tag=name:cacheA

the response is always like below, the COUNT is always ZERO, it seems the statistics not working, our Redis cache works though.

{"name":"cache.gets","description":"The number of pending requests","baseUnit":null,"measurements":[      {"statistic":"COUNT","value":0.0      }   ],"availableTags":[      {"tag":"result","values":["hit","pending","miss"         ]      },      {"tag":"cache","values":["cacheA"         ]      },      {"tag":"application","values":["business-service"         ]      },      {"tag":"cacheManager","values":["cacheManager"         ]      }   ]}

And also if we check the metrics from /management/prometheus, here are what we get, all values are ZERO.

# HELP cache_gets_total the number of times cache lookup methods have returned an uncached (newly loaded) value, or null# TYPE cache_gets_total countercache_gets_total{application="business-service",cache="cacheA",cacheManager="cacheManager",name="cacheA",result="miss",} 0.0cache_gets_total{application="business-service",cache="cacheA",cacheManager="cacheManager",name="cacheA",result="pending",} 0.0cache_gets_total{application="business-service",cache="cacheA",cacheManager="cacheManager",name="cacheA",result="hit",} 0.0

Is there anything that I miss when I'm configuring the Redis cache metrics? Thanks, any constructive suggestion is appreciated.


Viewing all articles
Browse latest Browse all 2204

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>