Skip to content

Commit

Permalink
feat: create opt-in kube_persistentvolume_csi_attributes metric
Browse files Browse the repository at this point in the history
Signed-off-by: João Vilaça <jvilaca@redhat.com>
  • Loading branch information
machadovilaca committed Oct 24, 2023
1 parent 9facc36 commit 0eee653
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
3 changes: 2 additions & 1 deletion docs/persistentvolume-metrics.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# PersistentVolume Metrics

| Metric name | Metric type | Description | Unit (where applicable) | Labels/tags | Status |
| ---------------------------------------- | ----------- | ------------------------------------------------------------------------------------------------------------------------- | ----------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------ |
|------------------------------------------|-------------|---------------------------------------------------------------------------------------------------------------------------|-------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------|
| kube_persistentvolume_annotations | Gauge | Kubernetes annotations converted to Prometheus labels controlled via [--metric-annotations-allowlist](./cli-arguments.md) | | `persistentvolume`=&lt;persistentvolume-name&gt; <br> `annotation_PERSISTENTVOLUME_ANNOTATION`=&lt;PERSISTENTVOLUME_ANNOTATION&gt; | EXPERIMENTAL |
| kube_persistentvolume_capacity_bytes | Gauge | | | `persistentvolume`=&lt;pv-name&gt; | STABLE |
| kube_persistentvolume_status_phase | Gauge | | | `persistentvolume`=&lt;pv-name&gt; <br>`phase`=&lt;Bound\|Failed\|Pending\|Available\|Released&gt; | STABLE |
Expand All @@ -10,6 +10,7 @@
| kube_persistentvolume_info | Gauge | Information about Persistent Volumes | | `persistentvolume`=&lt;pv-name&gt; <br> `storageclass`=&lt;storageclass-name&gt; <br> `gce_persistent_disk_name`=&lt;pd-name&gt; <br> `host_path`=&lt;path-of-a-host-volume&gt; <br> `host_path_type`=&lt;host-mount-type&gt; <br> `ebs_volume_id`=&lt;ebs-volume-id&gt; <br> `azure_disk_name`=&lt;azure-disk-name&gt; <br> `fc_wwids`=&lt;fc-wwids-comma-separated&gt; <br> `fc_lun`=&lt;fc-lun&gt; <br> `fc_target_wwns`=&lt;fc-target-wwns-comma-separated&gt; <br> `iscsi_target_portal`=&lt;iscsi-target-portal&gt; <br> `iscsi_iqn`=&lt;iscsi-iqn&gt; <br> `iscsi_lun`=&lt;iscsi-lun&gt; <br> `iscsi_initiator_name`=&lt;iscsi-initiator-name&gt; <br> `local_path`=&lt;path-of-a-local-volume&gt; <br> `local_fs`=&lt;local-volume-fs-type&gt; <br> `nfs_server`=&lt;nfs-server&gt; <br> `nfs_path`=&lt;nfs-path&gt; <br> `csi_driver`=&lt;csi-driver&gt; <br> `csi_volume_handle`=&lt;csi-volume-handle&gt; | STABLE |
| kube_persistentvolume_created | Gauge | Unix creation timestamp | seconds | `persistentvolume`=&lt;persistentvolume-name&gt; <br> | EXPERIMENTAL |
| kube_persistentvolume_deletion_timestamp | Gauge | Unix deletion timestamp | seconds | `persistentvolume`=&lt;persistentvolume-name&gt; <br> | EXPERIMENTAL |
| kube_persistentvolume_csi_attributes | Gauge | CSI attributes of the Persistent Volume. | | `persistentvolume`=&lt;persistentvolume-name&gt; <br> `csi_mounter`=&lt;csi-mounter&gt; <br> `csi_map_options`=&lt;csi-map-options&gt; | EXPERIMENTAL |

## Useful metrics queries

