gpt4 book ai didi

java - Micrometer/Prometheus 如何防止仪表值变为 NaN?

转载 作者:搜寻专家 更新时间:2023-11-01 02:19:40 30 4
gpt4 key购买 nike

我正在尝试监控登录用户,我通过调用 api 获取登录用户信息,这是我使用的代码,

public class MonitorService {
private InfoCollectionService infoService;
public MonitorService(InfoCollectionService infoService) {
this.infoService = infoService
}

@Scheduled(fixedDelay = 5000)
public void currentLoggedInUserMonitor() {
infoService.getLoggedInUser("channel").forEach(channel -> {
Metrics.gauge("LoggedInUsers.Inchannel_" + channel.getchannelName(), channel.getgetLoggedInUser());
});
}
}

我在 Prometheus 中看到了值,问题是几秒钟后,值变成了 NaN,我读到 Micrometer 仪表用 Wea​​kReference 包装它们的 obj 输入(因此垃圾收集)。我不知道如何修复它。如果有人知道如何解决这个问题,那就太好了。

最佳答案

这是 Micrometer 的一个缺点,我最终会修复它。

同时您需要将值保存在映射中,以避免垃圾回收。请注意我们如何将仪表指向 map 并使用 lambda 来提取值以避免垃圾收集。

public class MonitorService {
private Map<String, Integer> gaugeCache = new HashMap<>();
private InfoCollectionService infoService;
public MonitorService(InfoCollectionService infoService) {
this.infoService = infoService
}

@Scheduled(fixedDelay = 5000)
public void currentLoggedInUserMonitor() {
infoService.getLoggedInUser("channel").forEach(channel -> {
gaugeCache.put(channel.getchannelName(), channel.getgetLoggedInUser());
Metrics.gauge("LoggedInUsers.Inchannel_" + channel.getchannelName(), gaugeCache, g -> g.get(channel.getchannelName()));
});
}
}

我还建议为各种 channel 使用标签:

Metrics.gauge("loggedInUsers.inChannel", Tag.of("channel",channel.getchannelName()), gaugeCache, g -> g.get(channel.getchannelName()));

关于java - Micrometer/Prometheus 如何防止仪表值变为 NaN?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50879488/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com