0

我有以下 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()会调用并正确显示一个值。对于所有对 servletGameMetrics.GameFinished()的后续调用,不再调用并返回 NaN。

我无法弄清楚我做错了什么。

此致

4

1 回答 1

0

尝试保留对您在 Gauges 中用作状态对象的实例的引用(例如:私有 final 字段)(即:)GameMetrics

Gauges 使用 aWeakReference来引用它们的状态对象,这不会阻止它们被 GC 收集。因此,我认为在您的情况下,GC 会收集GameMetrics实例,因此它将为空,请参阅文档:https ://micrometer.io/docs/concepts#_why_is_my_gauge_reporting_nan_or_disappearing

于 2021-06-23T18:55:14.953 回答