我最近将 Spring Boot 应用程序从 1.5 升级到 2.0.1。我还使用千分尺将 prometheus 集成迁移到了新的执行器方法。大多数事情现在都可以工作 - 包括一些自定义计数器和仪表。
我注意到新的普罗米修斯端点 /actuator/prometheus
不再发布 spring 缓存指标(大小和命中率)。
我唯一能找到的是 this问题及其相关 commit .
我仍然无法获得 prometheus 导出的缓存指标。我尝试设置一些属性:
management.metrics.cache.instrument-cache=true
spring.cache.cache-names=cache1Name,cache2Name...
但没有什么是真正有效的。我可以看到 Hazelcast 缓存管理器启动,注册缓存管理器 bean 等等 - 但都没有
/metrics
也不是 /prometheus
显示任何统计数据。缓存使用 @Cacheable
填充注解。这适用于 Spring Boot 1.5 - 我认为通过 Hazelcast 通过 JMX 公开其指标,而 prometheus 导出器从那里获取它?现在不确定如何将它们连接在一起。欢迎任何提示!
最佳答案
正如您已经回答了我的问题,我可以为此提供答案。
my caches get created through scheduled tasks later on
那么文档的这一部分适用于您:
Only caches that are available on startup are bound to the registry. For caches created on-the-fly or programmatically after the startup phase, an explicit registration is required. A CacheMetricsRegistrar bean is made available to make that process easier.
所以你必须自己注册这样的缓存,希望这很容易,比如:
public class MyComponent {
private final CacheMetricsRegistrar cacheMetricsRegistrar;
private final CacheManager cacheManager
public MyComponent(CacheMetricsRegistrar cacheMetricsRegistrar,
CacheManager cacheManager) { ... }
public void register() {
// you have just registered cache "xyz"
Cache xyz = this.cacheManager.getCache("xyz");
this.cacheMetricsRegistrar.bindCacheToRegistry(xyz);
}
}
您可以将此代码包含在现有代码中。如果您不想这样做,那么您需要在现有代码之后运行其他东西来将这些缓存注册到注册表。
关于spring-boot - 升级到 Spring Boot 2 后,如何向 prometheus 公开缓存指标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49697063/