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
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.)