java – 千分尺/普罗米修斯如何保持仪表值不变为NaN?
作者:互联网
我正在尝试监控登录用户,我通过调用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的一个缺点,我想最终解决.
您需要在此期间将值保留在映射中,以避免垃圾回收.注意我们如何将测量仪指向地图,并使用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()));
});
}
}
我还建议为各种渠道使用标签:
Metrics.gauge("loggedInUsers.inChannel", Tag.of("channel",channel.getchannelName()), gaugeCache, g -> g.get(channel.getchannelName()));
标签:prometheus,java,spring-boot,micrometer 来源: https://codeday.me/bug/20190910/1800412.html