Skip to content

Commit c971903

Browse files
authored
Refactor typed metric descriptors (#3448)
* Remove second implementation of typedDesc. * Refactor pcidevice Desc vars to be typedDesc. Signed-off-by: Ben Kochie <[email protected]>
1 parent c16962b commit c971903

File tree

2 files changed

+110
-108
lines changed

2 files changed

+110
-108
lines changed

collector/diskstats_linux.go

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,16 @@ const (
6666
udevSCSIIdentSerial = "SCSI_IDENT_SERIAL"
6767
)
6868

69-
type typedFactorDesc struct {
70-
desc *prometheus.Desc
71-
valueType prometheus.ValueType
72-
}
73-
7469
type udevInfo map[string]string
7570

76-
func (d *typedFactorDesc) mustNewConstMetric(value float64, labels ...string) prometheus.Metric {
77-
return prometheus.MustNewConstMetric(d.desc, d.valueType, value, labels...)
78-
}
79-
8071
type diskstatsCollector struct {
8172
deviceFilter deviceFilter
8273
fs blockdevice.FS
83-
infoDesc typedFactorDesc
84-
descs []typedFactorDesc
85-
filesystemInfoDesc typedFactorDesc
86-
deviceMapperInfoDesc typedFactorDesc
87-
ataDescs map[string]typedFactorDesc
74+
infoDesc typedDesc
75+
descs []typedDesc
76+
filesystemInfoDesc typedDesc
77+
deviceMapperInfoDesc typedDesc
78+
ataDescs map[string]typedDesc
8879
logger *slog.Logger
8980
getUdevDeviceProperties func(uint32, uint32) (udevInfo, error)
9081
}
@@ -110,14 +101,14 @@ func NewDiskstatsCollector(logger *slog.Logger) (Collector, error) {
110101
collector := diskstatsCollector{
111102
deviceFilter: deviceFilter,
112103
fs: fs,
113-
infoDesc: typedFactorDesc{
104+
infoDesc: typedDesc{
114105
desc: prometheus.NewDesc(prometheus.BuildFQName(namespace, diskSubsystem, "info"),
115106
"Info of /sys/block/<block_device>.",
116107
[]string{"device", "major", "minor", "path", "wwn", "model", "serial", "revision", "rotational"},
117108
nil,
118109
), valueType: prometheus.GaugeValue,
119110
},
120-
descs: []typedFactorDesc{
111+
descs: []typedDesc{
121112
{
122113
desc: readsCompletedDesc, valueType: prometheus.CounterValue,
123114
},
@@ -220,21 +211,21 @@ func NewDiskstatsCollector(logger *slog.Logger) (Collector, error) {
220211
), valueType: prometheus.CounterValue,
221212
},
222213
},
223-
filesystemInfoDesc: typedFactorDesc{
214+
filesystemInfoDesc: typedDesc{
224215
desc: prometheus.NewDesc(prometheus.BuildFQName(namespace, diskSubsystem, "filesystem_info"),
225216
"Info about disk filesystem.",
226217
[]string{"device", "type", "usage", "uuid", "version"},
227218
nil,
228219
), valueType: prometheus.GaugeValue,
229220
},
230-
deviceMapperInfoDesc: typedFactorDesc{
221+
deviceMapperInfoDesc: typedDesc{
231222
desc: prometheus.NewDesc(prometheus.BuildFQName(namespace, diskSubsystem, "device_mapper_info"),
232223
"Info about disk device mapper.",
233224
[]string{"device", "name", "uuid", "vg_name", "lv_name", "lv_layer"},
234225
nil,
235226
), valueType: prometheus.GaugeValue,
236227
},
237-
ataDescs: map[string]typedFactorDesc{
228+
ataDescs: map[string]typedDesc{
238229
udevIDATAWriteCache: {
239230
desc: prometheus.NewDesc(prometheus.BuildFQName(namespace, diskSubsystem, "ata_write_cache"),
240231
"ATA disk has a write cache.",

collector/pcidevice_linux.go

Lines changed: 100 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -43,75 +43,108 @@ var (
4343

4444
pcideviceLabelNames = []string{"segment", "bus", "device", "function"}
4545

46-
pcideviceMaxLinkTSDesc = prometheus.NewDesc(
47-
prometheus.BuildFQName(namespace, pcideviceSubsystem, "max_link_transfers_per_second"),
48-
"Value of maximum link's transfers per second (T/s)",
49-
pcideviceLabelNames, nil,
50-
)
51-
pcideviceMaxLinkWidthDesc = prometheus.NewDesc(
52-
prometheus.BuildFQName(namespace, pcideviceSubsystem, "max_link_width"),
53-
"Value of maximum link's width (number of lanes)",
54-
pcideviceLabelNames, nil,
55-
)
46+
pcideviceMaxLinkTSDesc = typedDesc{
47+
desc: prometheus.NewDesc(
48+
prometheus.BuildFQName(namespace, pcideviceSubsystem, "max_link_transfers_per_second"),
49+
"Value of maximum link's transfers per second (T/s)",
50+
pcideviceLabelNames, nil,
51+
),
52+
valueType: prometheus.GaugeValue,
53+
}
5654

57-
pcideviceCurrentLinkTSDesc = prometheus.NewDesc(
58-
prometheus.BuildFQName(namespace, pcideviceSubsystem, "current_link_transfers_per_second"),
59-
"Value of current link's transfers per second (T/s)",
60-
pcideviceLabelNames, nil,
61-
)
62-
pcideviceCurrentLinkWidthDesc = prometheus.NewDesc(
63-
prometheus.BuildFQName(namespace, pcideviceSubsystem, "current_link_width"),
64-
"Value of current link's width (number of lanes)",
65-
pcideviceLabelNames, nil,
66-
)
55+
pcideviceMaxLinkWidthDesc = typedDesc{
56+
desc: prometheus.NewDesc(
57+
prometheus.BuildFQName(namespace, pcideviceSubsystem, "max_link_width"),
58+
"Value of maximum link's width (number of lanes)",
59+
pcideviceLabelNames, nil,
60+
),
61+
valueType: prometheus.GaugeValue,
62+
}
6763

68-
pcidevicePowerStateDesc = prometheus.NewDesc(
69-
prometheus.BuildFQName(namespace, pcideviceSubsystem, "power_state"),
70-
"PCIe device power state, one of: D0, D1, D2, D3hot, D3cold, unknown or error.",
71-
append(pcideviceLabelNames, "state"), nil,
72-
)
64+
pcideviceCurrentLinkTSDesc = typedDesc{
65+
desc: prometheus.NewDesc(
66+
prometheus.BuildFQName(namespace, pcideviceSubsystem, "current_link_transfers_per_second"),
67+
"Value of current link's transfers per second (T/s)",
68+
pcideviceLabelNames, nil,
69+
),
70+
valueType: prometheus.GaugeValue,
71+
}
72+
pcideviceCurrentLinkWidthDesc = typedDesc{
73+
desc: prometheus.NewDesc(
74+
prometheus.BuildFQName(namespace, pcideviceSubsystem, "current_link_width"),
75+
"Value of current link's width (number of lanes)",
76+
pcideviceLabelNames, nil,
77+
),
78+
valueType: prometheus.GaugeValue,
79+
}
7380

74-
pcideviceD3coldAllowedDesc = prometheus.NewDesc(
75-
prometheus.BuildFQName(namespace, pcideviceSubsystem, "d3cold_allowed"),
76-
"Whether the PCIe device supports D3cold power state (0/1).",
77-
pcideviceLabelNames, nil,
78-
)
81+
pcidevicePowerStateDesc = typedDesc{
82+
desc: prometheus.NewDesc(
83+
prometheus.BuildFQName(namespace, pcideviceSubsystem, "power_state"),
84+
"PCIe device power state, one of: D0, D1, D2, D3hot, D3cold, unknown or error.",
85+
append(pcideviceLabelNames, "state"), nil,
86+
),
87+
valueType: prometheus.GaugeValue,
88+
}
7989

80-
pcideviceSriovDriversAutoprobeDesc = prometheus.NewDesc(
81-
prometheus.BuildFQName(namespace, pcideviceSubsystem, "sriov_drivers_autoprobe"),
82-
"Whether SR-IOV drivers autoprobe is enabled for the device (0/1).",
83-
pcideviceLabelNames, nil,
84-
)
90+
pcideviceD3coldAllowedDesc = typedDesc{
91+
desc: prometheus.NewDesc(
92+
prometheus.BuildFQName(namespace, pcideviceSubsystem, "d3cold_allowed"),
93+
"Whether the PCIe device supports D3cold power state (0/1).",
94+
pcideviceLabelNames, nil,
95+
),
96+
valueType: prometheus.GaugeValue,
97+
}
8598

86-
pcideviceSriovNumvfsDesc = prometheus.NewDesc(
87-
prometheus.BuildFQName(namespace, pcideviceSubsystem, "sriov_numvfs"),
88-
"Number of Virtual Functions (VFs) currently enabled for SR-IOV.",
89-
pcideviceLabelNames, nil,
90-
)
99+
pcideviceSriovDriversAutoprobeDesc = typedDesc{
100+
desc: prometheus.NewDesc(
101+
prometheus.BuildFQName(namespace, pcideviceSubsystem, "sriov_drivers_autoprobe"),
102+
"Whether SR-IOV drivers autoprobe is enabled for the device (0/1).",
103+
pcideviceLabelNames, nil,
104+
),
105+
valueType: prometheus.GaugeValue,
106+
}
91107

92-
pcideviceSriovTotalvfsDesc = prometheus.NewDesc(
93-
prometheus.BuildFQName(namespace, pcideviceSubsystem, "sriov_totalvfs"),
94-
"Total number of Virtual Functions (VFs) supported by the device.",
95-
pcideviceLabelNames, nil,
96-
)
108+
pcideviceSriovNumvfsDesc = typedDesc{
109+
desc: prometheus.NewDesc(
110+
prometheus.BuildFQName(namespace, pcideviceSubsystem, "sriov_numvfs"),
111+
"Number of Virtual Functions (VFs) currently enabled for SR-IOV.",
112+
pcideviceLabelNames, nil,
113+
),
114+
valueType: prometheus.GaugeValue,
115+
}
97116

98-
pcideviceSriovVfTotalMsixDesc = prometheus.NewDesc(
99-
prometheus.BuildFQName(namespace, pcideviceSubsystem, "sriov_vf_total_msix"),
100-
"Total number of MSI-X vectors for Virtual Functions.",
101-
pcideviceLabelNames, nil,
102-
)
117+
pcideviceSriovTotalvfsDesc = typedDesc{
118+
desc: prometheus.NewDesc(
119+
prometheus.BuildFQName(namespace, pcideviceSubsystem, "sriov_totalvfs"),
120+
"Total number of Virtual Functions (VFs) supported by the device.",
121+
pcideviceLabelNames, nil,
122+
),
123+
valueType: prometheus.GaugeValue,
124+
}
103125

104-
pcideviceNumaNodeDesc = prometheus.NewDesc(
105-
prometheus.BuildFQName(namespace, pcideviceSubsystem, "numa_node"),
106-
"NUMA node number for the PCI device. -1 indicates unknown or not available.",
107-
pcideviceLabelNames, nil,
108-
)
126+
pcideviceSriovVfTotalMsixDesc = typedDesc{
127+
desc: prometheus.NewDesc(
128+
prometheus.BuildFQName(namespace, pcideviceSubsystem, "sriov_vf_total_msix"),
129+
"Total number of MSI-X vectors for Virtual Functions.",
130+
pcideviceLabelNames, nil,
131+
),
132+
valueType: prometheus.GaugeValue,
133+
}
134+
135+
pcideviceNumaNodeDesc = typedDesc{
136+
desc: prometheus.NewDesc(
137+
prometheus.BuildFQName(namespace, pcideviceSubsystem, "numa_node"),
138+
"NUMA node number for the PCI device. -1 indicates unknown or not available.",
139+
pcideviceLabelNames, nil,
140+
),
141+
valueType: prometheus.GaugeValue,
142+
}
109143
)
110144

111145
type pcideviceCollector struct {
112146
fs sysfs.FS
113147
infoDesc typedDesc
114-
descs []typedFactorDesc
115148
logger *slog.Logger
116149
pciVendors map[string]string
117150
pciDevices map[string]map[string]string
@@ -161,20 +194,6 @@ func NewPcideviceCollector(logger *slog.Logger) (Collector, error) {
161194
valueType: prometheus.GaugeValue,
162195
}
163196

164-
c.descs = []typedFactorDesc{
165-
{desc: pcideviceMaxLinkTSDesc, valueType: prometheus.GaugeValue},
166-
{desc: pcideviceMaxLinkWidthDesc, valueType: prometheus.GaugeValue},
167-
{desc: pcideviceCurrentLinkTSDesc, valueType: prometheus.GaugeValue},
168-
{desc: pcideviceCurrentLinkWidthDesc, valueType: prometheus.GaugeValue},
169-
{desc: pcidevicePowerStateDesc, valueType: prometheus.GaugeValue},
170-
{desc: pcideviceD3coldAllowedDesc, valueType: prometheus.GaugeValue},
171-
{desc: pcideviceSriovDriversAutoprobeDesc, valueType: prometheus.GaugeValue},
172-
{desc: pcideviceSriovNumvfsDesc, valueType: prometheus.GaugeValue},
173-
{desc: pcideviceSriovTotalvfsDesc, valueType: prometheus.GaugeValue},
174-
{desc: pcideviceSriovVfTotalMsixDesc, valueType: prometheus.GaugeValue},
175-
{desc: pcideviceNumaNodeDesc, valueType: prometheus.GaugeValue},
176-
}
177-
178197
return c, nil
179198
}
180199

@@ -300,23 +319,15 @@ func (c *pcideviceCollector) Update(ch chan<- prometheus.Metric) error {
300319
}
301320

302321
// Emit metrics for all fields except numa_node and power_state
303-
metrics := []float64{
304-
maxLinkSpeedTS,
305-
maxLinkWidth,
306-
currentLinkSpeedTS,
307-
currentLinkWidth,
308-
d3coldAllowed,
309-
sriovDriversAutoprobe,
310-
sriovNumvfs,
311-
sriovTotalvfs,
312-
sriovVfTotalMsix,
313-
}
314-
315-
// Emit regular metrics (excluding power_state which is at index 4)
316-
metricIndices := []int{0, 1, 2, 3, 5, 6, 7, 8, 9} // Skip power_state (4) and numa_node (10)
317-
for i, val := range metrics {
318-
ch <- c.descs[metricIndices[i]].mustNewConstMetric(val, device.Location.Strings()...)
319-
}
322+
ch <- pcideviceMaxLinkTSDesc.mustNewConstMetric(maxLinkSpeedTS, device.Location.Strings()...)
323+
ch <- pcideviceMaxLinkWidthDesc.mustNewConstMetric(maxLinkWidth, device.Location.Strings()...)
324+
ch <- pcideviceCurrentLinkTSDesc.mustNewConstMetric(currentLinkSpeedTS, device.Location.Strings()...)
325+
ch <- pcideviceCurrentLinkWidthDesc.mustNewConstMetric(currentLinkWidth, device.Location.Strings()...)
326+
ch <- pcideviceD3coldAllowedDesc.mustNewConstMetric(d3coldAllowed, device.Location.Strings()...)
327+
ch <- pcideviceSriovDriversAutoprobeDesc.mustNewConstMetric(sriovDriversAutoprobe, device.Location.Strings()...)
328+
ch <- pcideviceSriovNumvfsDesc.mustNewConstMetric(sriovNumvfs, device.Location.Strings()...)
329+
ch <- pcideviceSriovTotalvfsDesc.mustNewConstMetric(sriovTotalvfs, device.Location.Strings()...)
330+
ch <- pcideviceSriovVfTotalMsixDesc.mustNewConstMetric(sriovVfTotalMsix, device.Location.Strings()...)
320331

321332
// Emit power state metrics with state labels only if power state is available
322333
if hasPowerState {
@@ -330,13 +341,13 @@ func (c *pcideviceCollector) Update(ch chan<- prometheus.Metric) error {
330341
value = 0
331342
}
332343
stateLabels := append(deviceLabels, state)
333-
ch <- c.descs[4].mustNewConstMetric(value, stateLabels...)
344+
ch <- pcidevicePowerStateDesc.mustNewConstMetric(value, stateLabels...)
334345
}
335346
}
336347

337348
// Only emit numa_node metric if the value is available (not -1)
338349
if numaNode != -1 {
339-
ch <- c.descs[10].mustNewConstMetric(numaNode, device.Location.Strings()...)
350+
ch <- pcideviceNumaNodeDesc.mustNewConstMetric(numaNode, device.Location.Strings()...)
340351
}
341352
}
342353

0 commit comments

Comments
 (0)