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

如何将Prometheus文本文件静态指标作为Grafana告警的实例图例?

解决方案:用自定义ID替换Prometheus/Grafana中的IP实例标识

方法一:在Grafana查询中通过标签关联替换(无需修改Prometheus配置)

直接修改原内存使用率的PromQL,通过group_left将prod_server指标的id标签关联到查询结果中:

100 - (
  sum by (instance) (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes)
) * 100
* on(instance) group_left(id) prod_server

配置Grafana图例显示自定义ID

在Grafana面板编辑页的「图例」设置中,将图例格式改为{{id}},这样面板和告警通知就会显示自定义ID(如12345)而非IP地址。

原理:on(instance)指定两个指标通过instance标签(IP)关联,group_left(id)将prod_server中的id标签继承到内存使用率结果中,实现IP到自定义ID的映射。

方法二:在Prometheus采集阶段添加自定义ID标签(全局生效)

如果希望该实例的所有采集指标都带上自定义ID标签,可在Prometheus的scrape_configs中配置metric_relabel_configs,把prod_server的id标签复制到同一节点的所有指标上。

修改Prometheus配置文件

scrape_configs:
  - job_name: 'node_exporter'  # 对应你的node_exporter采集任务名
    static_configs:
      - targets: ['10.10.0.1:9100']  # 你的实例地址
    metric_relabel_configs:
      # 从prod_server指标提取id,存入临时标签__tmp_id
      - source_labels: [id]
        regex: '(.*)'
        target_label: __tmp_id
        action: replace
        condition: '{__name__="prod_server"}'
      # 将临时标签的ID复制到所有非prod_server指标的instance_id标签
      - source_labels: [__tmp_id]
        regex: '(.*)'
        target_label: instance_id
        action: replace
        condition: '{__name__!="prod_server"}'

更新内存使用率查询

修改查询语句,按新的instance_id标签分组:

100 - (sum by (instance_id) (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes)) * 100

之后在Grafana图例中设置为{{instance_id}}即可。

可选:直接替换instance标签

若想彻底把IP格式的instance标签替换为自定义ID,可调整metric_relabel_configs:

metric_relabel_configs:
  - source_labels: [id]
    regex: '(.*)'
    target_label: instance
    action: replace
    condition: '{__name__="prod_server"}'
  - source_labels: [instance]
    regex: '(.*)'
    target_label: instance
    action: replace
    condition: '{__name__!="prod_server"}'

注意:此方法会修改所有指标的instance标签值,建议先在测试环境验证。

关键注意事项

  • 确保prod_server指标与node_exporter指标来自同一采集目标(即拥有相同的instance标签值),否则关联或标签复制会失效。
  • 多实例场景下,每个实例的prod_server指标需对应唯一id,避免标识混淆。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 14:35:17