我将Spring Boot应用程序从Spring Boot 1(使用Prometheus Simpleclient)转换为Spring Boot 2(使用Micrometer)。
我很难将Spring Boot 1和Prometheus的标签转换为Micrometer中的概念。例如(使用普罗米修斯):
private static Counter requestCounter =
Counter.build()
.name("sent_requests_total")
.labelNames("method", "path")
.help("Total number of rest requests sent")
.register();
...
requestCounter.labels(request.getMethod().name(), path).inc();
Micrometer的标签似乎与Prometheus的标签不同:所有值必须预先声明,而不仅仅是< EM>键
可以使用普罗米修斯&#39;带有弹簧(引导)和千分尺的标签?
答案 0 :(得分:7)
进一步挖掘表明,只有微米标签的键必须预先声明 - 但构造函数确实需要成对的键/值;价值无关紧要。并且在使用度量标准时必须指定密钥。
这有效:
private static final String COUNTER_BATCHMANAGER_SENT_REQUESTS = "batchmanager.sent.requests";
private static final String METHOD_TAG = "method";
private static final String PATH_TAG = "path";
private final Counter requestCounter;
...
requestCounter = Counter.builder(COUNTER_BATCHMANAGER_SENT_REQUESTS)
.description("Total number of rest requests sent")
.tags(METHOD_TAG, "", PATH_TAG, "")
.register(meterRegistry);
...
Metrics.counter(COUNTER_BATCHMANAGER_SENT_REQUESTS, METHOD_TAG, methodName, PATH_TAG, path)
.increment();
答案 1 :(得分:0)
我知道这个话题有些陈旧,但仍然如此。 我想通过使用PrometheusMeterRegistry来获取CollectorRegistry
来使用Spring Boot Acuator来解决该问题。@Service
public class SomethingService {
private final PrometheusMeterRegistry prometheusMeterRegistry;
private final Counter counter;
public SomethingService(PrometheusMeterRegistry prometheusMeterRegistry) {
this.prometheusMeterRegistry = prometheusMeterRegistry;
counter = Counter.build()
.name("counter_name")
.help("counter help")
.labelNames("your_labels")
.register(prometheusMeterRegistry.getPrometheusRegistry());
}
@PostConstruct
private void init() {
inc("your_value");
}
private void inc(String value) {
counter.labels(String.valueOf(value)).inc();
}
}