我正在尝试监视已登录的用户,通过调用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,我已经读过千分尺用WeakReference(因此收集了垃圾)包装了它们的obj输入,但我不知道如何解决它。如果有人知道如何解决这个问题,那就太好了。
这是我最终要解决的千分尺缺陷。
同时,您需要将该值保存在映射中,以免垃圾回收。请注意,然后我们如何将量规指向地图,并通过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()));