[英]What would be the alternative to expose metrics endpoint for Prometheus?
I would like to expose Prometheus metrics to an endpoint.我想将 Prometheus 指标公开给端点。 I don't have spring-boot so I need to expose metrics on my own.我没有 spring-boot,所以我需要自己公开指标。
I took example code from:我从以下示例代码中获取:
https://micrometer.io/docs/registry/prometheus#_configuring https://micrometer.io/docs/registry/prometheus#_configuring
PrometheusMeterRegistry prometheusRegistry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
try {
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
server.createContext("/prometheus", httpExchange -> {
String response = prometheusRegistry.scrape(); (1)
httpExchange.sendResponseHeaders(200, response.getBytes().length);
try (OutputStream os = httpExchange.getResponseBody()) {
os.write(response.getBytes());
}
});
new Thread(server::start).start();
} catch (IOException e) {
throw new RuntimeException(e);
}
While it works, I would like to avoid using sun package.虽然它有效,但我想避免使用 sun 包。 Is there a way to do this as short and elegant with netty, okhttp or apache for example?例如,有没有办法用 netty、okhttp 或 apache 做到这一点既简短又优雅?
Thank you.谢谢你。
You may use this piece of code:你可以使用这段代码:
Server server = new Server(8080);
ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
server.setHandler(context);
context.addServlet(new ServletHolder(new MetricsServlet()), "/prometheus");
There are no sun packages in imports, only Jetty and Prometheus Java client :导入中没有 sun 包,只有Jetty和Prometheus Java 客户端:
import io.prometheus.client.exporter.MetricsServlet;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
I am facing an issue related to below code: 我面临与以下代码相关的问题:
PrometheusMeterRegistry prometheusRegistry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
try {
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
server.createContext("/prometheus", httpExchange -> {
String response = prometheusRegistry.scrape(); (1)
httpExchange.sendResponseHeaders(200, response.getBytes().length);
try (OutputStream os = httpExchange.getResponseBody()) {
os.write(response.getBytes());
}
});
new Thread(server::start).start();
} catch (IOException e) {
throw new RuntimeException(e);
}
after running code when i go to http://localhost:8080 it doesn't even connect 运行代码后,当我转到http:// localhost:8080时,它甚至无法连接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.