kubernetes / kube-state-metrics

Add-on agent to generate and expose cluster-level metrics.
https://kubernetes.io/docs/concepts/cluster-administration/kube-state-metrics/
Apache License 2.0
5.36k stars 2k forks source link

`kube_persistentvolume_*` and `kube_persistentvolumeclaim_*` should expose the volume name under the same label #2288

Open multani opened 9 months ago

multani commented 9 months ago

What would you like to be added:

kube_persistentvolume_* metrics and kube_persistentvolumeclaim_* metrics should expose the "name of the volume" inside the same metric label.

Why is this needed:

It's a bit difficult at the moment to combine the Persistent Volume and Persistent Volume Claim metrics together to get, for instance, the "size" of the PVC in a specific namespace:

It's then difficult to combine both metrics together, and bring the PVC labels into the PV metric (or vice-versa) (see https://github.com/prometheus/prometheus/issues/2204)

Describe the solution you'd like

Rename one of the two label to have the same label as the other metric.

Additional context

I was trying to get the "size" of the PVCs with a query like this, but it doesn't work because it's not possible to join on different labels:

avg(kube_persistentvolume_capacity_bytes) by (persistentvolume)
* on (volumename) group_left (namespace, persistentvolumeclaim)
avg(kube_persistentvolumeclaim_info) by (namespace, persistentvolumeclaim, volumename)
dashpole commented 8 months ago

/assign @CatherineF-dev /traige accepted

multani commented 8 months ago

/traige accepted

@dashpole there's a typo here ;)

Also, I'm happy to help on this feature, if needed :+1:

dashpole commented 8 months ago

/triage accepted

CatherineF-dev commented 8 months ago

You can rename a metric label, as long as this metric is not marked as STABLE.

# HELP kube_persistentvolume_status_phase [STABLE] The phase indicates if a volume is available, bound to a claim,…
46

# HELP kube_persistentvolumeclaim_created Unix creation timestamp

Free feel to open a PR @multani, I can help review

multani commented 8 months ago

I created https://github.com/kubernetes/kube-state-metrics/pull/2303 to fix the issue.

Looking at the code base, it seems the volumeattachment-related metrics may have the same issue.

ringerc commented 1 month ago

In the meantime the workaround - for info metrics - is something like

  group by (namespace,persistentvolumeclaim,persistentvolume) (
    # for some reason kube_persistentvolumeclaim_info uses label "volumename"
    # and persistentvolume_info uses "persistentvolume"
    label_replace(
      kube_persistentvolumeclaim_info,
      "persistentvolume", "$1", "volumename", "^(.*)$"
    )
  )
  * on (persistentvolume)
    group_left(csi_driver,csi_volume_handle,storageclass)
    group by (persistentvolume,csi_driver,csi_volume_handle,storageclass) (
      kube_persistentvolume_info
  )