Prometheus启动异常
1.问题表现
日志报错: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 containing tag keys [method, status, uri]. The meter you are attempting to register has keys [exception, method, status, uri].
2.分析原因
根据错误描述,Prometheus要求相同的meters 具有相同的标记键集,目前具有了两种标记键集方式( [method, status, uri],[exception, method, status, uri] ),所以会报出异常。
3.查询原因
如果配置文件中没有添加“management.metrics.web.server.auto-time-requests=false”,默认为 开启自动检测所有请求。
源码MetricsProperties类中的内部类:
4. 解决方案
方案一:
配置文件中增加management.metrics.web.server.auto-time-requests=false设置为false(默认为true),关闭自动检测所有请求的配置。
方案二:
重写WebMvcTagsProvider的实现,目的是去掉 WebMvcTags.exception (exception),将所有的标记键集方式统一。主要是重写httpRequestTags方法,去掉WebMvcTags.exception(exception)。最后将重写后 的实现类注 入bean。
-
总结
Prometheus默认会开启自动检测所有请求,此时要求所有请求具有相同的标记键集。默认的WebMvcTagsProvider实现类为
DefaultWebMvcTagsProvide
在获取请求标记时调用了WebMvcTags.exception(ex),而有些接口会调用不同的标记键集(eg:getLongRequestTags),当开启自动检测所有请求时,Prometheus认为是不安全的,但仍可以收集日志。因此,我们可以自行实现WebMvcTagsProvider,重写httpRequestTags方法(不同版本方法名不同,有些版本方法名为getTag)采用相同的标记键集来规避此异常信息。