前言
在
prometheus
監控體系中。標簽label
是一個極為重要的參數,考慮到要明智的使用標簽,需要使用標准的標簽對整個集群進行管理控制,特別是在復雜的環境中。
一些常見的標簽操作案例:
- 重命名標簽名
- 刪除標簽
- 過濾目標
特別注意的是,上列操作,只有兩個階段我們可以對標簽進行操作:
- 第一階段是重新標記來自服務發現的目標。
這對於服務發現的元數據標簽的信息引用到度量上的標簽費用游泳,這些將在
relabel_configs
模塊中完成。
- 第二階段是在刮刮
scape
之后,但在保存到存儲系統之前。
這使我們能夠確保我們保存了那些指標,刪除了那些指標,以及這些指標將是什么樣子,這些工作將在
metric_relabel_configs
模塊中完成
其實就是:在發生在 scape
之前 使用 relabel_configs
,發生在 scape
之后 使用 metric_relabel_configs
.
action:重新標簽動作
replace
:默認,通過regex匹配source_label的值,使用replacement來引用表達式匹配的分組keep
:刪除regex與連接不匹配的目標 source_labelsdrop
:刪除regex與連接匹配的目標 source_labelslabeldrop
:刪除regex匹配的標簽labelkeep
:刪除regex不匹配的標簽hashmod
:設置target_label為modulus連接的哈希值source_labelslabelmap
:匹配regex所有標簽名稱。然后復制匹配標簽的值進行分組,replacement分組引用(${1},${2},…)替代
**relabel_configs **
relable_configs:
# 源標簽
[ source_labels: '[' <labelname> [, ...] ']' ]
# 多個源標簽時連接的分隔符
[ separator: <string> | default = ; ]
# 重新標記的標簽
[ target_label: <labelname> ]
# 整則表達式匹配源標簽的值
[ regex: <regex> | default = (.*) ]
# 用的少,占時略
[ modulus: <uint64> ]
# 替換正則表達式匹配的分組,分組引用 $1,$2,$3,....
[ replacement: <string> | default = $1 ]
# 基於正則表達式匹配執行的操作
[ action: <relabel_action> | default = replace ]
配置測試
刪除metric值
測試基於 cAdvisor 監控容器 文章。
刪除 container_tasks_state
| container_memory_failures_total
這兩個標簽
在配置前先查看這兩個標簽的數據是正常的。
然后再進行配置:
編輯 prometheus.yml
文件,增加下來內容
[root@es01 config]# cat prometheus.yml
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: 'cadvisor'
metrics_path: '/metrics'
static_configs:
- targets: ['10.0.20.11:8080']
relabel_configs:
- source_labels: [__name__] # 匹配原標簽的名字
separator: ',' # 分隔符,擁有就匹配原標簽時,有多個標簽,則用逗號 `,` 分割
regex: '(container_tasks_state|container_memory_failures_total)' # 匹配要刪除的標簽名稱,如果有多個正則,用分號隔開`;`
action: drop # 匹配的動作
重新加載配置文件后測試
[root@es01 config]# /opt/prometheus-2.14/bin/promtool check config /opt/prometheus-2.14/config/prometheus.yml
Checking /opt/prometheus-2.14/config/prometheus.yml
SUCCESS: 0 rule files found
[root@es01 config]# curl -X POST http://10.0.20.11:9090/-/reload
重載配置文件完成后,稍等便宜,當再次查詢的時候,會發現 container_tasks_state
| container_memory_failures_total
這兩個標簽已無法查詢到數據
更換
其實所謂的更換,是匹配到對應的標簽內容后,再次增加一個key
來對應匹配的值
測試
編輯 prometheus.yml
文件,增加下來內容
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: 'cadvisor'
metrics_path: '/metrics'
static_configs:
- targets: ['10.0.20.11:8080']
relabel_configs:
- source_labels: [__name__]
separator: ','
regex: '(container_tasks_state|container_memory_failures_total)'
action: drop
- source_labels: [id] # 匹配原標簽
regex: '/docker/([a-z0-9]+)' # 匹配對應的值
target_label: container_id # 新的標簽的key值
replacement: '$1' # 替換的內容 value
action: replace # 動作為替換
重新加載配置文件后測試
[root@es01 config]# /opt/prometheus-2.14/bin/promtool check config /opt/prometheus-2.14/config/prometheus.yml
Checking /opt/prometheus-2.14/config/prometheus.yml
SUCCESS: 0 rule files found
[root@es01 config]# curl -X POST http://10.0.20.11:9090/-/reload
由此可以看出,已經新增了一個標簽。
刪除 Label 標簽
在獲取到的 metric 數據中的一些label,無用則可以通過匹配后刪除
編輯 prometheus.yml
文件,增加下來內容
[root@es01 config]# cat prometheus.yml
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: 'cadvisor'
metrics_path: '/metrics'
static_configs:
- targets: ['10.0.20.11:8080']
relabel_configs:
- source_labels: [__name__]
separator: ','
regex: '(container_tasks_state|container_memory_failures_total)'
action: drop
- source_labels: [id]
regex: '/docker/([a-z0-9]+)'
target_label: container_id
replacement: '$1'
action: replace
- regex: 'kernelVersion' # 正則匹配標簽
action: labeldrop # 執行動作刪除匹配的標簽