我正在尝试监控登录用户,我通过调用 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 仪表用 WeakReference 包装它们的 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/