繁体   English   中英

jersey 应用程序中的千分尺 Prometheus 指标(非弹簧)

[英]Micrometer Prometheus metrics in jersey application (Non spring)

我有一个带有 Jersey REST 端点的 web 应用程序(战争)。 我正在与 prometheus / micrometer 集成以生成指标。 我在这里公开了“/metrics”端点

@Path("/metrics")
public class Metrics {
    private static final PrometheusMeterRegistry prometheusRegistry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);

    static {
        new JvmGcMetrics().bindTo(prometheusRegistry);
        new JvmMemoryMetrics().bindTo(prometheusRegistry);
        new JvmCompilationMetrics().bindTo(prometheusRegistry);
        new JvmThreadMetrics().bindTo(prometheusRegistry);
    }
    @GET
    public String getMetrics() {
        return prometheusRegistry.scrape();
    }
}

我被困在如何生成 http 请求指标上。 我找不到任何与获取这些指标相关的代码。 有人可以帮我吗?

作为checketts建议的替代方案,您可以使用 Micrometer 的 Jersey 服务器工具,该工具甚至在1.0.0版本之前以micrometer-jersey2库的形式存在。 你可以在这里找到源代码。

您的入口点是MetricsApplicationEventListener ,可以使用 Jerseys ResourceConfig注册。 例如,您可以查看测试 class以了解如何做到这一点。

您还可以在此处查看如何在 Spring Boot 中集成/自动配置。

One last note: Spring Boots metric name is http.server.requests (to distinguish them from HTTP client request metrics) and if you one day will move to Spring Boot or your platform is already running Spring Boot applications, your non Spring Boot HTTP requests指标将很好地匹配,无需多言。

您需要包含一个Filter来记录每个请求。 https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web /servlet/WebMvcMetricsFilter.java Spring 是如何做到的。

如果可能,我建议不要使用static注册表,而是使用依赖注入。

这是您可以在过滤器的doFilter方法中执行的操作的一个小示例

        long start = System.nanoTime()
        try {
            filterChain.doFilter(request, response);
            long durationNanos = System.nanoTime() - start;
            prometheusRegistry.timer("http.server.requests", "status", response.getStatus().toString()).record(durationNanos, TimeUnit.NANOSECONDS)
        } catch (Exception ex) {
            //Equivalent exception timer recording
            throw ex;
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM