Reputation: 2081
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
@EnableCaching
public 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=redis
spring.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 counter
cache_gets_total{application="business-service",cache="cacheA",cacheManager="cacheManager",name="cacheA",result="miss",} 0.0
cache_gets_total{application="business-service",cache="cacheA",cacheManager="cacheManager",name="cacheA",result="pending",} 0.0
cache_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.
Upvotes: 1
Views: 5394
Reputation: 116051
You are creating the RedisCacheManager
yourself so the cache manager auto-configuration has backed off. As a result, spring.cache.type=redis
is unnecessary and, more importantly, spring.cache.redis.enable-statistics=true
will have no effect.
To enable statistics on your RedisCacheManager
, call the enableStatistics()
method on the builder in your cacheManager
@Bean
method.
Upvotes: 7