From 4ed35903deea1d411c8a924b5023bcf3823970a0 Mon Sep 17 00:00:00 2001 From: Shashwat Hiregoudar Date: Fri, 9 May 2025 09:59:45 +0000 Subject: [PATCH] correcting the logger Signed-off-by: Shashwat Hiregoudar --- collector/nvme_linux.go | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/collector/nvme_linux.go b/collector/nvme_linux.go index 6be5dafe..5d3c2ac7 100644 --- a/collector/nvme_linux.go +++ b/collector/nvme_linux.go @@ -19,20 +19,21 @@ package collector import ( "errors" "fmt" - "log/slog" "os" "path/filepath" "regexp" "strconv" "strings" + "github.com/go-kit/log" + "github.com/go-kit/log/level" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/procfs/sysfs" ) type nvmeCollector struct { fs sysfs.FS - logger *slog.Logger + logger log.Logger namespaceInfo *prometheus.Desc namespaceCapacityBytes *prometheus.Desc namespaceSizeBytes *prometheus.Desc @@ -46,7 +47,7 @@ func init() { } // NewNVMeCollector returns a new Collector exposing NVMe stats. -func NewNVMeCollector(logger *slog.Logger) (Collector, error) { +func NewNVMeCollector(logger log.Logger) (Collector, error) { fs, err := sysfs.NewFS(*sysPath) if err != nil { return nil, fmt.Errorf("failed to open sysfs: %w", err) @@ -104,7 +105,7 @@ func (c *nvmeCollector) Update(ch chan<- prometheus.Metric) error { devices, err := c.fs.NVMeClass() if err != nil { if errors.Is(err, os.ErrNotExist) { - c.logger.Debug("nvme statistics not found, skipping") + level.Debug(c.logger).Log("msg", "nvme statistics not found, skipping") return ErrNoData } return fmt.Errorf("error obtaining NVMe class info: %w", err) @@ -114,21 +115,21 @@ func (c *nvmeCollector) Update(ch chan<- prometheus.Metric) error { infoValue := 1.0 devicePath := filepath.Join(*sysPath, "class/nvme", device.Name) - cntlid, err := readUintFromFile(filepath.Join(devicePath, "cntlid")) + if cntlid, err := readUintFromFile(filepath.Join(devicePath, "cntlid")) if err != nil { - c.logger.Debug("failed to read cntlid", "device", device.Name, "err", err) + level.Debug(c.logger).Log("msg", "failed to read cntlid", "device", device.Name, "err", err) } ch <- prometheus.MustNewConstMetric(c.info, prometheus.GaugeValue, infoValue, device.Name, device.FirmwareRevision, device.Model, device.Serial, device.State, strconv.FormatUint(cntlid, 10)) - // Find namespace directories. namespacePaths, err := filepath.Glob(filepath.Join(devicePath, "nvme[0-9]*c[0-9]*n[0-9]*")) if err != nil { - c.logger.Error("failed to list NVMe namespaces", "device", device.Name, "err", err) + level.Error(c.logger).Log("msg", "failed to list NVMe namespaces", "device", device.Name, "err", err) continue } re := regexp.MustCompile(`nvme[0-9]+c[0-9]+n([0-9]+)`) for _, namespacePath := range namespacePaths { + // Read namespace data. match := re.FindStringSubmatch(filepath.Base(namespacePath)) if len(match) == 0 { @@ -137,15 +138,15 @@ func (c *nvmeCollector) Update(ch chan<- prometheus.Metric) error { nsid := match[1] nuse, err := readUintFromFile(filepath.Join(namespacePath, "nuse")) if err != nil { - c.logger.Debug("failed to read nuse", "device", device.Name, "namespace", match[0], "err", err) + level.Debug(c.logger).Log("msg", "failed to read nuse", "device", device.Name, "namespace", match[0], "err", err) } nsze, err := readUintFromFile(filepath.Join(namespacePath, "size")) if err != nil { - c.logger.Debug("failed to read size", "device", device.Name, "namespace", match[0], "err", err) + level.Debug(c.logger).Log("msg", "failed to read size", "device", device.Name, "namespace", match[0], "err", err) } lbaSize, err := readUintFromFile(filepath.Join(namespacePath, "queue", "logical_block_size")) if err != nil { - c.logger.Debug("failed to read queue/logical_block_size", "device", device.Name, "namespace", match[0], "err", err) + level.Debug(c.logger).Log("msg", "failed to read queue/logical_block_size", "device", device.Name, "namespace", match[0], "err", err) } ncap := nsze * lbaSize anaState := "unknown" @@ -153,7 +154,7 @@ func (c *nvmeCollector) Update(ch chan<- prometheus.Metric) error { if err == nil { anaState = strings.TrimSpace(string(anaStateSysfs)) } else { - c.logger.Debug("failed to read ana_state", "device", device.Name, "namespace", match[0], "err", err) + level.Debug(c.logger).Log("msg", "failed to read ana_state", "device", device.Name, "namespace", match[0], "err", err) } ch <- prometheus.MustNewConstMetric( @@ -200,4 +201,4 @@ func (c *nvmeCollector) Update(ch chan<- prometheus.Metric) error { } return nil -} +} \ No newline at end of file