简体   繁体   English

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

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

I have a web application (war) with Jersey REST endpoints.我有一个带有 Jersey REST 端点的 web 应用程序(战争)。 I am integrating with prometheus / micrometer for generating metrics.我正在与 prometheus / micrometer 集成以生成指标。 I have exposed "/metrics" endpoint as in here我在这里公开了“/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();
    }
}

I am stuck on how to generate http request metrics.我被困在如何生成 http 请求指标上。 I could not find any code that would relevant to get these metrics.我找不到任何与获取这些指标相关的代码。 Can someone help me on this?有人可以帮我吗?

Alternatively to what checketts proposed, you could make use of the Jersey server instrumentation of Micrometer which is present even prior to the 1.0.0 release in the form of micrometer-jersey2 library.作为checketts建议的替代方案,您可以使用 Micrometer 的 Jersey 服务器工具,该工具甚至在1.0.0版本之前以micrometer-jersey2库的形式存在。 You can find the source here .你可以在这里找到源代码。

Your entrypoint to this is the MetricsApplicationEventListener which can be registered with Jerseys ResourceConfig .您的入口点是MetricsApplicationEventListener ,可以使用 Jerseys ResourceConfig注册。 For an example, you can have a look at the test class on how this could be done.例如,您可以查看测试 class以了解如何做到这一点。

You can also have a look at how this is integrated/autoconfigured in Spring Boot here .您还可以在此处查看如何在 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 metrics will nicely match without further ado. 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指标将很好地匹配,无需多言。

You'll need to include a Filter to record each request as it comes through.您需要包含一个Filter来记录每个请求。 See 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 for how Spring does it.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 是如何做到的。

I would recommend against using a static registry if possible and using dependency injection instead.如果可能,我建议不要使用static注册表,而是使用依赖注入。

Here is a tiny example of what you might do within a filter's doFilter method这是您可以在过滤器的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