PromQL中的group_left和group_right的操作函数,分别提供了Many2One和One2Many的语义,让2个不同的指标可以进行join。

完整的语法形式:

<vector expr> <bin-op> ignoring(<label list>) group_left(<label list>) <vector expr>
<vector expr> <bin-op> ignoring(<label list>) group_right(<label list>) <vector expr>
<vector expr> <bin-op> on(<label list>) group_left(<label list>) <vector expr>
<vector expr> <bin-op> on(<label list>) group_right(<label list>) <vector expr>

官方的demo比较难理解,这里提供一个比较切合实际使用的demo。

2个输入指标:

  • kube_pod_spec_volumes_persistentvolumeclaims_info:标识pvc和pod的挂载信息;
  • kubelet_volume_stats_used_bytes / kubelet_volume_stats_capacity_bytes:标识pvc的使用量/总量信息;
kube_pod_spec_volumes_persistentvolumeclaims_info

kube_pod_spec_volumes_persistentvolumeclaims_info{__type__="gauge",instance="127.0.0.1:9999",job="pvc-test",namespace="default",persistentvolumeclaim="myclaim",pod="nginx-pv-1"}    1
kube_pod_spec_volumes_persistentvolumeclaims_info{__type__="gauge",instance="127.0.0.1:9999",job="pvc-test",namespace="default",persistentvolumeclaim="myclaim",pod="nginx-pv-2"}    1
kube_pod_spec_volumes_persistentvolumeclaims_info{__type__="gauge",instance="127.0.0.1:9999",job="pvc-test",namespace="default",persistentvolumeclaim="myclaim",pod="nginx-pv-3"}    1
kube_pod_spec_volumes_persistentvolumeclaims_info{__type__="gauge",instance="127.0.0.1:9999",job="pvc-test",namespace="default",persistentvolumeclaim="myclaim--2",pod="busybox-xxx"}    1
kube_pod_spec_volumes_persistentvolumeclaims_info{__type__="gauge",instance="127.0.0.1:9999",job="pvc-test",namespace="default",persistentvolumeclaim="myclaim--2",pod="busybox-yyy"}    1
kubelet_volume_stats_used_bytes / kubelet_volume_stats_capacity_bytes

{__type__="gauge",instance="127.0.0.1:9999",job="pvc-test",namespace="default",persistentvolumeclaim="myclaim"}    0.5
{__type__="gauge",instance="127.0.0.1:9999",job="pvc-test",namespace="default",persistentvolumeclaim="myclaim--2"}    0.25

目标:

  • 通过2个指标的join,得到每个pod及其挂载的pvc的使用率;

答案:

(0 * kube_pod_spec_volumes_persistentvolumeclaims_info) + on(namespace,persistentvolumeclaim) group_left 100*(kubelet_volume_stats_used_bytes / kubelet_volume_stats_capacity_bytes)
  • 使用group_left实现了Many2One的语义;
  • 2个指标join时,以name和persistentvolumeclaim为维度;

结果:

{__type__="gauge",instance="127.0.0.1:9999",job="pvc-test",namespace="default",persistentvolumeclaim="myclaim",pod="nginx-pv-1"}    50
{__type__="gauge",instance="127.0.0.1:9999",job="pvc-test",namespace="default",persistentvolumeclaim="myclaim",pod="nginx-pv-2"}    50
{__type__="gauge",instance="127.0.0.1:9999",job="pvc-test",namespace="default",persistentvolumeclaim="myclaim",pod="nginx-pv-3"}    50
{__type__="gauge",instance="127.0.0.1:9999",job="pvc-test",namespace="default",persistentvolumeclaim="myclaim--2",pod="busybox-xxx"}    25
{__type__="gauge",instance="127.0.0.1:9999",job="pvc-test",namespace="default",persistentvolumeclaim="myclaim--2",pod="busybox-yyy"}    25

参考:
1.group_left/group_right官方doc:
https://prometheus.io/docs/pr...
2.prometheus中各种映射关系的实现:
https://iximiuz.com/en/posts/...


a朋
60 声望38 粉丝