Reputation: 77
Say I have an application where a REST API updates the price of a product.
I want to use a Micrometer Gauge to expose the new price as a metric. I'm having trouble understanding from Micrometer documentation how this should be accomplished.
The only toDoubleFunction()
that worked for me was to create a new method in my ProductService
which returns its price. This seems like an overhead for every piece of data I want to expose as a metric.
What am I missing here? Why product.getPrice()
isn't enough to update the Gauge?
Upvotes: 4
Views: 5628
Reputation: 1533
Micrometer's Gauge
would hold a reference to whatever it has to pull the value from. And that reference is a WeakReference
by default.
For example:
This means that should your provided value get garbage collected, micrometer would have nothing to poll the value from.
I assume that when you call product.getPrice();
you never hold on to that value just passing it to something like meterRegistry.gauge("product.price",tags,value);
Since after the completion of this block of code nothing holds a strong reference to that specific value it gets Garbage Collected (GC-ed).
You have couple solutions here: either build a Gauge
using a builder and specifying the strongReference(true)
or (better) make sure you hold your references and manage their values yourself.
Both are rather weird as you'll end up holding a lot of "Gauge sources" in memory.
Upvotes: 3