【问题标题】:How to set prometheus endpoint in non spring boot app如何在非 Spring Boot 应用程序中设置 Prometheus 端点
【发布时间】:2018-09-10 10:54:10
【问题描述】:

我想在我的应用程序中添加一个路径“localhost:8080/metrics”,以便使用 Prometheus 在我的变量上查看 Counter。我读到,对于 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);
    }
}

如何在没有@SpringBootApplication 的非 Spring Boot 应用程序中获得相同的结果。

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

【问题讨论】:

    标签: java spring spring-boot prometheus


    【解决方案1】:

    您可能希望将 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 是一个 Spring Boot 集成。相反,您应该查看核心库 io.prometheus.simpleclient

    【讨论】:

    • "io.prometheus.simpleclient" 感谢您的帮助。现在一切正常。
    猜你喜欢
    • 2019-10-13
    • 2018-09-20
    • 2020-05-16
    • 1970-01-01
    • 2017-08-03
    • 1970-01-01
    • 1970-01-01
    • 2020-05-21
    • 1970-01-01
    相关资源
    最近更新 更多