mirror of
https://github.com/prometheus/node_exporter.git
synced 2025-08-20 18:33:52 -07:00
correcting the logger
Signed-off-by: Shashwat Hiregoudar <shashwat.h@flipkart.com>
This commit is contained in:
parent
dbe4c8c0d3
commit
4ed35903de
|
@ -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(
|
||||
|
|
Loading…
Reference in a new issue