如何在非 spring 引导应用程序中设置普罗米修斯端点

How to set prometheus endpoint in non spring boot app

我想向我的应用程序添加路径 "localhost:8080/metrics",以便使用 Prometheus 查看我的变量 Counter。我读到,对于 spring 引导应用程序,我需要在主 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);
    }
}

如何在没有 @SpringBootApplication.

的非 Spring 引导应用程序中获得相同的结果

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

您可能希望将 Prometheus servlet 添加到您的应用程序。

我将举一个在the documentation中引用的Jetty服务器的例子:

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

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

依赖关系io.prometheus.simpleclient_spring_boot is a Spring Boot integration. Instead, you should look at the core library io.prometheus.simpleclient.