如何在 Java 中向 Prometheus 摘要指标添加标签

How to add labels to Prometheus Summary metric in Java

计数器和仪表允许添加标签。当我尝试向摘要添加标签时,出现 "incorrect number of labels" 错误。

这就是我正在尝试的:

private static final Summary latencySummary = Summary.build()
            .name("all_latencies")
            .help("all latencies.")
            .register();

latencySummary.labels("xyz_api_latency").observe(timer.elapsedSeconds());

我查看了摘要 github 源代码,但找不到答案。如何将标签添加到摘要?

你需要provide the labelname in the metric:

private static final Summary latencySummary = Summary.build()
    .name("latency_seconds")
    .help("All latencies.")
    .labelNames("api")
    .register();

下面是另一个示例,我们可以在其中了解如何定义标签和设置实际值:

Gauge mem_usage = Gauge.build().name("mem_usage").help("Memory usage").labelNames("label1", "label2").register();
mem_usage.labels("label1_value1", "label2_value2").set(1000);