java - 如何为 Prometheus 检测 Java 应用程序代码指标

标签 java prometheus

我正在尝试将 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");
     }
   }

但是当我运行该项目时,出现下一个错误: 线程“main”中的异常 java.lang.NoClassDefFoundError: io/prometheus/client/exporter/common/TextFormat 在 io.prometheus.client.exporter.PushGateway.doRequest(PushGateway.java:299) 在 io.prometheus.client.exporter.PushGateway.pushAdd(PushGateway.java:158) 在 nemshelloworld.NemsHelloWorld.executeBatchJob2(NemsHelloWorld.java:78) 在 nemshelloworld.NemsHelloWorld.main(NemsHelloWorld.java:33) 引起原因:java.lang.ClassNotFoundException:io.prometheus.client.exporter.common.TextFormat 在 java.net.URLClassLoader.findClass(URLClassLoader.java:381) 在 java.lang.ClassLoader.loadClass(ClassLoader.java:424) 在 sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) 在 java.lang.ClassLoader.loadClass(ClassLoader.java:357)

最佳答案

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

关于java - 如何为 Prometheus 检测 Java 应用程序代码指标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56594029/

相关文章:

go - 为 golang prometheus 收集器添加标签

kubernetes - 如何配置 kube-prometheus-stack helm 安装来抓取 Kubernetes 服务?

java - 具有对数概率 Java 实现的数值精度

java - 使用反射生成的 Java 源代码

elasticsearch - Grafana中如何用Prometheus数据源监控elasticsearch

node.js - NodeJS API 服务失败如何重启?

java - 下载 hibernate jar 文件

java - @JsonIgnore 有条件

java - JSF 组件渲染器

django - 如何在 django Rest Framework 和 docker 中设置 prometheus