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

如何为Prometheus的node_exporter单个目标配置自定义metrics_path?

问题

我在多台服务器上部署了node_exporter,希望将它们统一添加到Grafana的主仪表板中,因此将所有目标归到同一个job_name下,以便在Grafana中进行筛选。

当前Prometheus的prometheus.yml配置如下,多个node_exporter目标共享默认的metrics_path和basic_auth,运行正常:

scrape_configs:
  - job_name: node_exporter
    static_configs:
      - targets: ["nodeexporter.app1.example.com"]
      - targets: ["nodeexporter.app2.example.com"]
      - targets: ["nodeexporter.app3.example.com"]
      - targets: ["nodeexporter.app4.example.com"]
    basic_auth:
      username: 'admin'
      password: 'my_password'

现在需要添加一个新目标nodeexporter.app5.example.com,它的metrics路径是/extra/metrics,尝试了两种配置都报YAML结构错误:

第一种错误配置:

static_configs:
  [... the other targets]
  - targets: ["nodeexporter.app5.example.com/extra/metrics"]

第二种错误配置:

static_configs:
  [... the other targets]
  - targets: ["nodeexporter.app5.example.com"]
    __metrics_path__: "/extra/metrics"

请问如何正确配置这个自定义metrics路径的目标?


正确配置方法

错误原因说明

  • 第一种配置错误:targets字段仅用于定义主机(含端口,默认9100),不能直接拼接metrics路径,Prometheus会把整个字符串当作主机名解析,导致无法正常抓取。
  • 第二种配置错误:__metrics_path__是抓取时的元标签,不能直接写在static_configs的条目下,static_configs只支持targets和labels字段,因此会触发YAML结构校验错误。

两种可行的配置方案

方案1:使用relabel_configs针对特定目标修改metrics路径

在原job配置中添加relabel_configs,匹配目标主机后动态设置__metrics_path__标签:

scrape_configs:
  - job_name: node_exporter
    static_configs:
      - targets: 
          - "nodeexporter.app1.example.com"
          - "nodeexporter.app2.example.com"
          - "nodeexporter.app3.example.com"
          - "nodeexporter.app4.example.com"
          - "nodeexporter.app5.example.com"
    basic_auth:
      username: 'admin'
      password: 'my_password'
    # 添加relabel规则,针对app5修改metrics路径
    relabel_configs:
      - source_labels: [__address__]
        regex: "nodeexporter\\.app5\\.example\\.com"
        target_label: __metrics_path__
        replacement: "/extra/metrics"

方案2:拆分static_configs并配合labels+relabel(更灵活)

如果后续有更多特殊路径的目标,可以给目标添加自定义标签,再通过relabel规则匹配标签修改路径:

scrape_configs:
  - job_name: node_exporter
    basic_auth:
      username: 'admin'
      password: 'my_password'
    static_configs:
      # 普通目标
      - targets: 
          - "nodeexporter.app1.example.com"
          - "nodeexporter.app2.example.com"
          - "nodeexporter.app3.example.com"
          - "nodeexporter.app4.example.com"
      # 特殊路径目标,添加自定义标签
      - targets: ["nodeexporter.app5.example.com"]
        labels:
          custom_metrics_path: "/extra/metrics"
    # 根据自定义标签修改metrics路径
    relabel_configs:
      - source_labels: [custom_metrics_path]
        regex: ".+"
        target_label: __metrics_path__

验证配置

修改完成后,重启Prometheus,通过http://<prometheus-ip>:9090/targets页面检查目标状态,确认nodeexporter.app5.example.com的Metrics Path显示为/extra/metrics且抓取状态正常。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 12:31:01