我正在尝试使用 Micrometer.io 和 Spring Boot 2.0.0.RELEASE 来生成 Prometheus 指标。
当我尝试将 List 的大小公开为 Gauge 时,它一直显示 NaN。在文档中它说;
您有责任持有对您使用 Gauge 测量的状态对象的强引用。
我尝试了一些不同的方法,但我无法解决问题。这是我的一些试验代码。
import io.micrometer.core.instrument.*;
import io.swagger.backend.model.Product;
import io.swagger.backend.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
@RestController
@RequestMapping("metrics")
public class ExampleController {
private AtomicInteger atomicInteger = new AtomicInteger();
private ProductService productService;
private final Gauge productGauge;
@Autowired
public HelloController(ProductService productService,
MeterRegistry registry) {
this.productService = productService;
createGauge("product_gauge", productService.getProducts(), registry);
}
private void createGauge(String metricName, List<Product> products,
MeterRegistry registry) {
List<Product> products = productService.getProducts();
// …Run Code Online (Sandbox Code Playgroud) 我正在尝试监视已登录的用户,我通过调用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());
});
}
}
Run Code Online (Sandbox Code Playgroud)
我在Prometheus中看到了值,问题出在几秒钟后,该值变成NaN,我已经读到千分尺用WeakReference(因此收集了垃圾)包装了它们的obj输入,我不知道如何解决它。如果有人知道如何解决这个问题,那就太好了。