java - 实现普罗米修斯时的竞争条件

标签 java prometheus

我正在尝试在我的应用程序中实现自定义 Promethues 指标,我怀疑会存在竞争条件。如何避免竞争条件。

@Component
public class CustomeCounter {

Counter mycounter;

public CustomCounter(CollectorRegistry registry) {
 mycounter = Counter.build().name("test").help("test").register(registry);
}

public void incrementCounter() {
 mycounter.inc();
}
}


@Component
public class Test{

@Resource
private CustomCounter customCounter;

public void testInc() {
customCounter.incrementCounter();
}
}

对我来说,这看起来像是一个潜在的竞争条件。我怎样才能避免它。

最佳答案

一般来说,Prometheus 客户端库是线程安全的。我在java风格的文档中没有看到这样的保证,但有提示in the README :

  • Gauge(我希望计数器)方法是线程安全的

    Keep in mind that the default inc(), dec() and set() methods on Gauge take care of thread safety

  • 指标的创建(至少使用构建器)是线程安全的

    The labels() method looks up or creates the corresponding labelled timeseries. You might also consider storing the labelled timeseries as an instance variable if it is appropriate. It is thread safe and can be used multiple times, which can help performance.

根据您的代码,我预计不会出现其他并发问题。

关于java - 实现普罗米修斯时的竞争条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58796459/

相关文章:

prometheus - 不理解rate()函数的不同解析

java - 带有组合框和文本字段的简单 Jframe 以及标签中的结果

java - 对 LinkedList 感到困惑

java - JSF:如果转换失败则调用方法

java - java中的自增和自减运算符

go - OpenTelemetry 收集器有哪些用例?

azure - 当 Pod 重新创建时,Prometheus 发生崩溃循环

kubernetes - EKS、GKE 上的 kubelet_* 指标

prometheus - 在 Alertmanager webhook 中使用标签

java - Android 错误 : java.net.SocketException:套接字已关闭