千分尺相当于普罗米修斯的标签
Micrometer's equivalent of Prometheus' labels
我正在将 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 的 tags 似乎与 Prometheus 的 labels 有所不同:所有 values 必须预先声明,不仅是 keys.
可以将 Prometheus 的标签与 Spring(启动)和 Micrometer 一起使用吗?
进一步挖掘表明,只有 keys micrometer 标签必须预先声明 - 但构造函数确实需要成对的 key/values;价值观并不重要。使用指标时必须指定密钥。
这个有效:
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();
我知道这个话题有点过时了,但仍然如此。
我想像使用 Spring Boot Acuator 那样解决它,方法是使用 PrometheusMeterRegistry 获取 CollectorRegistry
@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();
}
}
我正在将 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 的 tags 似乎与 Prometheus 的 labels 有所不同:所有 values 必须预先声明,不仅是 keys.
可以将 Prometheus 的标签与 Spring(启动)和 Micrometer 一起使用吗?
进一步挖掘表明,只有 keys micrometer 标签必须预先声明 - 但构造函数确实需要成对的 key/values;价值观并不重要。使用指标时必须指定密钥。
这个有效:
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();
我知道这个话题有点过时了,但仍然如此。 我想像使用 Spring Boot Acuator 那样解决它,方法是使用 PrometheusMeterRegistry 获取 CollectorRegistry
@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();
}
}