千分尺 - Prometheus Gauge 显示 NaN

Cem*_*nal 10 java spring-boot spring-boot-actuator prometheus micrometer

我正在尝试使用 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
    }
}
Run Code Online (Sandbox Code Playgroud)

有没有人可以帮助解决这个问题?任何帮助,将不胜感激。

mwe*_*uch 7

在所有情况下,您都必须持有对观察到的实例的强引用。当您的createGauge()方法退出时,所有函数堆栈分配的引用都有资格进行垃圾回收。

对于#1,通过你的atomicInteger领域是这样的:registry.gauge("my_ai", atomicInteger);。然后根据需要增加/减少。每当千分尺需要查询时,只要找到引用就可以了。

对于#2,传递您的productService字段和 lambda。基本上,无论何时查询仪表,它都会使用提供的对象调用该 lambda:registry.gauge("product_gauge", productService, productService -> productService.getProducts().size());

(不保证语法错误。)


cri*_*oms 6

我无法使用 @panser 解决方案,因为我使用带有标签的仪表。我的解决方案涉及使用 的键和值作为映射键创建com.google.common.util.concurrent.AtomicDouble缓存io.micrometer.core.instrument.Tag,如下所示:

    private static final Map<String, AtomicDouble> GAUGE_CACHE = new HashMap<>();

    public void handleGauge(String name, List<Tag> tags, double value) {
        String gaugeKey = this.gaugeKey(name, tags);
        if (!GAUGE_CACHE.containsKey(gaugeKey)) {
            GAUGE_CACHE.put(gaugeKey, new AtomicDouble());
        }
        Objects.requireNonNull(this.registry.gauge(name, tags, GAUGE_CACHE.get(gaugeKey))).set(value);
    }

    private String gaugeKey(String name, List<Tag> tags) {
        return name + ":" + tags.stream().map(tag -> tag.getKey() + tag.getValue()).collect(Collectors.joining(""));
    }
Run Code Online (Sandbox Code Playgroud)

这非常适合我的需求,希望对其他人有帮助。