我有以下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
}
}
在第一次调用Servlet时,会调用GameMetrics.GameFinished()
并正确显示一个值。对于所有后续对servlet的调用,不再调用GameMetrics.GameFinished()
,并返回NaN。
我不明白我做错了什么。
向致以最良好的问候
尝试保留对在Gauges中用作状态对象的实例的引用(例如:专用最终字段((即:GameMetrics
(。
仪表使用WeakReference
引用其状态对象,这不会阻止GC收集仪表。因此,我认为在您的情况下,GC收集GameMetrics
实例,因此它将为null,请参阅文档:https://micrometer.io/docs/concepts#_why_is_my_gauge_reporting_nan_or_disappearing