1

我们正在尝试向 Prometheus 公开我们的 redis 缓存指标。以下是我们所做的。

我们有一个类CachingConfig如下,

@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();
  }
}

然后我们在我们的类中使用 Redis 缓存,如下所示。

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);
    }
}

根据这个,我还在我们的application.properties文件中添加了以下几行。

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

所以理论上,Redis 缓存指标应该能够工作,但是当我从以下 URL 检查我们的缓存指标时。

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

响应总是如下所示,COUNT 总是为零,似乎统计数据不起作用,但我们的 Redis 缓存仍然有效。

{
   "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"
         ]
      }
   ]
}

而且,如果我们检查来自 的指标/management/prometheus,这就是我们得到的,所有值都是零。

# 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

我在配置 Redis 缓存指标时有什么遗漏的吗?谢谢,任何建设性的建议表示赞赏。

4

1 回答 1

1

您正在创建RedisCacheManager自己,因此缓存管理器自动配置已退出。结果,spring.cache.type=redis是不必要的,更重要的是,spring.cache.redis.enable-statistics=true不会有任何效果。

要启用您的统计信息,请在您RedisCacheManager的方法中调用enableStatistics()构建器上的cacheManager @Bean方法。

于 2021-02-25T20:13:22.473 回答