How to trigger Metrics calculation at the time of prometheus endpoint call

Viewed 24

I am trying to configure my Micronaut Application with Micrometer and Prometheus. I want to do some calculation at the time of prometheus endpoint call. Can someone guide me here?

I tried to define a bean in Micronaut as follows, but it didn't get triggered and no metric with name SampleMetric was there:

@Prototype
public class MyBean {

  @Inject
  MeterRegistry registry;

  public MyBean() {
    Gauge.builder("SampleMetric", 12).register(registry);
    // I also tried the following but that also didn't work
    //registry.gauge("SampleMetric", 12);
  }
}

Thanks in advance

1 Answers

Gauges are queried every time the Prometheus registry is scraped (prometheus endpoint call). Also Gauge supports providing your own state object and own method that fetches the current value, please see the docs: https://micrometer.io/docs/concepts#_gauge_fluent_builder

If you follow the example in the docs:

Gauge.builder("gauge", myObj, myObj::gaugeValue).register(registry);

myObj.gaugeValue() is called every time the prometheus endpoint is called.

Related