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

如何将Prometheus配置中的instance标签设为Grafana仪表盘的下拉选项

解决Grafana Instance下拉框显示自定义标签而非IP:Port的问题

问题根源

Prometheus在抓取目标时,会自动生成一个instance标签,值为目标的host:port(比如你的172.17.0.1:9187)。这个自动生成的标签优先级高于你在static_configs里手动设置的instance标签,所以最终存储在Prometheus中的instance值是IP+端口,而非你自定义的quoter-dev等值,这就导致Grafana下拉框显示的是自动生成的内容。

解决方案:通过Relabel配置替换默认Instance标签

我们可以利用Prometheus的relabel_configs功能,将自定义的标签值替换掉自动生成的instance标签。以下提供两种常用实现方式:

方式1:通过自定义中间标签转义

修改你的Prometheus配置,先给每个目标添加一个自定义标签(比如app_instance),再通过重标签规则将其赋值给instance:

global:
  scrape_interval: 15s
scrape_configs:
- job_name: 'quoters'
  static_configs:
  - targets: ['172.17.0.1:9187']
    labels:
      app_instance: quoter-dev
  - targets: ['172.17.0.1:9188']
    labels:
      app_instance: quoter-staging
  - targets: ['172.17.0.1:9189']
    labels:
      app_instance: quoter-prod
  # 添加重标签规则,用自定义标签替换默认instance
  relabel_configs:
    - source_labels: [app_instance]
      target_label: instance
      replacement: '$1'  # 将source_labels的值赋值给target_label

方式2:直接根据目标地址匹配替换

如果你不想新增中间标签,可以直接根据目标的__address__(即IP:Port)匹配,替换成对应的自定义instance值:

global:
  scrape_interval: 15s
scrape_configs:
- job_name: 'quoters'
  static_configs:
  - targets: ['172.17.0.1:9187']
  - targets: ['172.17.0.1:9188']
  - targets: ['172.17.0.1:9189']
  relabel_configs:
    # 匹配第一个目标,替换instance为quoter-dev
    - source_labels: [__address__]
      regex: '172.17.0.1:9187'
      target_label: instance
      replacement: 'quoter-dev'
    # 匹配第二个目标,替换instance为quoter-staging
    - source_labels: [__address__]
      regex: '172.17.0.1:9188'
      target_label: instance
      replacement: 'quoter-staging'
    # 匹配第三个目标,替换instance为quoter-prod
    - source_labels: [__address__]
      regex: '172.17.0.1:9189'
      target_label: instance
      replacement: 'quoter-prod'

验证与生效

  1. 修改完Prometheus配置后,重启Prometheus服务使配置生效。
  2. 等待几分钟让Prometheus完成新的抓取周期,然后在Prometheus的表达式浏览器中查询{job="quoters"},确认instance标签的值已经变成你自定义的quoter-dev/quoter-staging/quoter-prod。
  3. 回到Grafana,刷新仪表盘,此时instance下拉框就会显示你配置的自定义标签值了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 16:02:38