4

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");
     }
   }

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)

2
  • You should use the exporter, not the push gateway. You also might want to consider micrometer.io facade Commented Jun 14, 2019 at 8:40
  • @Marged I use the exporter, but only exports JVM default metrics, I would like to export customized metrics inside the Java code. Micrometer is not used for spring? Can I use to in Java? Commented Jun 14, 2019 at 8:50

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I add the simpleclient_common and it works perfect library

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.