Skip to content

Commit 375c801

Browse files
committed
fix(systemstatsmonitor): declare all labels for disk_percent_used
disk_percent_used records device_name, fs_type, mount_option and state but only declared device_name. With per-metric label validation, Record now rejects the undeclared labels and the metric is dropped from the export (caught by the metriconly e2e). Declare all four labels, matching the sibling disk_bytes_used, and add a regression test.
1 parent ee90453 commit 375c801

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

pkg/systemstatsmonitor/disk_collector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func NewDiskCollectorOrDie(diskConfig *ssmtypes.DiskStatsConfig) *diskCollector
157157
"Disk usage in percentage of total space",
158158
"%",
159159
metrics.LastValue,
160-
[]string{deviceNameLabel})
160+
[]string{deviceNameLabel, fsTypeLabel, mountOptionLabel, stateLabel})
161161
if err != nil {
162162
klog.Fatalf("Error initializing metric for %q: %v", metrics.DiskPercentUsedID, err)
163163
}

pkg/systemstatsmonitor/disk_collector_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,41 @@ package systemstatsmonitor
1919
import (
2020
"testing"
2121

22+
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
23+
2224
ssmtypes "k8s.io/node-problem-detector/pkg/systemstatsmonitor/types"
25+
"k8s.io/node-problem-detector/pkg/util/metrics"
26+
otelutil "k8s.io/node-problem-detector/pkg/util/otel"
2327
)
2428

2529
func TestDiskCollector(t *testing.T) {
2630
dc := NewDiskCollectorOrDie(&ssmtypes.DiskStatsConfig{})
2731
dc.collect()
2832
}
33+
34+
// TestDiskCollectorPercentUsedLabels guards against the declared label set for
35+
// disk_percent_used drifting from the labels collect() records. Every label key
36+
// passed to Record must be in the metric's declared set, otherwise Record
37+
// rejects the call and the metric is silently dropped from the export.
38+
func TestDiskCollectorPercentUsedLabels(t *testing.T) {
39+
otelutil.ResetForTesting()
40+
defer otelutil.ResetForTesting()
41+
otelutil.AddMetricReader(sdkmetric.NewManualReader())
42+
otelutil.InitializeMeterProvider()
43+
44+
dc := NewDiskCollectorOrDie(&ssmtypes.DiskStatsConfig{
45+
MetricsConfigs: map[string]ssmtypes.MetricConfig{
46+
string(metrics.DiskPercentUsedID): {DisplayName: string(metrics.DiskPercentUsedID)},
47+
},
48+
})
49+
50+
// These are the labels collect() records disk_percent_used with.
51+
if err := dc.mPercentUsed.Record(map[string]string{
52+
deviceNameLabel: "sda1",
53+
fsTypeLabel: "ext4",
54+
mountOptionLabel: "rw,relatime",
55+
stateLabel: "used",
56+
}, 42.0); err != nil {
57+
t.Fatalf("disk_percent_used rejected the labels collect() records: %v", err)
58+
}
59+
}

0 commit comments

Comments
 (0)