簡體   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