How to set prometheus endpoint in non spring boot app

Viewed 2377

I want to add a path "localhost:8080/metrics" to my app to see Counter on my variables by using Prometheus. I read that for a spring boot application I need the only annotation over the main class.

package hello;

import io.prometheus.client.spring.boot.EnablePrometheusEndpoint;
import io.prometheus.client.spring.boot.EnableSpringBootMetricsCollector;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnablePrometheusEndpoint
@EnableSpringBootMetricsCollector
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

How can I obtain the same result in a non-Spring Boot application where I don't have a @SpringBootApplication.

Can it be achieved by registering multiple servlets?

1 Answers

You might want to add the Prometheus servlet to your application.

I will give an example of a Jetty server cited in the documentation:

Server server = new Server(1234);
ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
server.setHandler(context);

context.addServlet(new ServletHolder(new MetricsServlet()), "/metrics");

The dependency io.prometheus.simpleclient_spring_boot is a Spring Boot integration. Instead, you should look at the core library io.prometheus.simpleclient.

Related