Prometheus 動態lables標簽


prometheus lables

介紹

本篇筆記重點學習Prometheus的relabel_config.也就是標簽的管理,包括標簽過濾,自定義標簽,動態替換標簽等.

在Prometheus的官方文檔有詳細介紹:

https://prometheus.io/docs/prometheus/latest/configuration/configuration/#relabel_config

一. 標簽作用

Prometheus中存儲的數據為時間序列,是由Metric的名字和一系列的標簽(鍵值對)唯一標識的, 不同的標簽代表不同的時間序列,即 通過指定標簽查詢指定數據 。

Alertmanager還可以根據標簽進行分組,將同一個標簽的多個告警通知,打包成一條告警短信發送.

二.Metadata標簽

在Prometheus所有的Target實例中,都包含一些默認的Metadata標簽信息。可以通過Prometheus UI的Targets頁面中查看這些實例的Metadata標簽的內容:
• address:當前Target實例的訪問地址:
• scheme:采集目標服務訪問地址的HTTP Scheme,HTTP或者HTTPS
• metrics_path:采集目標服務訪問地址的訪問路徑
一般來說元標簽只是Prometheus內部使用,**我們一般不會讓其做什么事情,並且這些標簽是不會寫到數據庫當中的,使用promql是查詢不到這些標簽的.

三.自定義標簽

在Prometheus的配置文件里可以定義laebls.添加標簽的Key:Value信息.Key是標簽名,Value是標簽值.在下面的例子中,為某個target定義了2個標簽:

1
2
3
4
5
6
7
8
9
- job_name: 'Shanghai Linux Server'
basic_auth:
username: prometheus
password: 123456
static_configs:
- targets: ['192.168.179.99:9100']
labels:
idc: sss
project: blog

修改標簽

在下面2個階段可以對標簽進行修改:

  • relabel_configs : 在數據采集之前.在這個階段可以過濾特定的監控目標(keep,drop),或者過濾某個標簽(labelkeep,labeldrop)
  • metric_relabel_configs : 抓取到數據之后.對標簽進行重新標記

修改標簽的場景:

  • 動態生成新標簽(根據已有標簽生成新的標簽)
  • 過濾target(對匹配某個標簽的target進行過濾)
  • 刪除不需要標簽
  • 添加新標簽

標簽匹配之后的動作Action:

  • replace : 默認.通過regex匹配source_label的值,使用replacement來引用表達式匹配的分組,分組使用$1,$2…引用(正則匹配,提取字段重新創建新標簽,注意這里是創建新的標簽
  • keep : 刪除regex與連接不匹配的目標 source_labels,讓Prometheus丟棄沒有匹配到regex的target
  • drop : 刪除regex與連接匹配的目標 source_labels,讓Prometheus丟棄匹配到regex的target

  • labeldrop : 刪除regex匹配的標簽,和drop 區別是這里是刪除某個標簽,而不是刪除target

  • labelkeep: 刪除regex不匹配的標簽,和keep 區別是這里是刪除某個標簽,而不是刪除target

  • labelmap : 匹配regex所有標簽名稱,並將捕獲的內容分組,用第一個分組內容作為新的標簽名(使用正則提取出多個字段,使用匹配到的作為新標簽名,但是標簽的內容不會改變,相對於對原有標簽換了個名字,原標簽仍然存在)

修改標簽場景實戰

下面以Prometheus+Consul的自動發現為例,介紹修改標簽的實際場景.

服務器通過Consul的API進行注冊:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
cat /tmp/consul.json
{
"ID": "node-exporter-172.26.4.53",
"Name": "node-exporter-172.26.4.53",
"Tags": [
"node-exporter"
],
"Address": "172.26.4.53",
"Port": 9100,
"Meta": {
"app": "nginx",
"project": "abc"

},
"EnableTagOverride": false,
"Check": {
"HTTP": "http://172.26.4.53:9100/metrics",
"Interval": "30s"
},
"Weights": {
"Passing": 10,
"Warning": 1
}
}

服務注冊:

1
curl --request PUT --data @consul-0.json http://172.16.83.201:8500/v1/agent/service/register

下面是Prometheus的配置文件

1
2
3
4
5
6
7
8
9
10
- job_name: 'consul-prometheus'
consul_sd_configs:
- server: '172.16.83.201:8500'
services: []
relabel_configs:
- source_labels: [__meta_consul_tags]
regex: .*node-exporter.*
action: keep
- regex: __meta_consul_service_metadata_(.+)
action: labelmap

在Prometheus的UI界面的Service Discovery自動發現可以看到target的label信息

image-20210914172649800

左邊是通過consul注冊時的meta元標簽,在Consul注冊時定義的數據都會以__meta_consul_的標簽格式展示.而之前提到過, __meta開頭的是元數據.只供Prometheus內部使用.

右邊的target labels是經過匹配,修改后的標簽.而target labels的標簽才是我們可以查詢或者搜索的,有實際意義的標簽.

修改標簽場景一: 過濾Target

在Prometheus的配置文件中下面的relabel_configs 定義了一條匹配到

1
2
3
4
relabel_configs:
- source_labels: [__meta_consul_tags]
regex: .*node-exporter.*
action: keep

我們在Consul注冊的時候定義了一個Tag:node-exporter .這個配置表示保留__meta_consul_tags 這個標簽的值是”.node-exporter.“ 的target.如果服務器注冊Consul時,tags不能匹配到”.node-exporter.那么這個服務器會被Prometheus過濾.即使這個服務器運行了node-exporter

我們還可以通過drop將consul自身的服務器從target里拿掉.

1
2
3
- source_labels: [__meta_consul_service]
regex: 'consul'
action: drop

__meta_consul_service 匹配到consul 值的標簽就drop掉.不監控consul服務器

修改標簽場景二: 動態新增自定義標簽

在通過Consul注冊的時候可以攜帶自定義標簽:

1
2
3
4
5
"Meta": {
"app": "nginx",
"project": "abc"

},

這些自定義的標簽,在Prometheus里會自動添加下面2個標簽:

__meta_consul_service_metadata_app=nginx

__meta_consul_service_metadata_project=abc

但是這些標簽是元標簽.所以要轉換成自定義標簽.通過下面的Prometheus配置:

1
2
3
4
5
6
7
- job_name: 'consul-prometheus'
consul_sd_configs:
- server: '172.16.83.201:8500'
services: []
relabel_configs:
- regex: __meta_consul_service_metadata_(.+)
action: labelmap

匹配__meta_consul_service_metadata_(.+)的標簽,然后將匹配到的分組(.+) 生成一個新的標簽.

也就是__meta_consul_service_metadata_app=nginx 匹配到app=nginx .然后生產一個app=nginx的標簽.也就是在Prometheus的UI界面中看到的target labels

labelmap的作用是產生一個新的標簽,原來的標簽仍然存在.

目前我是利用Ansible的jinjia2模板動態為服務器定義標簽,然后在Prometheus中利用labelmap將標簽動態生成為自定義標簽.

修改標簽場景三: 刪除某個標簽

有些標簽不希望被存儲上,那么可以使用 labeldrop:刪除regex匹配的標簽去完成不需要入庫 將里面的標簽刪除掉,在入庫之前刪除

1
2
3
4
5
6
7
8
9
- job_name: 'Linux Server'
basic_auth:
username: prometheus
password: 123456
static_configs:
- targets: ['192.168.179.99:9100']
relabel_configs:
- action: labeldrop
regex: "job" #正則匹配標簽名稱

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM