prometheus -label_replace中的细节
首先看下函数label_replace的语法:
label_replace(v instant-vector, dst_label string, replacement string, src_label string, regex string)
v instant-vector 代表要操作的指标,其内包含了我们要替换的label;
dst_label 替换/新增(原始label不存在)label后的label 名字;
replacement 替换/新增label的value,可以是常量,可以是$[0/1/2…]格式的占位符,其后面的序号代表regex的匹配变量位置;
src_label 原始的label;
regex 正则表达式;
上几个例子说明一下replacement的具体含义:
1、replacement定义为常量
原始指标
http_requests_total{code=“400”,handler=“query”,instance=“120.77.65.193:9090”,job=“prometheus”,method=“get”} 9
label_replace函数执行
label_replace(http_requests_total{code=“400”,handler=“query”,instance=“120.77.65.193:9090”,job=“prometheus”,method=“get”}, “instance”, “hello,world”, “method”, “ge.*”)
显示结果:
http_requests_total{code=“400”,handler=“query”,instance=“hello,world”,job=“prometheus”,method=“get”} 9
2、replacement定义为变量
示例1
$1,从表达式的第二项开始匹配
原始指标
container_last_seen{application=“120501_commission_data_service”, container=“120501-0-930614”}
label_replace函数执行
label_replace(container_last_seen{application=“120501_commission_data_service”},“lineId”,“$1”,“container”,“120.-0-(.)”)
显示结果:
container_last_seen{application=“120501_commission_data_service”, container=“120501-0-930614”, lineId=“930614”}
示例2
$0,从表达式的第一项开始匹配
原始指标
container_last_seen{application=“120501_commission_data_service”, container=“120501-0-930614”}
label_replace函数执行
label_replace(container_last_seen{system=“mione”,application=“120501_commission_data_service”},“pId”,“$0”,“container”,“.-0-(.)”)
显示结果:
container_last_seen{application=“120501_commission_data_service”, container=“120501-0-930614”, pId=“120501-0-930614”}
由此可见,replacement为$[1/2/3…]的时候,指的是正则表达式开始位置,$0表示从正则表达式的第一个变量开始匹配,$1表示从正则表达式的第二个变量开始匹配,依此类推!