【问题标题】:Grafana + Prometheus how to use regex in query metrics?Grafana + Prometheus 如何在查询指标中使用正则表达式?
【发布时间】:2025-11-27 08:05:01
【问题描述】:

我们正在动态添加新指标。是否可以在 Grafana 查询指标中使用正则表达式?

application_test_total{color="0"}
application_test_total{color="1"}
application_test_total{color="2"}

如何避免这种情况?

sum(application_test_total{color="0"})+sum(application_test_total{color="1"})

可以用这个代替吗?

application_test_total{color="[0-9]{2}"}

【问题讨论】:

    标签: prometheus grafana metrics


    【解决方案1】:

    你可以替换:

    sum(application_test_total{color="0"})+sum(application_test_total{color="1"})
    

    收件人:

    sum(application_test_total{color=~"[0-9]{1}"})
    

    【讨论】:

    • 这行不通。 Grafana 版本 v6.6.2 (3fa63cfc34)。
    • 缺少一个“~”。我确定了答案。