Gauge Metrics not getting updated

642 views
Skip to first unread message

Victor Augusto Farias Dinis

unread,
Dec 4, 2020, 4:54:23 PM12/4/20
to Prometheus Users
Hi there!

I am trying to build a few set of metrics for the health information of my Spring Boot Application (it has the Actuator framework as the metrics aggregator).
At first, I have tried to implement a gauge metric, intercepting the result of all health checks that my app does. When I saw the result, the metrics appeared to be static, even though I was setting it to 1 and -1, depending on the UP and DOWN information.
Because of that, I tried to implement a summary metric, but when I tried to put those info into a Grafana Dashboard, it was getting really difficult to achieve what I was intending to.

That said, I have two questions:
1. Am I trying to do something wrong, considering a gauge metric like a "boolean" metric? Because is exactly that that I wish to achieve in the end.
2. What am I doing wrong, so that the metric kept its first value set?

Here is the snippet of the code:

meterRegistry.gauge("health_state", tagsHealth, healthResponse.getStatus().equals(Status.UP) ? 1 : -1)

I tried to use other constructors, but the result was the same. With the Gauge API, I was getting another error.

Has anyone received the same error or a similar one?

Important info:
Java Version: 12
Spring Boot Version: 2.3.4.RELEASE

Thanks in advance.

Stuart Clark

unread,
Dec 4, 2020, 5:02:50 PM12/4/20
to Victor Augusto Farias Dinis, Prometheus Users

Using a gauge to express a boolean or other fixed options is quite normal.

In fact Prometheus does it itself with the "up" metric. However it is normal to use 1 and 0 for the true/false values (which also makes some queries simpler).

Victor Augusto Farias Dinis

unread,
Dec 4, 2020, 6:06:50 PM12/4/20
to Prometheus Users
Hi Stuart!
Thanks for the quick response!
Then my idea was correct about how to achieve what I am trying to.
Now for the second question, do you know why my gauge metric are not getting updated?

I found a question on stack overflow referring to the same problem: https://stackoverflow.com/questions/61771197/spring-boot-prometheus-micrometers-gauge-not-updating

Thanks again!

Victor Augusto Farias Dinis

unread,
Dec 8, 2020, 7:54:48 AM12/8/20
to Prometheus Users
Hi everyone, I figured out what was going on. The method that create a gauge metric takes a numeric value as a function. I imagine that this function is somehow executed in a lazy way, and that was the problem. The "function" that I was passing was actually a simple number, and the value was never updated.
For anyone having this issue, here is a snippet from the code that I wrote:

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);
}
Reply all
Reply to author
Forward
0 new messages