问题描述
我有以下 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 中用作状态对象的实例的引用(例如:私有 final 字段)(即:GameMetrics
)。
Gauge 使用 WeakReference
来引用它们的状态对象,这不会阻止它们被 GC 收集。因此,我认为在您的情况下,GC 会收集 GameMetrics
实例,因此它将为空,请参阅文档:https://micrometer.io/docs/concepts#_why_is_my_gauge_reporting_nan_or_disappearing