惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

有赞技术团队
有赞技术团队
G
Google Developers Blog
T
Tailwind CSS Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
J
Java Code Geeks
P
Proofpoint News Feed
V
Visual Studio Blog
爱范儿
爱范儿
The Cloudflare Blog
博客园 - 叶小钗
V
V2EX
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
M
MIT News - Artificial intelligence
Microsoft Security Blog
Microsoft Security Blog
博客园 - 聂微东
H
Help Net Security
B
Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 【当耐特】
量子位
宝玉的分享
宝玉的分享
WordPress大学
WordPress大学
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

mafeifan 的编程技术分享

mafengwo-mp3-downloader | mafeifan 的编程技术分享 示例页面 | mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 查看 default namespace 下的 default service account名称 mafeifan 的编程技术分享 检查日志 | mafeifan 的编程技术分享 bridge fdb show dev flannel.1 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享
mafeifan 的编程技术分享
2026-01-16 · via mafeifan 的编程技术分享

标签重写(Relabeling)是Prometheus一个非常有用的功能,它可以在任务拉取(scraping)阶段前,修改target和它的labels。

该功能在日常的监控中常常会使用到,值得我们好好了解。

一. 默认标签 ​

默认情况下,Prometheus加载targets后,都会包含一些默认的标签,其中以__作为前置的标签是在系统内部使用的,因此这些标签不会写入到样本数据中。

如:

  • address:当前Target实例的访问地址
  • scheme:采集目标服务访问地址的HTTP Scheme,HTTP或者HTTPS;
  • metrics_path:采集目标服务访问地址的访问路径;

上面这些标签将会告诉Prometheus如何从该目标实例中获取监控数据,而通过标签重写功能,我们可以对这些标签进行重写,从而实现对Target目标的控制。

二. relabel_config ​

标签重写的配置参数为relabel_config,其完整的配置格式如下 :

#源标签,需要在现有标签中已存在
[ source_labels: '[' <labelname> [, ...] ']' ]

# 多个源标签的分隔符;
[ separator: <string> | default = ; ]

# 要替换的目标标签;
[ target_label: <labelname> ]

# 正则表达式,用于匹配源标签的值
[ regex: <regex> | default = (.*) ]

# 源标签值取hash的模块;
[ modulus: <uint64> ]

# 当正则表达式匹配时,用于替换的值,$1代替正则匹配到的值;
[ replacement: <string> | default = $1 ]

# 基于正则匹配的动作
[ action: <relabel_action> | default = replace ]

其中,相关的action类型有如下几种:

  • replace:正则匹配源标签的值用来替换目标标签,如果有replacement,使用replacement替换目标标签;
  • keep: 如果正则没有匹配到源标签的值,删除该targets ,不进行采集;
  • drop: 与keep相反,正则匹配到源标签,删除该targets;
  • labelmap:正则匹配所有标签名,将匹配的标签值部分做为新标签名,原标签值 做为新标签的值;
  • labeldrop:正则匹配所有标签名,匹配则移除标签;
  • labelkeep:正则匹配所有标签名,不匹配的标签会被移除;

注意:重定义标签并应用后,__开头的标签会被删除; 要临时存储值用于下一阶段的处理,使用__tmp开头的标签名,这种标签不会被Prometheus使用;

三. 功能操作 ​

在开始测试前,我们先配置一个测试Job,该Job包含两个实例,实例分别包含了两个标签,__machine_hostname__machine_idc__

yaml

scrape_configs:
  - job_name: 'myjob'
    static_configs:
    - targets: 
      -  '10.12.61.1:9100'
      labels: 
        __machine_hostname__: 'node-01'
        __machine_idc__: 'idc-01'
    - targets: 
      -  '10.12.61.2:9100'
      labels: 
        __machine_hostname__: 'node-02'
        __machine_idc__: 'idc-02'

replace操作 ​

将__machine_hostname__的值替换到新标签hostname

yaml

scrape_configs:
  - job_name: 'myjob'
    static_configs:
    - targets: 
      -  '10.12.61.1:9100'
      labels: 
        __machine_hostname__: 'node-01'
        __machine_idc__: 'idc-01'
    - targets: 
      -  '10.12.61.2:9100'
      labels: 
        __machine_hostname__: 'node-02'
        __machine_idc__: 'idc-02'
    relabel_configs:
    - source_labels: [__machine_hostname__]
      regex: "(.*)"
      target_label: "hostname"
      action: replace
      replacement: '$1'

重启Prometheus后,查看target信息如下:

keep/drop操作 ​

排除标签值不匹配正则的targets 目标,此处正则匹配__machine_hostname__: 'node-01' 。

yaml


scrape_configs:
  - job_name: 'myjob'
    static_configs:
    - targets: 
      -  '10.12.61.1:9100'
      labels: 
        __machine_hostname__: 'node-01'
        __machine_idc__: 'idc-01'
    - targets: 
      -  '10.12.61.2:9100'
      labels: 
        __machine_hostname__: 'node-02'
        __machine_idc__: 'idc-02'
    relabel_configs:
    - source_labels: [__machine_hostname__]
      regex: "(.*)-01"
      target_label: "hostname"
      action: keep
      replacement: '$1'

重启后,会发现只有一个__machine_hostname__: 'node-01'的实例了

如果将上面配置的action改为drop,则结果相反,将删除正则匹配到标签的实例。

labelmap操作 ​

重写新的标签hostname和idc,使用原有__machine_hostname__和__machine_idc__标签的值。

yaml

scrape_configs:
  - job_name: 'myjob'
    static_configs:
    - targets: 
      -  '10.12.61.1:9100'
      labels: 
        __machine_hostname__: 'node-01'
        __machine_idc__: 'idc-01'
    - targets: 
      -  '10.12.61.2:9100'
      labels: 
        __machine_hostname__: 'node-02'
        __machine_idc__: 'idc-02'
    relabel_configs:
      - action: labelmap
        regex: __machine_(.+)__

查看target信息,可看到重写的新标签。

两个实例的标签

hostname="node-01",idc="idc-01",instance="10.12.61.1:9100",job="myjob"
hostname="node-02",idc="idc-02",instance="10.12.61.2:9100",job="myjob"