I have the following servlet:
@Servlet(\path : String -> path.matches("/metrics"))
class MetricsServlet_De extends HttpServlet {
private var prometheusMeterRegistry : PrometheusMeterRegistry
override function init() {
configureMetrics()
}
protected override function doGet(request : HttpServletRequest, response : HttpServletResponse) {
response.ContentType = TextFormat.CONTENT_TYPE_004
response.setStatus(HttpServletResponse.SC_OK)
prometheusMeterRegistry.scrape(response.Writer)
}
private function configureMetrics() : PrometheusMeterRegistry {
prometheusMeterRegistry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
new ClassLoaderMetrics().bindTo(prometheusMeterRegistry)
new JvmMemoryMetrics().bindTo(prometheusMeterRegistry)
new JvmGcMetrics().bindTo(prometheusMeterRegistry)
new ProcessorMetrics().bindTo(prometheusMeterRegistry)
new JvmThreadMetrics().bindTo(prometheusMeterRegistry)
var toDoubleFunction : ToDoubleFunction<GameMetrics> = \status -> status.GameFinished
Gauge.builder("game_status", new GameMetrics(), toDoubleFunction)
.description("Return Game status")
.register(prometheusMeterRegistry)
return prometheusMeterRegistry
}
}
Upon the first call to the Servlet, GameMetrics.GameFinished()
is called and a value is properly displayed.
For alls subsequent calls to the servlet GameMetrics.GameFinished()
is no longer called and NaN is returned.
I can't figure out what I am doing wrong.
Best regards
Try to keep a reference (e.g.: a private final field) to the instances you use as state objects in Gauges (i.e.: GameMetrics
).
Gauges are using a WeakReference
to refer to their state object which does not prevent them to be collected by the GC. So I think in your case what happens is that the GC collects the GameMetrics
instance so it will be null, see the docs: https://micrometer.io/docs/concepts#_why_is_my_gauge_reporting_nan_or_disappearing