label_replace和label_join是PromQL的预置函数,支持将label的value进行截取和拼接,生成新的label。
值得注意的是,它们不改变源label的name及value,仅生成新label。
label_replace
支持对某个label的值进行正则匹配,截取出某些值,生成新的label。
label_replace(v instant-vector, dst_label string, replacement string, src_label string, regex string)
原始指标:
node_network_transmit_bytes_total{container="kube-rbac-proxy",device="eth0",endpoint="https",instance="k8s-v22-1",job="node-exporter",namespace="monitoring",pod="node-exporter-nvj7r",service="node-exporter"} 9457895219
通过label_replace将device==>iface
label_replace(node_network_transmit_bytes_total{job="node-exporter", device="eth0"}, "iface", "$1", "device", "(.+)")
结果:
node_network_transmit_bytes_total{container="kube-rbac-proxy",device="eth0",endpoint="https",iface="eth0",instance="k8s-v22-1",job="node-exporter",namespace="monitoring",pod="node-exporter-nvj7r",service="node-exporter"} 9465702581
label_join
支持将某些label拼接起来,生成新的label。
label_join(v instant-vector, dst_label string, separator string, src_label_1 string, src_label_2 string, ...)
原始指标:
kubelet_node_name{endpoint="https-metrics",instance="178.104.163.63:10250",job="kubelet",metrics_path="/metrics",namespace="kube-system",node="k8s-v22-1",service="kubelet"} 1
通过label_join将instance和metrics_path拼接起来,生成新label: metrics_url
label_join(kubelet_node_name, "metrics_url", "", "instance", "metrics_path")
结果:
kubelet_node_name{endpoint="https-metrics",instance="178.104.163.63:10250",job="kubelet",metrics_path="/metrics",metrics_url="178.104.163.63:10250/metrics",namespace="kube-system",node="k8s-v22-1",service="kubelet"} 1