gpt4 book ai didi

java - 如何在非 spring boot 应用程序中设置 prometheus 端点

转载 作者:行者123 更新时间:2023-11-30 07:44:50 25 4
gpt4 key购买 nike

我想在我的应用程序中添加一个路径“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来实现?

最佳答案

您可能希望将 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 .

关于java - 如何在非 spring boot 应用程序中设置 prometheus 端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52256259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com