Skip to content

Commit

Permalink
internal/util: Read(U)intFromFile: return file name in errors
Browse files Browse the repository at this point in the history
This improves error messages by adding the affected file name.
This helps downstream users such as node_exporter to locate the source
of errors which is useful for prometheus/node_exporter#1710.

The original error is wrapped as some consumers perform checks on it.
The consumers within procfs have been updated to Unwrap the error if
necessary.

Signed-off-by: Christian Hoffmann <[email protected]>
  • Loading branch information
hoffie committed Sep 13, 2020
1 parent 9dece15 commit 37cba46
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
17 changes: 13 additions & 4 deletions internal/util/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package util

import (
"fmt"
"io/ioutil"
"strconv"
"strings"
Expand Down Expand Up @@ -68,18 +69,26 @@ func ParsePInt64s(ss []string) ([]*int64, error) {
func ReadUintFromFile(path string) (uint64, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return 0, err
return 0, fmt.Errorf("failed to read file %s: %w", path, err)
}
return strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64)
u, err := strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64)
if err != nil {
return 0, fmt.Errorf("failed to parse uint from file %s: %w", path, err)
}
return u, nil
}

// ReadIntFromFile reads a file and attempts to parse a int64 from it.
func ReadIntFromFile(path string) (int64, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return 0, err
return 0, fmt.Errorf("failed to read file %s: %w", path, err)
}
i, err := strconv.ParseInt(strings.TrimSpace(string(data)), 10, 64)
if err != nil {
return 0, fmt.Errorf("failed to parse int from file %s: %w", path, err)
}
return strconv.ParseInt(strings.TrimSpace(string(data)), 10, 64)
return i, nil
}

// ParseBool parses a string into a boolean pointer.
Expand Down
3 changes: 2 additions & 1 deletion kernel_random.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package procfs

import (
"errors"
"os"

"github.com/prometheus/procfs/internal/util"
Expand Down Expand Up @@ -49,7 +50,7 @@ func (fs FS) KernelRandom() (KernelRandom, error) {
"read_wakeup_threshold": &random.ReadWakeupThreshold,
} {
val, err := util.ReadUintFromFile(fs.proc.Path("sys", "kernel", "random", file))
if os.IsNotExist(err) {
if os.IsNotExist(errors.Unwrap(err)) {
continue
}
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion sysfs/class_thermal.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func parseClassThermalZone(zone string) (ClassThermalZoneStats, error) {

var zonePassive *uint64
passive, err := util.ReadUintFromFile(filepath.Join(zone, "passive"))
if os.IsNotExist(err) || os.IsPermission(err) {
if os.IsNotExist(errors.Unwrap(err)) || os.IsPermission(errors.Unwrap(err)) {
zonePassive = nil
} else if err != nil {
return ClassThermalZoneStats{}, err
Expand Down
3 changes: 2 additions & 1 deletion sysfs/system_cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package sysfs

import (
"errors"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -196,7 +197,7 @@ func parseCpufreqCpuinfo(cpuPath string) (*SystemCPUCpufreqStats, error) {
for i, f := range uintFiles {
v, err := util.ReadUintFromFile(filepath.Join(cpuPath, f))
if err != nil {
if os.IsNotExist(err) || os.IsPermission(err) {
if os.IsNotExist(errors.Unwrap(err)) || os.IsPermission(errors.Unwrap(err)) {
continue
}
return &SystemCPUCpufreqStats{}, err
Expand Down

0 comments on commit 37cba46

Please sign in to comment.