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

如何在Prometheus中为指定Target自定义覆盖$label.instance值?

解决Prometheus自定义Instance名称的问题

你的问题出在对Prometheus处理完整URL target的逻辑不了解:当你在static_configs里填写带路径的完整URL(比如http://localhost:1234/v1/status)时,Prometheus会自动拆分出三个内部标签:

  • __scheme__:协议部分(比如http)
  • __address__:仅包含主机和端口(比如localhost:1234)
  • __path__:路径部分(比如/v1/status)

你之前的relabel规则用__address__去匹配整个完整URL,自然匹配不到,所以规则不会生效。

下面给你两种可行的解决方案:

方案一:拆分Static Config直接指定Instance标签

这是最直观的方式,把需要自定义instance的目标单独拆成一个static_config条目,直接通过labels指定instance名称:

- job_name: my_job
  static_configs:
  - targets: ["https://example-1.com/api/v1/status"]
  - targets: ["https://example-2.com/api/v1/status"]
  - targets: ["http://localhost:1234/v1/status"]
    labels:
      instance: myserver

方案二:使用Relabel规则匹配完整URL组合标签

如果你不想拆分配置,可以通过组合__scheme__、__address__、__path__三个内部标签来匹配目标,再替换instance标签:

- job_name: my_job
  static_configs:
  - targets:
    - https://example-1.com/api/v1/status
    - https://example-2.com/api/v1/status
    - http://localhost:1234/v1/status
  relabel_configs:
  - source_labels: [__scheme__, __address__, __path__]
    separator: "://"
    regex: '(http)://(localhost:1234)(/v1/status)'
    target_label: instance
    replacement: "myserver"

或者也可以用分号作为分隔符拼接标签后匹配:

relabel_configs:
- source_labels: [__scheme__, __address__, __path__]
  separator: ";"
  regex: 'http;localhost:1234;/v1/status'
  target_label: instance
  replacement: "myserver"

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 14:17:23