首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Java正确使用prometheus标签?

如何使用Java正确使用prometheus标签?
EN

Stack Overflow用户
提问于 2022-10-20 17:28:22
回答 1查看 45关注 0票数 0

如何使用Java正确使用prometheus标签?

我有Java应用程序,有Spring,但是没有SpringBoot。

我有一个度量,它只能递增:我需要计算每个用户登录到我的系统的时间。用户可以动态添加:例如,在乞讨时可以有一个用户,而在一天结束时+40个用户。据我所知,最好的办法是用标签做一个柜台。

我在rabbitmq指标中看到了类似的东西:

代码语言:javascript
复制
# 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

所以,在我的例子中应该是

代码语言:javascript
复制
# TYPE user_logins counter
user_logins_total{user="Alex"} 0
user_logins_total{user="Kate"} 1
user_logins_total{user="Ali"} 7

我试过这样做:

代码语言:javascript
复制
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客户端:

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-20 21:28:49

试着让两个文档示例在您的身边运行。如果它在普通java中工作,那么它也可以在spring、JEE或其他地方工作。

https://github.com/prometheus/client_java

Counter

  • Example 2:标签
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74144023

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档