micrometer - 更新千分尺会阻塞调用线程吗?
问题描述
更新千分尺是否会阻塞调用线程(例如执行 I/O)?
我相信答案是“不,I/O 发生在一个单独的指标收集线程中”但想知道这个例外情况,边缘情况,..
谢谢塔里克
解决方案
这取决于调用线程是什么意思。如果您的意思是注册仪表的用户线程,答案是否定的,此时您提供给仪表的方法甚至不会被调用。
如果您的意思是“运送”指标的线程,那将被阻止。这通常是一个单独的线程(因为大多数注册表都是基于推送的),但在 Prometheus(基于拉取)的情况下,仪表将阻塞 Prometheus 端点和为其提供服务的线程。
出于这个原因,在千分尺中,您可以拥有一个中间“状态”对象,而不是向仪表注册阻塞方法,您可以从单独的线程定期更新(阻塞)并从仪表中读取它(非阻塞),例如:
AtomicInteger currentValue = registry.gauge("test.gauge", new AtomicInteger(0));
您可以currentValue
从另一个线程修改,请参阅文档
您可以对任意对象执行此操作,例如:
State state = registry.gauge("test.gauge", Collections.emptyList(), new State(), State::getValue);
WheregetValue
不阻塞只是给你最新的值,而在另一个线程上你可以更新封装在State
对象中的值。
这里有几行表明您注册到仪表中的方法是阻塞的:
public class GaugeSample {
public static void main(String[] args) throws InterruptedException {
PrometheusMeterRegistry registry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
System.out.println("registering the gauge...");
Gauge.builder("test.gauge", GaugeSample::getValue)
.register(registry);
System.out.println("scraping...");
System.out.println(registry.scrape());
}
private static double getValue() {
try {
Thread.sleep(5_000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
return 42;
}
}
推荐阅读
- excel - If语句改变连续数据值
- javascript - 如何在 Node.js 应用程序中检索数据后运行客户端函数
- python - 以下是函数 hangman 的最终代码,我收到语法错误
- class - 类图的实体属性查询
- rust - 是否可以实现一个拥有锁的迭代器以及引用该锁的内部迭代器?
- mysql - 关闭 Ubuntu phpmyadmin 区分大小写
- firebase - 如何将列表存储到 Firestore 并在 Flutter 中获取
- azure - 未能在 Azure 中触发自定义 HeartBeat 指标的警报
- java - 如何知道一个字符串是否属于另一个字符串,即使跳过字母也可以计算
- python - 在两个 Flask 应用程序之间共享 Flask-SQLAlchemy