如何为 Prometheus 检测 Java 应用程序代码指标 [英] How to instrument Java application code metrics to Prometheus
问题描述
我正在尝试将我的 Java 应用程序的自定义值指标导出到 Prometheus.我已经读过它可以通过 Push Gateway 完成,下面是我使用下一个方法的示例:
I am trying to export customized values metrics of my Java application to Prometheus. I have read that it could be done with Push Gateway, following an example I use the next method:
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)
But when I run the project I am having the next error:
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)
推荐答案
您缺少 simpleclient_common
模块,它是 simpleclient_pushgateway
的列出依赖项,所以听起来像您的 pom.xml 或等效文件不正确.
You're missing the simpleclient_common
module, which is a listed dependency of simpleclient_pushgateway
so it sounds like your pom.xml or equivalent isn't right.
这篇关于如何为 Prometheus 检测 Java 应用程序代码指标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!