prometheus relabel 配置(四)

relabel_config

从新标记是一个功能强大的工具,能够在目标的标签集被抓取以前重写它,每一个采集配置能够配置多个重写标签设置,并按照配置的顺序来应用于每一个目标的标签集。node

目标从新标签以后,以__开头的标签将从标签集中删除的。linux

若是使用只须要临时的存储临时标签值的,可使用_tmp做为前缀标识。服务器

relabel的action类型

  • replace: 对标签和标签值进行替换。
  • keep: 知足特定条件的实例进行采集,其余的不采集。
  • drop: 知足特定条件的实例不采集,其余的采集。
  • hashmod: 这个我也没看懂啥意思,囧。
  • labelmap: 这个我也没看懂啥意思,囧。
  • labeldrop: 对抓取的实例特定标签进行删除。
  • labelkeep: 对抓取的实例特定标签进行保留,其余标签删除。

经常使用action的测试

在测试前,同步下配置文件以下。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信息,以下图。测试

image-20200524201723718

这些都是默认的 label ,由于系统生成的 label 都是以__开头的,目标从新标签以后,以__开头的标签将从标签集中删除的。lua

replace

好比将全部 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

image-20200524202121908

说下上面的配置:get

  • source_labels 指定咱们咱们须要处理的源标签, 咱们这里处理__address__
  • target_labels 指定了咱们要 replace 后的标签名字, 咱们这里选择instance
  • action 指定 relabel 动做,这里使用 replace 替换动做。
  • regex 去匹配源标签__address__的值,"(.*):9100"表明匹配这个表情9100前部分
  • replacement 指定的替换后的标签(target_label)对应的数值,采用正则引用方式获取的。

keep

好比只采集所匹配名称为 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 开头的服务器。

image-20200524203142788

drop

在上面的基础上,修改 action 为 drop。

重启以后,target 以下图

image-20200524203259914

action 为 drop,其实和 keep 是类似的, 不过是相反的, 只要 source_labels 的值匹配APP.*的实例不会被采集。 其余的实例会被采集。

给 target 手动增长 label

配置文件修改成以下

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"

image-20200524204614693

欢迎你们扫码关注,获取更多信息

prometheus relabel 配置(四)

相关文章
相关标签/搜索