- 6.1relabel_config的位置
- 6.2 relabel_config參數詳解
- 1.replace
- 2. keep
- 3.drop
- 6.labelkeep
- 7.hashmod
- 6.3 正則表達式
重新標記是一種強大的工具,可以在抓取目標之前動態重寫目標的標簽集。 每個抓取配置可以配置多個重新標記步驟。 按照它們在配置文件中的出現順序應用於每個目標的標簽集。
6.1relabel_config的位置
1 global: 2 .... 3 # 抓取配置列表. 4 scrape_configs: 5 [ - <scrape_config> ... ]....
1 # The job name assigned to scraped metrics by default. 2 job_name: <job_name> 3 4 [ honor_labels: <boolean> | default = false ] 5 6 [ honor_timestamps: <boolean> | default = true ] 7 8 # List of target relabel configurations. 9 relabel_configs: 10 [ - <relabel_config> ... ] 11 12 # List of metric relabel configurations. 13 metric_relabel_configs: 14 [ - <relabel_config> ... ] 15 ......
6.2 relabel_config參數詳解
1 # 源標簽從現有標簽中選擇值。 它們的內容使用已配置的分隔符進行連接,並與已配置的正則表達式進行匹配,以進行替換,保留和刪除操作。 2 [ source_labels: '[' <labelname> [, ...] ']' ] 3 4 # 分隔符放置在連接的源標簽值之間。 5 [ separator: <string> | default = ; ] 6 7 # 在替換操作中將結果值寫入的標簽。 8 # 替換操作是強制性的。 正則表達式捕獲組可用。 9 [ target_label: <labelname> ] 10 11 # 與提取的值匹配的正則表達式。 12 [ regex: <regex> | default = (.*) ] 13 14 # 采用源標簽值的散列的模數。 15 [ modulus: <uint64> ] 16 17 # 如果正則表達式匹配,則執行正則表達式替換的替換值。 正則表達式捕獲組可用。 18 [ replacement: <string> | default = $1 ] 19 20 # 基於正則表達式匹配執行的操作。 21 [ action: <relabel_action> | default = replace ]
<regex>
是任何有效的RE2正則表達式。 它是replace
,keep
,drop
,labelmap
,labeldrop
和labelkeep
操作所必需的。 正則表達式固定在兩端。 要取消錨定正則表達式,請使用。* <regex>.*
。
<relabel_action>
確定要采取的重新簽名行動:
1.replace
將regex
與連接的source_labels
匹配。 然后,將target_label
設置為replacement
,將匹配組引用(${1}
,${2}
,...)替換為其值。 如果正則表達式不匹配,則不進行替換。
1 scrape_configs: 2 - job_name: file 3 file_sd_configs: 4 - files: 5 - '*.json' 6 #例如:team="monitoring"-->team_new="monitor" 7 relabel_configs: 8 -source_labels: [team] 9 regex: monitoring 10 replacement: monitor 11 target_label: team_new 12 action: replace 13 #action的默認值是replace 14 - source_labels: [__meta_consul_address] 15 regex: '(.*)' 16 replacement: '${1}:9100' 17 target_label: __address__ 18 #參看上面看看這個什么意思 19 - source_labels: [__meta_consul_tag] 20 regex: '.*,(prod|staging|dev),.*' 21 target_label: env
2. keep
刪除regex
與連接的source_labels
不匹配的目標。
regex
與連接的source_labels
不匹配的目標。1 scrape_configs: 2 - job_name: node 3 consul_sd_configs: 4 - server: 'localhost:8500' 5 relabel_configs: 6 - source_labels: [__meta_consul_tag] 7 regex: '.*,prod,.*' 8 action: keep
3.drop
刪除regex
與連接的source_labels
匹配的目標。
1 scrape_configs: 2 - job_name: node 3 consul_sd_configs: 4 - server: 'localhost:8500' 5 relabel_configs: 6 - source_labels: [__meta_consul_tag] 7 regex: '.*,prod,.*' 8 action: drop
4.labelmap
將regex
與所有標簽名稱匹配。 然后將匹配標簽的名稱替換為replacement指定的值。(不同於replace,keep,drop,只改變label名,不修改標簽值)
1 scrape_configs: 2 - job_name: ec2 3 ec2_sd_configs: 4 - region: <region> 5 access_key: <access key> 6 secret_key: <secret key> 7 #將所有以monitor_開頭的標簽名替換為去掉monitor_前綴的新標簽名字(例如:monitor_foo="bar"-->foo="bar") 8 relabel_configs: 9 - regex: monitor_(.*) 10 replacement: '${1}' 11 action: labelmap
5.labeldrop
將regex
與所有標簽名稱匹配。匹配的任何標簽都將從標簽集中刪除。(理labelmap)5.labeldrop
6.labelkeep
將regex
與所有標簽名稱匹配。任何不匹配的標簽都將從標簽集中刪除。(同理labelmap)
7.hashmod
將target_label
設置為連接的source_labels
的哈希模數。
1 scrape_configs: 2 - job_name: my_job 3 # Service discovery etc. goes here. 4 relabel_configs: 5 #prometheus從特定targets隨機抓取數據量的10% 6 - source_labels: [__address__] 7 modulus: 10 8 target_label: __tmp_hash 9 action: hashmod 10 - source_labels: [__tmp_hash] 11 regex: 0 #指定抓取的目標 12 action: keep
必須小心使用labeldrop
和labelkeep
,以確保在刪除標簽后仍然對指標進行唯一標記。
6.3 正則表達式