从新标记是一个功能强大的工具,能够在目标的标签集被抓取以前重写它,每一个采集配置能够配置多个重写标签设置,并按照配置的顺序来应用于每一个目标的标签集。node
目标从新标签以后,以__开头的标签将从标签集中删除的。linux
若是使用只须要临时的存储临时标签值的,可使用_tmp做为前缀标识。服务器
在测试前,同步下配置文件以下。ide
global: scrape_interval: 15s evaluation_interval: 15s alerting: alertmanagers: - static_configs: - targets: rule_files: scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['localhost:9090'] - job_name: 'node_export' static_configs: - targets: - deliver-database:9100 - basic-database:9100 - ETL:9100 - APP3:9100 - APP5:9100 - APP4:9100 - QCD1:9100 - QCD2:9100 - QCA1:9100
以上的机器,我已经在 hosts 作好解析了。工具
此时若是查看target信息,以下图。测试
这些都是默认的 label ,由于系统生成的 label 都是以__
开头的,目标从新标签以后,以__
开头的标签将从标签集中删除的。lua
好比将全部 labels 中的 instance 的 9100 端口去掉code
global: scrape_interval: 15s evaluation_interval: 15s alerting: alertmanagers: - static_configs: - targets: rule_files: scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['localhost:9090'] - job_name: 'node_export' static_configs: - targets: - deliver-database:9100 - basic-database:9100 - ETL:9100 - APP3:9100 - APP5:9100 - APP4:9100 - QCD1:9100 - QCD2:9100 - QCA1:9100 relabel_configs: - source_labels: - "__address__" regex: "(.*):9100" target_label: "instance" action: replace replacement: "$1"
重启服务查看 target 信息以下图:blog
说下上面的配置:get
__address__
instance
__address__
的值,"(.*):9100"
表明匹配这个表情9100前部分好比只采集所匹配名称为 APP* 的机器。
修改配置文件以下
global: scrape_interval: 15s evaluation_interval: 15s alerting: alertmanagers: - static_configs: - targets: rule_files: scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['localhost:9090'] - job_name: 'node_export' static_configs: - targets: - deliver-database:9100 - basic-database:9100 - ETL:9100 - APP3:9100 - APP5:9100 - APP4:9100 - QCD1:9100 - QCD2:9100 - QCA1:9100 relabel_configs: - source_labels: - "__address__" regex: "APP.*" action: keep
重启以后查看 target 以下图,能够看到只采集 APP 开头的服务器。
在上面的基础上,修改 action 为 drop。
重启以后,target 以下图
action 为 drop,其实和 keep 是类似的, 不过是相反的, 只要 source_labels 的值匹配APP.*
的实例不会被采集。 其余的实例会被采集。
配置文件修改成以下
global: scrape_interval: 15s evaluation_interval: 15s alerting: alertmanagers: - static_configs: - targets: rule_files: scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['localhost:9090'] - job_name: 'node_export' static_configs: - targets: - deliver-database:9100 - basic-database:9100 - ETL:9100 - APP3:9100 - APP5:9100 - APP4:9100 - QCD1:9100 - QCD2:9100 - QCA1:9100 labels: os: "linux" relabel_configs: - source_labels: - "__address__" regex: "(.*):9100" target_label: "instance" action: replace replacement: "$1"