我正在尝试使用 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();
// #1
// this displays product_gauge as NaN
AtomicInteger n = registry.gauge("product_gauge", new AtomicInteger(0));
n.set(1);
n.set(2);
// #2
// this also displays product_gauge as NaN
Gauge
.builder("product_gauge", products, List::size)
.register(registry);
// #3
// this displays also NaN
testListReference = Arrays.asList(1, 2);
Gauge
.builder("random_gauge", testListReference, List::size)
.register(registry);
// #4
// this also displays NaN
AtomicInteger currentHttpRequests = registry.gauge("current.http.requests", new AtomicInteger(0));
}
@GetMapping(path = "/product/decrement")
public Counter decrementAndGetProductCounter() {
// decrement the gague by one
}
}
有没有人可以帮助解决这个问题?任何帮助,将不胜感激。