如何在非 Spring Boot 应用程序中设置 Prometheus 端点

Kar*_*ski 5 java spring spring-boot prometheus

我想向我的应用程序添加路径“localhost:8080/metrics”,以Counter使用 Prometheus 查看我的变量。我读到,对于 Spring Boot 应用程序,我需要主类上的唯一注释。

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

如何在没有@SpringBootApplication.

可以通过注册多个servlet来实现吗?

And*_*lko 5

您可能想要将 Prometheus servlet 添加到您的应用程序中。

我将给出文档中引用的 Jetty 服务器的示例:

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

context.addServlet(new ServletHolder(new MetricsServlet()), "/metrics");
Run Code Online (Sandbox Code Playgroud)

依赖项io.prometheus.simpleclient_spring_boot是 Spring Boot 集成。相反,您应该查看核心库io.prometheus.simpleclient