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

如何在Grafana变量中从Prometheus目标端点提取无端口服务器名

解决方案

方法一:从Prometheus抓取配置层面修正(推荐)

直接在Prometheus的scrape_configs里添加relabel规则,抓取时就把instance标签处理成不带端口的主机名,一劳永逸:

scrape_configs:
  - job_name: 'node_exporter'
    static_configs:
      - targets: ['http://xyz.abc:9100', ...]
    relabel_configs:
      - source_labels: [__address__]
        regex: '([^:]+):.*'
        target_label: instance

这样抓取到的instance标签就直接是xyz.abc,后续Grafana里直接用label_values(instance)就能得到想要的结果。

方法二:在Grafana查询中动态提取主机名

如果不想修改Prometheus配置,直接在Grafana的变量查询里用PromQL的regex_extract函数提取主机名:

label_values(regex_extract(instance, "^.*://([^:]+):.*$", "$1"))

或者用label_replace先构造新标签再取值:

label_values(label_replace(node_exporter_build_info, "host", "$1", "instance", "^.*://([^:]+):.*$"), host)

注:这里用node_exporter_build_info是因为它是每个节点都会存在的指标,你也可以换成其他所有目标都有的通用指标(比如up)。

内容的提问来源于stack exchange,提问作者Spiderweb Computer Technologie

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 15:15:42