From 11bd515aa5e8549df4f1264da844a56c800afbf0 Mon Sep 17 00:00:00 2001 From: Tiger1218 Date: Mon, 15 Sep 2025 20:54:27 +0800 Subject: [PATCH] fix(collector/pcidevice): Add nil checks to prevent panic Signed-off-by: Tiger1218 --- collector/pcidevice_linux.go | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/collector/pcidevice_linux.go b/collector/pcidevice_linux.go index a7e7f6d308..d43a18cb8b 100644 --- a/collector/pcidevice_linux.go +++ b/collector/pcidevice_linux.go @@ -126,17 +126,26 @@ func (c *pcideviceCollector) Update(ch chan<- prometheus.Metric) error { ch <- c.infoDesc.mustNewConstMetric(1.0, values...) // MaxLinkSpeed and CurrentLinkSpeed are represnted in GT/s - maxLinkSpeedTS := float64(int64(*device.MaxLinkSpeed * 1e9)) - currentLinkSpeedTS := float64(int64(*device.CurrentLinkSpeed * 1e9)) - - for i, val := range []float64{ - maxLinkSpeedTS, - float64(*device.MaxLinkWidth), - currentLinkSpeedTS, - float64(*device.CurrentLinkWidth), - } { - ch <- c.descs[i].mustNewConstMetric(val, device.Location.Strings()...) + var maxLinkSpeedTS, currentLinkSpeedTS float64 + var maxLinkWidth, currentLinkWidth float64 + + if device.MaxLinkSpeed != nil { + maxLinkSpeedTS = float64(int64(*device.MaxLinkSpeed * 1e9)) + } + if device.CurrentLinkSpeed != nil { + currentLinkSpeedTS = float64(int64(*device.CurrentLinkSpeed * 1e9)) + } + if device.MaxLinkWidth != nil { + maxLinkWidth = float64(*device.MaxLinkWidth) } + if device.CurrentLinkWidth != nil { + currentLinkWidth = float64(*device.CurrentLinkWidth) + } + + ch <- c.descs[0].mustNewConstMetric(maxLinkSpeedTS, device.Location.Strings()...) + ch <- c.descs[1].mustNewConstMetric(maxLinkWidth, device.Location.Strings()...) + ch <- c.descs[2].mustNewConstMetric(currentLinkSpeedTS, device.Location.Strings()...) + ch <- c.descs[3].mustNewConstMetric(currentLinkWidth, device.Location.Strings()...) } return nil