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

如何为Grafana Loki多范围查询结果设置不同标签名称?

解决方法

要让不同指标的结果在表格里分开展示,核心是给每个查询的结果添加唯一标识标签,再通过Grafana的Transform功能拆分列,具体操作如下:

1. 修改LogQL查询,添加自定义标签

给四个查询分别添加用于区分指标类型的标签(比如metric),这样每个查询的结果都会带上专属标识:

  • 统计调用次数:
count by (httpRequest_requestUrl) (count_over_time({app="ingress-nginx"} | json [1h])) | label_format metric="request_count"
  • 平均延迟:
avg_over_time({app="ingress-nginx"} | json | label_format lat=`{{ TrimSuffix .httpRequest_latency " s" }}` | unwrap lat [1h]) by (httpRequest_requestUrl) | label_format metric="avg_latency"
  • 最小延迟:
min_over_time({app="ingress-nginx"} | json | label_format lat=`{{ TrimSuffix .httpRequest_latency " s" }}` | unwrap lat [1h]) by (httpRequest_requestUrl) | label_format metric="min_latency"
  • 最大延迟:
max_over_time({app="ingress-nginx"} | json | label_format lat=`{{ TrimSuffix .httpRequest_latency " s" }}` | unwrap lat [1h]) by (httpRequest_requestUrl) | label_format metric="max_latency"

2. 配置Grafana面板的Transform

在表格面板的「Transform」选项卡中,按顺序添加以下转换规则:

  • 第一步:合并查询结果
    添加「Merge」转换,把四个查询的结果合并为一个数据集。

  • 第二步:整理字段
    添加「Organize fields」转换,保留httpRequest_requestUrl、metric、Value、Time这几个字段,其余字段可以移除。

  • 第三步:将行转列为单独指标列
    添加「Convert rows to columns」转换:

    • 选择「Column name」为metric(用这个标签的值作为列名)
    • 选择「Value column」为Value(用这个字段的值填充对应列)
  • 第四步:优化列名(可选)
    再添加一次「Organize fields」转换,把自动生成的列名(比如request_count、avg_latency)改成更友好的名称,比如「调用次数」、「平均延迟」等。

完成后,表格就会按端点分组,每一行展示该端点每小时的调用次数、平均/最小/最大延迟,每个指标对应单独的列。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 01:12:23