前面五章已经围绕prometheus讲解了相关的知识点,本章节主要对prometheus的另外一个知识点进行补充。
prometheus的标签管理
在prometheus监控体系中,标签label是一个极为重要的参数,要合理的使用标签,需要使用标准的标签对整个服务进行管理控制,特别是在复杂的环境中。
1、一些常见的标签操作案例
1、重命名标签名
2、删除标签
3、过滤目标
特别注意的是,上列操作,只有两个阶段我们可以对标签进行操作:
第一阶段是重新标记来自服务发现的目标。
这对于服务发现的元数据标签的信息引用到度量上的标签,这些将在relabel_configs模块中完成。
第二阶段是在scrape之后,但在保存到存储系统之前。
这使我们能够确保我们保存了那些指标,删除了那些指标,以及这些指标将是什么样子,这些工作将在metric_relabel_configs模块中完成
其实就是:在发生在 scrape之前 使用 relabel_configs,发生在 scape 之后 使用 metric_relabel_configs.
2、action:重新标签动作
replace:默认,通过regex匹配source_label的值,使用replacement来引用表达式匹配的分组
keep:删除regex与连接不匹配的目标 source_labels
drop:删除regex与连接匹配的目标 source_labels
labeldrop:删除regex匹配的标签
labelkeep:删除regex不匹配的标签
hashmod:设置target_label为modulus连接的哈希值source_labels
labelmap:匹配regex所有标签名称。然后复制匹配标签的值进行分组,replacement分组引用(${1},${2},…)替代
3、action的格式
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 ]
配置案例
1、新增标签
1) 添加标签键值对
cat /home/monitor/prometheus/conf.d/es_cluster.json
[
{
"labels": {
#labels下的键值对都是标签,可以根据实际需要添加即可
"desc": "es-21",
"group": "es_cluster",
"host_ip": "192.168.16.21",
"hostname": "wg-16-21",
"nodes": "es651" #新增的标签键值对
},
"targets": [
"192.168.16.21:4221"
]
}
]
2) 热启动服务查看
curl -XPOST http://192.168.16.115:9090/-/reload #重载服务
界面查看
2、relabel_configs:action为replace
1) 对prometheus.yml的某个任务进行重新定义
cat /home/monitor/prometheus/prometheus.yml
...
...
...
- job_name: 'es_cluster'
relabel_configs: #添加relabel_configs配置,添加在job_name是es_cluster的下面
- source_labels: ["job"] #匹配原labels名称,此处匹配是job
regex: "(.*)" #正则匹配所有
action: replace #动作是替换,除了替换别的动作
replacement: $1 #替换第一个值
target_label: "es651" #新的label的key值
scrape_interval: 1m
static_configs:
file_sd_configs:
- files:
- /home/monitor/prometheus/conf.d/es_cluster.json
honor_labels: true
...
...
...
2) 热启动服务查看
cd /home/monitor/prometheus && ./promtool check config prometheus.yml #校验配置文件
curl -XPOST http://192.168.16.115:9090/-/reload #重载服务
界面查看
3、relabel_configs:action为drop动作
1) 对prometheus.yml的某个任务进行删除
cat /home/monitor/prometheus/prometheus.yml
...
...
...
- job_name: 'es_cluster'
relabel_configs: #添加relabel_configs配置,添加在job_name是es_cluster的下面
- source_labels: ["job"] #匹配原labels名称,此处匹配是job
regex: "(.*)" #正则匹配所有
action: replace #动作是替换,除了替换别的动作
replacement: $1 #替换第一个值
target_label: "es651" #重新定义的job的label值
- source_labels: ["job"] #匹配原labels名称,此处匹配是job
action: drop #将原标签为job实例均删除,即不采集
scrape_interval: 1m
static_configs:
file_sd_configs:
- files:
- /home/monitor/prometheus/conf.d/es_cluster.json
honor_labels: true
...
...
...
2) 热启动服务查看
加载后如下图所示,无数据-nodata
4、relabel_configs:action为keep
1) 对prometheus.yml的某个任务进行保留
cat /home/monitor/prometheus/prometheus.yml
...
...
...
- job_name: 'es_cluster'
relabel_configs: #添加relabel_configs配置,添加在job_name是es_cluster的下面
- source_labels: ["job"] #匹配原labels名称,此处匹配是job
regex: "(.*)" #正则匹配所有
action: replace #动作是替换,除了替换别的动作
replacement: $1 #替换第一个值
target_label: "es651" #重新定义的job的label值
- source_labels: ["job"] #匹配原labels名称,此处匹配是job
action: keep #将原标签为job实例保留
scrape_interval: 1m
static_configs:
file_sd_configs:
- files:
- /home/monitor/prometheus/conf.d/es_cluster.json
honor_labels: true
...
...
...
2) 热启动服务查看
加载后如下图所示
5、relabel_configs:action为labeldrop
1) 对prometheus.yml的某个任务进行原标签删除
cat /home/monitor/prometheus/prometheus.yml
...
...
...
- job_name: 'es_cluster'
relabel_configs: #添加relabel_configs配置,添加在job_name是es_cluster的下面
- source_labels: ["job"] #匹配原labels名称,此处匹配是job
regex: "(.*)" #正则匹配所有
action: replace #动作是替换,除了替换别的动作
replacement: $1 #替换第一个值
target_label: "es651" #重新定义的job的label值
- source_labels: ["job"] #匹配原labels名称,此处匹配是job
action: keep #将原标签为job实例保留
- action: labeldrop #将原标签为job删除
regex: "job" #匹配名称为job的标签
scrape_interval: 1m
static_configs:
file_sd_configs:
- files:
- /home/monitor/prometheus/conf.d/es_cluster.json
honor_labels: true
...
...
...
2) 热启动服务查看
加载后如下图所示