简体   繁体   English

为 Prometheus 公开指标端点的替代方法是什么?

[英]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 包,只有JettyPrometheus 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.

相关问题 如何向 Prometheus 公开 Dropwizard 指标 - How to expose Dropwizard Metrics to Prometheus 无法将 Flink 指标公开给 Prometheus - Can't expose Flink metrics to Prometheus Prometheus端点中不提供使用Micrometer的自定义指标 - Custom metrics using Micrometer is not available in Prometheus endpoint 普罗米修斯端点中缺少 Spring Boot Webclient 指标 - Spring Boot Webclient metrics missing in prometheus endpoint 如何在默认端点/指标下覆盖普罗米修斯 - How to override prometheus by default endpoint /metrics Prometheus 不在“/metrics”端点上显示来自本地主机服务的自定义指标 - Prometheus not displaying custom metrics from localhost service on `/metrics` endpoint 向 prometheus 公开 spring 集成 amqp/jms 通道消息指标 - Expose spring integration amqp/jms channel messages metrics to prometheus 使用 Jax-Rs 将 Prometheus Metrics Endpoint 添加到 Java 应用程序 - Add Prometheus Metrics Endpoint to Java App Using Jax-Rs prometheus端点调用时如何触发Metrics计算 - How to trigger Metrics calculation at the time of prometheus endpoint call 将应用程序迁移到Spring Boot 2之后,无法将hystrix指标公开给/ actuator / prometheus - Can't expose hystrix metrics to /actuator/prometheus after migrating application to Spring boot 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM