如何使用Java正确使用prometheus标签?
我有Java应用程序,有Spring,但是没有SpringBoot。
我有一个度量,它只能递增:我需要计算每个用户登录到我的系统的时间。用户可以动态添加:例如,在乞讨时可以有一个用户,而在一天结束时+40个用户。据我所知,最好的办法是用标签做一个柜台。
我在rabbitmq指标中看到了类似的东西:
# TYPE rabbitmq_queue_message_bytes_persistent gauge
rabbitmq_queue_message_bytes_persistent{durable="true",policy="",queue="ERROR",vhost="/"} 0
rabbitmq_queue_message_bytes_persistent{durable="true",policy="",queue="OK",vhost="/"} 1
rabbitmq_queue_message_bytes_persistent{durable="true",policy="",queue="PROGRESS",vhost="/"} 7所以,在我的例子中应该是
# TYPE user_logins counter
user_logins_total{user="Alex"} 0
user_logins_total{user="Kate"} 1
user_logins_total{user="Ali"} 7我试过这样做:
private Counter getByLabel(String labelName) {
Counter counter = map.get(labelName);
if (counter == null) {
counter = Counter.build()
.namespace(NAMESPACE)
.labelNames(labelName)
.name(USER_LOGINS_NAME)
.help(USER_LOGINS_HELP)
.register();
map.put(labelName, counter);
}
return counter;
}
//With such increment() I receive NPE
public void increment(String labelName) {
this.getByLabel(labelName).inc();
}
//With such increment() I receive an error abt I have the counter with the same name each time i try to increment counter more then once.
public void increment(String labelName) {
this.getByLabel(labelName).labels(labelName).inc();
}我使用的Prometheus客户端:
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient</artifactId>
<version>0.16.0</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_httpserver</artifactId>
<version>0.16.0</version>
</dependency>发布于 2022-10-20 21:28:49
试着让两个文档示例在您的身边运行。如果它在普通java中工作,那么它也可以在spring、JEE或其他地方工作。
https://github.com/prometheus/client_java
Counter
https://stackoverflow.com/questions/74144023
复制相似问题