You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Grafana中使用变量排除查询数据时的标签名列表?

解决方案:用Grafana多值变量实现动态排除多个端点

步骤1:创建支持多选的排除端点变量

在Grafana中创建一个Query类型的变量,配置如下:

  • 变量名:exclude_endpoints(可自定义)
  • 数据源:选择你的Prometheus数据源
  • 查询语句:label_values(http_request_duration_seconds_sum, exported_endpoint)(自动获取所有存在的exported_endpoint标签值)
  • 变量选项:
    • 勾选「Multi-value」(支持多选端点)
    • 勾选「Include All option」(添加“全选”选项,用于不排除任何端点的场景)
    • 「Custom all value」设置为空字符串(确保选择“全选”时变量值为空)

步骤2:修改PromQL查询实现动态排除

将原查询修改为以下形式,利用变量实现动态排除多个端点:

rate(http_request_duration_seconds_sum{
    container="$container", 
    namespace="$namespace",
    exported_endpoint!~"$exclude_endpoints|^$"
}[5m]) 
/ 
rate(http_request_duration_seconds_count{
    container="$container", 
    namespace="$namespace",
    exported_endpoint!~"$exclude_endpoints|^$"
}[5m])

逻辑说明

  • 当选中多个要排除的端点时,Grafana会自动将选中的值用|拼接成正则表达式(比如选中/management/health和/metrics,变量会变为/management/health|/metrics),!~操作符会排除所有匹配这些正则的端点。
  • 末尾的|^$是为了处理变量为空的场景(即选择“全选”或未选任何端点):此时!~"|^$"等价于不排除任何端点,查询会正常返回所有数据。

进阶优化(可选)

如果需要支持排除前缀匹配的端点(比如所有/management/开头的路径),可以在变量的查询语句中调整为:

label_values(http_request_duration_seconds_sum{exported_endpoint=~"/management/.*"}, exported_endpoint)

这样变量只会列出符合前缀的端点,方便批量选择排除。

内容的提问来源于stack exchange,提问作者Abolfazl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 05:03:36