我正在尝试向 Prometheus 中的目标添加标签,修改 SpringBoot 中的文件。
我尝试按照下面的方式在Springboot中添加Prometheus标签(修改SpringBoot中的application.yml
),但没有成功。
management:
metrics:
tags:
application: ${spring.application.name}
threadLimitInPrometheus: 40 # This tag didn't work
您能告诉我在 SpringBoot 中添加 Prometheus 标签的方法吗?
我知道有一种方法可以通过如下方式修改 prometheus.yml
向目标添加新标签
- target_label: "foo"
replacement: "bar"
但是,我想在 SpringBoot 中找到方法,而不是 Prometheus。
谢谢。
最佳答案
定义标签的方法有多种。
常见标签可以在 application.yml
中定义
management:
metrics:
tags:
key2: value2
如果它不起作用,请确保应用配置。您可以在本地运行您的服务并使用 http://localhost:<port>/actuator/prometheus
检查公开的指标。
作为替代方案,您可以
使用MeterRegistryCustomizer
@Configuration
public class MeterRegistryConfiguration {
@Bean
public MeterRegistryCustomizer<MeterRegistry> metricsCommonTags() {
return (registry) -> registry.config().commonTags("key2", "value2");
}
}
如果您需要仅为特定指标定义标签,请使用 MeterFilter
@Bean
public MeterFilter customMeterFilter() {
return new MeterFilter() {
@Override
public Meter.Id map(Meter.Id id) {
if (id.getName().contains("name")) {
return id.withTag(Tag.of("key3", "value3"));
}
return id;
}
};
}
关于spring - 如何使用Spring boot向Prometheus中的目标添加标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74525500/