Spring Boot 千分尺寄存器缓存指标

Posted

技术标签:

【中文标题】Spring Boot 千分尺寄存器缓存指标【英文标题】:Spring boot micrometer register cache metrics 【发布时间】:2020-03-15 22:04:37 【问题描述】:

我在为应用程序的 2 个实例创建指标时遇到问题。 我使用

为 hazelcast 缓存指标创建了指标
 HazelcastCacheMetrics.monitor(prometheusMeterRegistry, (IMap<?, ?>) cache.getNativeCache());

当我运行 1 个应用程序实例时一切正常,但当我运行 2 个实例时出现错误

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hazelcastCacheMetricConfig': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Prometheus requires that all meters with the same name have the same set of tag keys. There is already an existing meter named 'cache_size' containing tag keys [cache, cacheManager, name]. The meter you are attempting to register has keys [cache].

或者当我将注册的指标更改为

@Autowired
private CacheMetricsRegistrar cacheMetricsRegistrar;

cacheMetricsRegistrar.bindCacheToRegistry(cache, Tag.of("instance", podName));

我有这个错误

 Error creating bean with name 'hazelcastCacheMetricConfig': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Prometheus requires that all meters with the same name have the same set of tag keys. There is already an existing meter named 'cache_size' containing tag keys [cache, cacheManager, name]. The meter you are attempting to register has keys [cache, instance, name].

感谢您帮助解决此问题

【问题讨论】:

【参考方案1】:

您好,使用 MeterRegistryCustomizer 代替 HazelcastCacheMetrics

导入 io.micrometer.core.instrument.MeterRegistry; 导入 org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;

@Bean
MeterRegistryCustomizer<MeterRegistry> metricsCommonTags() 
    return registry -> registry.config().commonTags("application", "MYAPPNAME");

【讨论】:

感谢您的帮助,但是当我将 appName 添加到标签中时,我仍然有错误。有问题更新 你是从 spring props 添加 appName 吗?还是奇怪的编码? 来自环境属性 尝试这样实现,private void bindCacheToRegistry(String beanName, Cache cache) Tag cacheManagerTag = Tag.of("cacheManager", getCacheManagerName(beanName)); this.cacheMetricsRegistrar.bindCacheToRegistry(cache, cacheManagerTag); github.com/spring-projects/spring-boot/blob/master/…

以上是关于Spring Boot 千分尺寄存器缓存指标的主要内容,如果未能解决你的问题,请参考以下文章

千分尺发送指标为零 - Spring Boot

如何使用千分尺指定我想在spring-boot中使用的指标的白名单

如何使用千分尺指定我想要在spring-boot中使用的度量标准的白名单

Spring-boot千分尺定时器()它是如何工作的?

在 Spring Boot 中,如何在每次测试之前重置指标注册表?

使用 Micrometer 或 Spring Metrics 的 Prometheus 的 Spring Boot 指标