千分尺-普罗米修斯量规显示NaN [英] Micrometer - Prometheus Gauge displays NaN
问题描述
我正在尝试通过将Micrometer.io与Spring Boot 2.0.0.RELEASE结合使用来生成Prometheus指标.
I am trying to generate Prometheus metrics with using Micrometer.io with Spring Boot 2.0.0.RELEASE.
当我尝试将List的大小显示为Gauge时,它会一直显示NaN.在文档中说:
您有责任对要使用量规进行测量的状态对象保持严格的引用.
It is your responsibility to hold a strong reference to the state object that you are measuring with a Gauge.
我尝试了一些不同的方法,但无法解决问题.这是我的一些试用代码.
I have tried some different ways but I could not solve the problem. Here is my code with some trials.
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
}
}
有没有人可以帮助解决这个问题?任何帮助将不胜感激.
Is there anyone who can help with this issue? Any help would be appreciated.
推荐答案
在所有情况下,您都必须拥有对观察到的实例的强引用.退出createGauge()
方法时,所有为函数堆栈分配的引用都可以进行垃圾回收.
In all cases, you must hold a strong reference to the observed instance. When your createGauge()
method is exited, all function stack allocated references are eligible for garbage collection.
对于#1
,像这样传递您的atomicInteger
字段:registry.gauge("my_ai", atomicInteger);
.然后根据需要增加/减少.每当千分尺需要查询时,只要找到参考,就可以.
For #1
, pass your atomicInteger
field like this: registry.gauge("my_ai", atomicInteger);
. Then increment/decrement as you wish. Whenever micrometer needs to query it, it will as long as it finds the reference.
对于#2
,传递您的productService
字段和一个lambda.基本上,每当查询量规时,它将使用提供的对象调用该lambda:registry.gauge("product_gauge", productService, productService -> productService.getProducts().size());
For #2
, pass your productService
field and a lambda. Basically whenever the gauge is queried, it will call that lambda with the provided object: registry.gauge("product_gauge", productService, productService -> productService.getProducts().size());
(不保证语法错误.)
这篇关于千分尺-普罗米修斯量规显示NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!