【问题标题】:How to instrument Java application code metrics to Prometheus如何将 Java 应用程序代码指标检测到 Prometheus
【发布时间】:2019-06-14 08:07:42
【问题描述】:

我正在尝试将我的 Java 应用程序的自定义值指标导出到 Prometheus。我已经读过它可以使用 Push Gateway 来完成,以下是我使用下一个方法的示例:

static void executeBatchJob() throws Exception {
     CollectorRegistry registry = new CollectorRegistry();
     Gauge duration = Gauge.build()
         .name("my_batch_job_duration_seconds").help("Duration of my batch job in seconds.").register(registry);
     Gauge.Timer durationTimer = duration.startTimer();
     try {
       // Your code here.
       myCode();
       // This is only added to the registry after success,
       // so that a previous success in the Pushgateway isn't overwritten on failure.
       Gauge lastSuccess = Gauge.build()
           .name("my_batch_job_last_success").help("Last time my batch job succeeded, in unixtime.").register(registry);
       lastSuccess.setToCurrentTime();
     } finally {
       durationTimer.setDuration();
       PushGateway pg = new PushGateway("172.16.124.40:9091");
       pg.pushAdd(registry, "my_batch_job");
     }
   }

但是当我运行项目时,我遇到了下一个错误: Exception in thread "main" java.lang.NoClassDefFoundError: io/prometheus/client/exporter/common/TextFormat at io.prometheus.client.exporter.PushGateway.doRequest(PushGateway.java:299) at io.prometheus.client.exporter.PushGateway.pushAdd(PushGateway.java:158) at nemshelloworld.NemsHelloWorld.executeBatchJob2(NemsHelloWorld.java:78) at nemshelloworld.NemsHelloWorld.main(NemsHelloWorld.java:33) Caused by: java.lang.ClassNotFoundException: io.prometheus.client.exporter.common.TextFormat at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

【问题讨论】:

  • 您应该使用导出器,而不是推送网关。您可能还需要考虑 micrometer.io 外观
  • @Marged 我使用导出器,但只导出 JVM 默认指标,我想导出 Java 代码中的自定义指标。千分尺不是用来做弹簧的吗?我可以在 Java 中使用 to 吗?

标签: java prometheus


【解决方案1】:

您缺少 simpleclient_common 模块,它是 simpleclient_pushgateway 的列出依赖项,因此听起来您的 pom.xml 或等效项不正确。

【讨论】:

  • 谢谢!我添加了 simpleclient_common,它可以完美运行库
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-14
  • 1970-01-01
  • 1970-01-01
  • 2020-11-09
  • 2020-10-31
  • 2018-01-07
  • 1970-01-01
相关资源
最近更新 更多