I wrote a Spring Boot app and I'd like to expose custom metrics to Prometheus with Micrometer. Here's a code snippet where I'm incrementing a counter (which works) and attempts to set a couple of gauges (which doesn't work):
@Scheduled(cron = "*/2 * * * * *") // run every 2 seconds
private void logTemperature() throws UnknownHostException {
// get measurement
SensorReader sensorReader = new SensorReader();
SensorReading sensorReading = sensorReader.getSensorReading();
Metrics.counter("measurements").increment();
Metrics.gauge("fahrenheit", sensorReading.getFahrenheit());
Metrics.gauge("humidity", sensorReading.getHumidity());
// do stuff with measurement
// ...
}
The complete source code is here: https://github.com/alexwoolford/htu21d_logger/tree/master/htu21d-logger-spring
The fahrenheit
field in the sensorReading
object is a float
. The gauges exist in the output, however the value is NaN
(i.e. not a number):
# HELP measurements_total
# TYPE measurements_total counter
measurements_total 82.0
...
# HELP humidity
# TYPE humidity gauge
humidity NaN
...
# HELP fahrenheit
# TYPE fahrenheit gauge
fahrenheit NaN
...
I've seen similar posts, e.g. Micrometer/Prometheus How do I keep a gauge value from becoming NaN? and Micrometer - Prometheus Gauge displays NaN, but I can't figure out what, specifically, I need to do from those answers.
I also tried creating a variable and updating that with a lambda, e.g.
Metrics.gauge("humidity", humidity, humidity -> sensorReading.getHumidity());
... and the guage disappeared altogether (not even an NaN
).
This question is slightly different from the others since I'm not using a cache and I'm attempting to use the global registry which, presumably, is the simplest option.