Micrometer - Prometheus Gauge displays NaN

后端 未结 4 1165
一个人的身影
一个人的身影 2021-02-20 12:59

I am trying to generate Prometheus metrics with using Micrometer.io with Spring Boot 2.0.0.RELEASE.

When I am trying to expose the size of a List as Gauge, it ke

4条回答
  •  北恋
    北恋 (楼主)
    2021-02-20 13:59

    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.

    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.

    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());

    (No guarantee regarding syntax errors.)

提交回复
热议问题