是否可以为默认的Spring Boot 2指标定义其他标签? [英] Is it possible to define additional tags for default Spring Boot 2 metrics?
问题描述
最近我用Micrometer
切换到了Spring Boot 2
.当我获得这些闪亮的新指标时,我与我们的DevOps成员进行了交谈,然后开始将它们导出到Telegraf
.
Recently I switched to Spring Boot 2
with Micrometer
. As I got these shiny new metrics, I talked with our DevOps guys and we started exporting them to Telegraf
.
为了区分不同的应用程序和应用程序节点,我们决定使用标签.这非常适合自定义指标,但现在我开始考虑预定义的指标.为了在默认指标上达到同样的效果,我还需要为它们添加额外的标签.
To distinguish between different applications and application nodes, we decided to use tags. This works perfectly for custom metrics, but now I started thinking about the pre-defined. To achieve the same for default metrics, I need an ability to add extra tags for them as well.
有可能实现这一目标吗?我这样做正确吗?
Is it possible to achieve this? Am I doing this right?
我尝试了下一种方法:
I tried next approach:
@Component
public class MyMetricsImpl implements MyMetrics {
@Autowired
protected MyProperties myProperties;
@Autowired
protected MeterRegistry meterRegistry;
@PostConstruct
public void initialize() {
this.meterRegistry.config()
.commonTags(commonTags());
}
@Override
public List<Tag> commonTags() {
List<Tag> tags = new ArrayList<>();
tags.add(Tag.of("application", myProperties.getApplicationName()));
tags.add(Tag.of("node", myProperties.getNodeName()));
return tags;
}
}
问题是我的指标正常运行,甚至某些Boot的指标(至少是http.server.requests
)也可以看到我的标签.但是jvm.*
,system.*
,tomcat.*
和许多其他标签仍然没有所需的标签.
The problem is that my metrics behave correctly and even some of the Boot's metrics (at least http.server.requests
) see my tags. But jvm.*
, system.*
, tomcat.*
and many others still don't have the needed tags.
推荐答案
如果您正在寻找通用标签支持,则可以通过注册MeterFilter
来实现.
If you are looking for common tags support, you can do it by registering a MeterFilter
doing it.
请参见此提交或使用即将推出的Spring Boot 2.1.0.M1 ,您可以使用以下属性:
With the upcoming Spring Boot 2.1.0.M1, you can use the following properties:
management.metrics.tags.*= # Common tags that are applied to every meter.
See the reference for details.
更新:
问题已更新,因此我使用基于MeterFilter
的方法检查了更新后的问题,并确认其工作方式如下:
As the question has been updated, I checked the updated question with this MeterFilter
-based approach and confirmed it's working as follows:
请求:http://localhost:8080/actuator/metrics/jvm.gc.memory.allocated
响应:
{
"name" : "jvm.gc.memory.allocated",
"measurements" : [ {
"statistic" : "COUNT",
"value" : 1.98180864E8
} ],
"availableTags" : [ {
"tag" : "stack",
"values" : [ "prod" ]
}, {
"tag" : "region",
"values" : [ "us-east-1" ]
} ]
}
我没有检查更新问题中提供的方法,但除非有任何理由坚持使用,否则我只会使用经过验证的基于MeterFilter
的方法.
I didn't check the approach which has been provided in the updated question but I'd just use the proven MeterFilter
-based approach unless there's any reason to stick with the approach.
第二次更新:
2nd UPDATED:
我研究了该方法,并能够使用此分支.
I looked into the approach and was able to reproduce it with this branch.
在某些地方已经注册了一些度量,现在在@PostConstruct
中应用通用标签为时已晚. http.server.requests
起作用的原因是它将在第一个请求中注册.尝试在
It's too late to apply common tags in @PostConstruct
as some metrics have been registered already. The reason why http.server.requests
works is that it will be registered with the first request. Try to put a breakpoint on the point of filters' application if you're interested in it.
简而言之,请尝试上述方法,该方法类似于即将推出的Spring Boot开箱即用支持.
In short, try the above approach which is similar to the upcoming Spring Boot out-of-box support.
这篇关于是否可以为默认的Spring Boot 2指标定义其他标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!