Spring boot prometheus micrometers - Gauge not updating
Asked Answered
T

3

6

I have a SpringBoot 2.2.4.RELEASE with a RestRepostory like

import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.PostConstruct;

@RestController
public class MyController {

    private MeterRegistry meterRegistry;

    public MyController(MeterRegistry meterRegistry) {
        this.meterRegistry = meterRegistry;
    }

    private Gauge myGauge;
    private Integer myInteger = 0;

    @PostConstruct
    private void init() {
        myGauge = Gauge.builder("my.gauge", myInteger, Integer::intValue)
                .register(meterRegistry);
    }

    @GetMapping("/count")
    public void count() {
        myInteger = 5;
    }

}

After the application is started, going to http://localhost:8082/actuator/prometheus I can see

# HELP my_gauge  
# TYPE my_gauge gauge
my_gauge 0.0

But after going to http://localhost:8082/count/, the value remains 0.0

What's the problem ? I also don't understand the 3rd parameter of the builder function. Is the the cause ?

I also tried with a Counter. And it's working fine when I increment it withing the count function.

Trumpetweed answered 13/5, 2020 at 9:39 Comment(0)
P
2

I found a way to workaround, seams that meterRegistry keeps the fist value you put, but you can remove the previous value and register one more time, I don't know if is the best way, but solves the problem to me

 public void percentage(String name, AtomicReference<Double> value) {
try {
  var m = meterRegistry.get(name);
  if (m != null) {
    var g = m.gauge();
    meterRegistry.remove(g); //this removes the old value
  }
} catch (Exception e){
  //doNothing
}
Gauge gauge = Gauge.builder(name, value, AtomicReference::get).register(meterRegistry);
  System.out.println(gauge.value()); //this shows a diferent value every time you call the percentage method
 }
Psychosurgery answered 10/7, 2022 at 19:55 Comment(1)
Thanks same problem ... solved with the removing of old value !!!Photosensitive
C
0

I know that this is an old question and probably you have already solved or tried another solution to your problem. I just faced the same problem and, in the end, it was a misunderstanding regarding how the gauge method works. The 3rd/4th, depending on the constructor, parameter of gauge method, receives a lazy function.

I asked a similar question on the Prometheus Google Group and found the solution in the meantime.

Here is the snippet with the solution that I implemented on my API:

private final Map<String, AtomicInteger> statusCodes = new HashMap<>();

.
.
.

private void createOrUpdateMetric(String healthType, Status status) {
    statusCodes.put(healthType, new AtomicInteger(healthToCode(status)));

    Gauge.builder(HEALTH, statusCodes, statusCodes -> statusCodes.get(healthType).get())
         .tags(Tags.of(Tag.of(HEALTH_TYPE, healthType)))
         .description(HEALTH_DESCRIPTION + healthType)
         .register(meterRegistry);
}

Here is the question I asked on Prometheus Groups.

Chalet answered 8/12, 2020 at 13:5 Comment(1)
When did you trigger the 'createOrUpdate' method? (as e.g. the configuration class for actuator health is only called initally...)Gowon
T
0

The solution is to use an AtomicInteger:

import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.atomic.AtomicInteger;

@RestController
public class MyController {

    private final AtomicInteger myInteger;

    public MyController(MeterRegistry meterRegistry) {
        this.myInteger = meterRegistry.gauge("my.gauge", new AtomicInteger(0));
    }

    @GetMapping("/count")
    public void count() {
        myInteger.set(5);
    }

}

Consult the docs for more information: Manually Incrementing or Decrementing a Gauge

Tremor answered 12/9, 2024 at 6:36 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.