【问题标题】:Display only a count of failures for Java success/fail metrics仅显示 Java 成功/失败指标的失败计数
【发布时间】:2021-09-15 15:36:41
【问题描述】:

关于如何在 Grafana 中的 Java 计数器的特定条件下构建视觉效果的快速问题。

目前,我有一小段java代码,直截了当。

  private String question(MeterRegistry meterRegistry) {
        if (someCondition()) {
            Counter.builder("theCounter").tags("GOOD", "GOOD").register(meterRegistry).increment();
            return "good";
        } else {
            LOGGER.warn("it is failing, we should increment failure");
            Counter.builder("theCounter").tags("FAIL", "FAIL").register(meterRegistry).increment();
            return "fail";
        }
    }

如您所见,很简单,只是一个“如果满足条件,则递增 GOOD 计数器,如果不满足,则递增 FAIL 计数器”

我有兴趣仅为失败构建仪表板。

当我查询我的/prometheus 端点时,我成功看到:

myCounter_total{FAIL="FAIL",} 7.0
myCounter_total{GOOD="GOOD",} 3.0

因此,我开始使用此查询。

myCounter_total{_ws_="workspace",_ns_="namespace",_source_="source}

不幸的是,这个查询让我看到了一切,好的和失败的。在我的示例中,我看到了所有 10 个计数器,而我只想看到 7 个失败。

我试过了

myCounter_total{FAIL="FAIL",_ws_="workspace",_ns_="namespace",_source_="source}

{{FAIL}}

但没有运气。 请问我错过了什么?

【问题讨论】:

    标签: java prometheus grafana spring-micrometer prometheus-java


    【解决方案1】:

    只为这种情况创建一个计数器,并给它一个名为status的标签,例如。现在,根据是出现好还是失败条件,使用字符串 "GOOD""FAIL" 作为状态标签的值来增加计数器,例如在这个伪代码中:

    Counter myCounter =
        Counter.build().name("myCounter").help("This is my counter").labelNames("status").register();
    
    
    if (someCondition()) {
        myCounter.labels("GOOD").increment();
    } else {
        myCounter.labels("FAIL").increment();
    }
    

    现在您应该可以看到如下查询输出:

    myCounter_total{status="FAIL"} 7.0
    myCounter_total{status="GOOD"} 3.0
    

    出于可视化目的,如果您想查看两张图表,一张表示好的情况,一张表示坏的情况,您可以使用如下所示的内容。查询myCounter_total 的这一查询会显示其数据,而图例负责分离具有不同状态标签值的值/图。

    查询:myCounter_total

    图例:{{status}}

    【讨论】:

    • 感谢您的回答!我目前正在使用来自io.micrometer.core.instrument.CounterCounter 来自micrometer-core-1.7.1.jar。它与您的样本略有不同。没有使用.labelNames("status").labels("GOOD")
    猜你喜欢
    • 1970-01-01
    • 2017-05-16
    • 2017-09-17
    • 2017-08-16
    • 1970-01-01
    • 2021-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多