relabel_config
重新標記是一個功能強大的工具,可以在目標的標簽集被抓取之前重寫它,每個采集配置可以配置多個重寫標簽設置,並按照配置的順序來應用於每個目標的標簽集。
目標重新標簽之后,以__開頭的標簽將從標簽集中刪除的。
如果使用只需要臨時的存儲臨時標簽值的,可以使用_tmp作為前綴標識。
relabel的action類型
- replace: 對標簽和標簽值進行替換。
- keep: 滿足特定條件的實例進行采集,其他的不采集。
- drop: 滿足特定條件的實例不采集,其他的采集。
- hashmod: 這個我也沒看懂啥意思,囧。
- labelmap: 這個我也沒看懂啥意思,囧。
- labeldrop: 對抓取的實例特定標簽進行刪除。
- labelkeep: 對抓取的實例特定標簽進行保留,其他標簽刪除。
常用action的測試
在測試前,同步下配置文件如下。
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config. - job_name: 'prometheus' # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - targets: ['localhost:9090'] - job_name: "node" file_sd_configs: - refresh_interval: 1m files: - "/usr/local/prometheus/prometheus/conf/node*.yml"
[root@node00 prometheus]# cat conf/node-dis.yml
- targets:
- "192.168.100.10:20001"
labels:
__hostname__: node00
__businees_line__: "line_a"
__region_id__: "cn-beijing"
__availability_zone__: "a"
- targets:
- "192.168.100.11:20001"
labels:
__hostname__: node01
__businees_line__: "line_a"
__region_id__: "cn-beijing"
__availability_zone__: "a"
- targets:
- "192.168.100.12:20001"
labels:
__hostname__: node02
__businees_line__: "line_c"
__region_id__: "cn-beijing"
__availability_zone__: "b"
此時如果查看target信息,如下圖。
因為我們的label都是以__開頭的,目標重新標簽之后,以__開頭的標簽將從標簽集中刪除的。
一個簡單的relabel設置
將labels中的__hostname__替換為node_name。
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config. - job_name: 'prometheus' # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - targets: ['localhost:9090'] - job_name: "node" file_sd_configs: - refresh_interval: 1m files: - "/usr/local/prometheus/prometheus/conf/node*.yml" relabel_configs: - source_labels: - "__hostname__" regex: "(.*)" target_label: "nodename" action: replace replacement: "$1"
重啟服務查看target信息如下圖:
說下上面的配置: source_labels指定我們我們需要處理的源標簽, target_labels指定了我們要replace后的標簽名字, action指定relabel動作,這里使用replace替換動作。 regex去匹配源標簽(__hostname__)的值,"(.*)"代表__hostname__這個標簽是什么值都匹配的,然后replacement指定的替換后的標簽(target_label)對應的數值。采用正則引用方式獲取的。
這里修改下上面的正則表達式為 ‘’regex: "(node00)"'的時候可以看到如下圖。
keep
修改配置文件
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config. - job_name: 'prometheus' # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - targets: ['localhost:9090'] - job_name: "node" file_sd_configs: - refresh_interval: 1m files: - "/usr/local/prometheus/prometheus/conf/node*.yml"
target如下圖
修改配置文件如下
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config. - job_name: 'prometheus' # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - targets: ['localhost:9090'] - job_name: "node" file_sd_configs: - refresh_interval: 1m files: - "/usr/local/prometheus/prometheus/conf/node*.yml" relabel_configs: - source_labels: - "__hostname__" regex: "node00" action: keep
target如下圖
action為keep,只要source_labels的值匹配regex(node00)的實例才能會被采集。 其他的實例不會被采集。
drop
在上面的基礎上,修改action為drop。
target如下圖
action為drop,其實和keep是相似的, 不過是相反的, 只要source_labels的值匹配regex(node00)的實例不會被采集。 其他的實例會被采集。
replace
我們的基礎信息里面有__region_id__和__availability_zone__,但是我想融合2個字段在一起,可以通過replace來實現。
修改配置如下
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config. - job_name: 'prometheus' # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - targets: ['localhost:9090'] - job_name: "node" file_sd_configs: - refresh_interval: 1m files: - "/usr/local/prometheus/prometheus/conf/node*.yml" relabel_configs: - source_labels: - "__region_id__" - "__availability_zone__" separator: "-" regex: "(.*)" target_label: "region_zone" action: replace replacement: "$1"
target如下圖:
labelkeep
配置文件如下
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config. - job_name: 'prometheus' # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - targets: ['localhost:9090'] - job_name: "node" file_sd_configs: - refresh_interval: 1m files: - "/usr/local/prometheus/prometheus/conf/node*.yml" relabel_configs: - source_labels: - "__hostname__" regex: "(.*)" target_label: "nodename" action: replace replacement: "$1" - source_labels: - "__businees_line__" regex: "(.*)" target_label: "businees_line" action: replace replacement: "$1" - source_labels: - "__datacenter__" regex: "(.*)" target_label: "datacenter" action: replace replacement: "$1"
target如下圖
修改配置文件如下
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config. - job_name: 'prometheus' # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - targets: ['localhost:9090'] - job_name: "node" file_sd_configs: - refresh_interval: 1m files: - "/usr/local/prometheus/prometheus/conf/node*.yml" relabel_configs: - source_labels: - "__hostname__" regex: "(.*)" target_label: "nodename" action: replace replacement: "$1" - source_labels: - "__businees_line__" regex: "(.*)" target_label: "businees_line" action: replace replacement: "$1" - source_labels: - "__datacenter__" regex: "(.*)" target_label: "datacenter" action: replace replacement: "$1" - regex: "(nodename|datacenter)" action: labeldrop
target如下圖