Expand Down
42 changes: 42 additions & 0 deletions internal/store/persistentvolume.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ var (
descPersistentVolumeLabelsName = "kube_persistentvolume_labels"
descPersistentVolumeLabelsHelp = "Kubernetes labels converted to Prometheus labels."
descPersistentVolumeLabelsDefaultLabels = []string{"persistentvolume"}

descPersistentVolumeCSIAttributesName = "kube_persistentvolume_csi_attributes"
descPersistentVolumeCSIAttributesHelp = "CSI attributes of the Persistent Volume."
)

func persistentVolumeMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generator.FamilyGenerator {
Expand Down Expand Up @@ -349,6 +352,45 @@ func persistentVolumeMetricFamilies(allowAnnotationsList, allowLabelsList []stri
}
}),
),
*generator.NewOptInFamilyGenerator(
descPersistentVolumeCSIAttributesName,
descPersistentVolumeCSIAttributesHelp,
metric.Gauge,
basemetrics.ALPHA,
"",
wrapPersistentVolumeFunc(func(p *v1.PersistentVolume) *metric.Family {
if p.Spec.CSI == nil {
return &metric.Family{
Metrics: []*metric.Metric{},
}
}

var csiMounter, csiMapOptions string
for k, v := range p.Spec.PersistentVolumeSource.CSI.VolumeAttributes {
if k == "mapOptions" {
csiMapOptions = v
} else if k == "mounter" {
csiMounter = v
}
}

return &metric.Family{
Metrics: []*metric.Metric{
{
LabelKeys: []string{
"csi_mounter",
"csi_map_options",
},
LabelValues: []string{
csiMounter,
csiMapOptions,
},
Value: 1,
},
},
}
}),
),
}
}

Expand Down
50 changes: 50 additions & 0 deletions internal/store/persistentvolume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,10 @@ func TestPersistentVolumeStore(t *testing.T) {
CSI: &v1.CSIPersistentVolumeSource{
Driver: "test-driver",
VolumeHandle: "test-volume-handle",
VolumeAttributes: map[string]string{
"mounter": "rbd",
"mapOptions": "krbd:rxbounce",
},
},
},
},
Expand Down Expand Up @@ -697,6 +701,52 @@ func TestPersistentVolumeStore(t *testing.T) {
`,
MetricNames: []string{"kube_persistentvolume_deletion_timestamp"},
},
{
Obj: &v1.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{
Name: "test-pv-available",
},
Spec: v1.PersistentVolumeSpec{
PersistentVolumeSource: v1.PersistentVolumeSource{
CSI: &v1.CSIPersistentVolumeSource{
Driver: "test-driver",
VolumeHandle: "test-volume-handle",
},
},
},
},
Want: `
# HELP kube_persistentvolume_csi_attributes CSI attributes of the Persistent Volume.
# TYPE kube_persistentvolume_csi_attributes gauge
kube_persistentvolume_csi_attributes{persistentvolume="test-pv-available",csi_mounter="",csi_map_options=""} 1
`,
MetricNames: []string{"kube_persistentvolume_csi_attributes"},
},
{
Obj: &v1.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{
Name: "test-pv-available",
},
Spec: v1.PersistentVolumeSpec{
PersistentVolumeSource: v1.PersistentVolumeSource{
CSI: &v1.CSIPersistentVolumeSource{
Driver: "test-driver",
VolumeHandle: "test-volume-handle",
VolumeAttributes: map[string]string{
"mounter": "rbd",
"mapOptions": "krbd:rxbounce",
},
},
},
},
},
Want: `
# HELP kube_persistentvolume_csi_attributes CSI attributes of the Persistent Volume.
# TYPE kube_persistentvolume_csi_attributes gauge
kube_persistentvolume_csi_attributes{persistentvolume="test-pv-available",csi_mounter="rbd",csi_map_options="krbd:rxbounce"} 1
`,
MetricNames: []string{"kube_persistentvolume_csi_attributes"},
},
}
for i, c := range cases {
c.Func = generator.ComposeMetricGenFuncs(persistentVolumeMetricFamilies(c.AllowAnnotationsList, c.AllowLabelsList))
Expand Down

0 comments on commit 0eee653

Please sign in to comment.