Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: replace pkg/errors with Go 1.13 native errors
Browse files Browse the repository at this point in the history
Add support for error wrapping and switch to modern
Go 1.13 errors.
kruskall committed Jul 28, 2022
1 parent 9aede31 commit 1068c21
Showing 35 changed files with 135 additions and 153 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -5,7 +5,6 @@ go 1.17
require (
github.com/elastic/go-windows v1.0.0
github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901
github.com/pkg/errors v0.8.1
github.com/prometheus/procfs v0.0.0-20190425082905-87a4384529e0
github.com/stretchr/testify v1.3.0
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a
@@ -14,5 +13,6 @@ require (

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pkg/errors v0.8.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
)
6 changes: 3 additions & 3 deletions internal/registry/registry.go
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@
package registry

import (
"github.com/pkg/errors"
"fmt"

"github.com/elastic/go-sysinfo/types"
)
@@ -41,14 +41,14 @@ type ProcessProvider interface {
func Register(provider interface{}) {
if h, ok := provider.(HostProvider); ok {
if hostProvider != nil {
panic(errors.Errorf("HostProvider already registered: %v", hostProvider))
panic(fmt.Sprintf("HostProvider already registered: %v", hostProvider))
}
hostProvider = h
}

if p, ok := provider.(ProcessProvider); ok {
if processProvider != nil {
panic(errors.Errorf("ProcessProvider already registered: %v", processProvider))
panic(fmt.Sprintf("ProcessProvider already registered: %v", processProvider))
}
processProvider = p
}
7 changes: 3 additions & 4 deletions providers/aix/boottime_aix_ppc64.go
Original file line number Diff line number Diff line change
@@ -22,10 +22,9 @@ package aix

import (
"encoding/binary"
"fmt"
"os"
"time"

"github.com/pkg/errors"
)

// utmp can't be used by "encoding/binary" if generated by cgo,
@@ -60,7 +59,7 @@ func bootTime(filename string) (time.Time, error) {
// Get boot time from /etc/utmp
file, err := os.Open(filename)
if err != nil {
return time.Time{}, errors.Wrap(err, "failed to get host uptime: cannot open /etc/utmp")
return time.Time{}, fmt.Errorf("failed to get host uptime: cannot open /etc/utmp: %w", err)
}

defer file.Close()
@@ -76,5 +75,5 @@ func bootTime(filename string) (time.Time, error) {
}
}

return time.Time{}, errors.Wrap(err, "failed to get host uptime: no utmp record")
return time.Time{}, fmt.Errorf("failed to get host uptime: no utmp record: %w", err)
}
9 changes: 5 additions & 4 deletions providers/aix/host_aix_ppc64.go
Original file line number Diff line number Diff line change
@@ -31,11 +31,12 @@ package aix
import "C"

import (
"errors"
"fmt"
"os"
"time"

"github.com/joeshaw/multierror"
"github.com/pkg/errors"

"github.com/elastic/go-sysinfo/internal/registry"
"github.com/elastic/go-sysinfo/providers/shared"
@@ -80,7 +81,7 @@ func (*host) CPUTime() (types.CPUTimes, error) {
cpudata := C.perfstat_cpu_total_t{}

if _, err := C.perfstat_cpu_total(nil, &cpudata, C.sizeof_perfstat_cpu_total_t, 1); err != nil {
return types.CPUTimes{}, errors.Wrap(err, "error while callin perfstat_cpu_total")
return types.CPUTimes{}, fmt.Errorf("error while callin perfstat_cpu_total: %w", err)
}

return types.CPUTimes{
@@ -100,7 +101,7 @@ func (*host) Memory() (*types.HostMemoryInfo, error) {
meminfo := C.perfstat_memory_total_t{}
_, err := C.perfstat_memory_total(nil, &meminfo, C.sizeof_perfstat_memory_total_t, 1)
if err != nil {
return nil, errors.Wrap(err, "perfstat_memory_total failed")
return nil, fmt.Errorf("perfstat_memory_total failed: %w", err)
}

mem.Total = uint64(meminfo.real_total) * pagesize
@@ -137,7 +138,7 @@ type reader struct {

func (r *reader) addErr(err error) bool {
if err != nil {
if errors.Cause(err) != types.ErrNotImplemented {
if !errors.Is(err, types.ErrNotImplemented) {
r.errs = append(r.errs, err)
}
return true
9 changes: 4 additions & 5 deletions providers/aix/kernel_aix_ppc64.go
Original file line number Diff line number Diff line change
@@ -26,27 +26,26 @@ package aix
import "C"

import (
"fmt"
"strconv"

"github.com/pkg/errors"
)

var oslevel string

func getKernelVersion() (int, int, error) {
name := C.struct_utsname{}
if _, err := C.uname(&name); err != nil {
return 0, 0, errors.Wrap(err, "kernel version: uname")
return 0, 0, fmt.Errorf("kernel version: uname: %w", err)
}

version, err := strconv.Atoi(C.GoString(&name.version[0]))
if err != nil {
return 0, 0, errors.Wrap(err, "parsing kernel version")
return 0, 0, fmt.Errorf("parsing kernel version: %w", err)
}

release, err := strconv.Atoi(C.GoString(&name.release[0]))
if err != nil {
return 0, 0, errors.Wrap(err, "parsing kernel release")
return 0, 0, fmt.Errorf("parsing kernel release: %w", err)
}
return version, release, nil
}
6 changes: 2 additions & 4 deletions providers/aix/machineid_aix_ppc64.go
Original file line number Diff line number Diff line change
@@ -25,15 +25,13 @@ package aix
*/
import "C"

import (
"github.com/pkg/errors"
)
import "fmt"

// MachineID returns the id of the machine
func MachineID() (string, error) {
name := C.struct_utsname{}
if _, err := C.uname(&name); err != nil {
return "", errors.Wrap(err, "machine id")
return "", fmt.Errorf("machine id: %w", err)
}
return C.GoString(&name.machine[0]), nil
}
5 changes: 2 additions & 3 deletions providers/aix/os_aix_ppc64.go
Original file line number Diff line number Diff line change
@@ -21,12 +21,11 @@
package aix

import (
"fmt"
"io/ioutil"
"strconv"
"strings"

"github.com/pkg/errors"

"github.com/elastic/go-sysinfo/types"
)

@@ -44,7 +43,7 @@ func getOSInfo() (*types.OSInfo, error) {
// Retrieve build version from "/proc/version".
procVersion, err := ioutil.ReadFile("/proc/version")
if err != nil {
return nil, errors.Wrap(err, "failed to get OS info: cannot open /proc/version")
return nil, fmt.Errorf("failed to get OS info: cannot open /proc/version: %w", err)
}
build := strings.SplitN(string(procVersion), "\n", 4)[2]

25 changes: 12 additions & 13 deletions providers/aix/process_aix_ppc64.go
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@ import "C"
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"io/ioutil"
"os"
@@ -43,8 +44,6 @@ import (
"time"
"unsafe"

"github.com/pkg/errors"

"github.com/elastic/go-sysinfo/types"
)

@@ -54,7 +53,7 @@ func (aixSystem) Processes() ([]types.Process, error) {
// getprocs which will also retrieve kernel threads.
files, err := ioutil.ReadDir("/proc")
if err != nil {
return nil, errors.Wrap(err, "error while reading /proc")
return nil, fmt.Errorf("error while reading /proc: %w", err)
}

processes := make([]types.Process, 0, len(files))
@@ -121,7 +120,7 @@ func (p *process) Info() (types.ProcessInfo, error) {
err = syscall.ESRCH
}
if err != nil {
return types.ProcessInfo{}, errors.Wrap(err, "error while calling getprocs")
return types.ProcessInfo{}, fmt.Errorf("error while calling getprocs: %w", err)
}

p.info.PPID = int(info.pi_ppid)
@@ -133,7 +132,7 @@ func (p *process) Info() (types.ProcessInfo, error) {
buf := make([]byte, 8192)
var args []string
if _, err := C.getargs(unsafe.Pointer(&info), C.sizeof_struct_procsinfo64, (*C.char)(&buf[0]), 8192); err != nil {
return types.ProcessInfo{}, errors.Wrap(err, "error while calling getargs")
return types.ProcessInfo{}, fmt.Errorf("error while calling getargs: %w", err)
}

bbuf := bytes.NewBuffer(buf)
@@ -143,7 +142,7 @@ func (p *process) Info() (types.ProcessInfo, error) {
break
}
if err != nil {
return types.ProcessInfo{}, errors.Wrap(err, "error while reading arguments")
return types.ProcessInfo{}, fmt.Errorf("error while reading arguments: %w", err)
}

args = append(args, string(chop(arg)))
@@ -183,7 +182,7 @@ func (p *process) Info() (types.ProcessInfo, error) {
cwd, err := os.Readlink("/proc/" + strconv.Itoa(p.pid) + "/cwd")
if err != nil {
if !os.IsNotExist(err) {
return types.ProcessInfo{}, errors.Wrapf(err, "error while reading /proc/%s/cwd", strconv.Itoa(p.pid))
return types.ProcessInfo{}, fmt.Errorf("error while reading /proc/%s/cwd: %w", strconv.Itoa(p.pid), err)
}
}

@@ -205,7 +204,7 @@ func (p *process) Environment() (map[string]string, error) {
info.pi_pid = C.pid_t(p.pid)

if _, err := C.getevars(unsafe.Pointer(&info), C.sizeof_struct_procsinfo64, (*C.char)(&buf[0]), 8192); err != nil {
return nil, errors.Wrap(err, "error while calling getevars")
return nil, fmt.Errorf("error while calling getevars: %w", err)
}

bbuf := bytes.NewBuffer(buf)
@@ -218,12 +217,12 @@ func (p *process) Environment() (map[string]string, error) {
break
}
if err != nil {
return nil, errors.Wrap(err, "error while calling getevars")
return nil, fmt.Errorf("error while calling getevars: %w", err)
}

pair := bytes.SplitN(chop(line), delim, 2)
if len(pair) != 2 {
return nil, errors.Wrap(err, "error reading process environment")
return nil, fmt.Errorf("error reading process environment: %w", err)
}
p.env[string(pair[0])] = string(pair[1])
}
@@ -260,7 +259,7 @@ func (p *process) Memory() (types.MemoryInfo, error) {
err = syscall.ESRCH
}
if err != nil {
return types.MemoryInfo{}, errors.Wrap(err, "error while calling getprocs")
return types.MemoryInfo{}, fmt.Errorf("error while calling getprocs: %w", err)
}

mem.Resident = uint64(info.pi_drss+info.pi_trss) * pagesize
@@ -286,12 +285,12 @@ func (p *process) decodeProcfsFile(name string, data interface{}) error {

file, err := os.Open(fileName)
if err != nil {
return errors.Wrapf(err, "error while opening %s", fileName)
return fmt.Errorf("error while opening %s: %w", fileName, err)
}
defer file.Close()

if err := binary.Read(file, binary.BigEndian, data); err != nil {
return errors.Wrapf(err, "error while decoding %s", fileName)
return fmt.Errorf("error while decoding %s: %w", fileName, err)
}

return nil
5 changes: 2 additions & 3 deletions providers/darwin/arch_darwin.go
Original file line number Diff line number Diff line change
@@ -21,17 +21,16 @@
package darwin

import (
"fmt"
"syscall"

"github.com/pkg/errors"
)

const hardwareMIB = "hw.machine"

func Architecture() (string, error) {
arch, err := syscall.Sysctl(hardwareMIB)
if err != nil {
return "", errors.Wrap(err, "failed to get architecture")
return "", fmt.Errorf("failed to get architecture: %w", err)
}

return arch, nil
5 changes: 2 additions & 3 deletions providers/darwin/boottime_darwin.go
Original file line number Diff line number Diff line change
@@ -21,18 +21,17 @@
package darwin

import (
"fmt"
"syscall"
"time"

"github.com/pkg/errors"
)

const kernBoottimeMIB = "kern.boottime"

func BootTime() (time.Time, error) {
var tv syscall.Timeval
if err := sysctlByName(kernBoottimeMIB, &tv); err != nil {
return time.Time{}, errors.Wrap(err, "failed to get host uptime")
return time.Time{}, fmt.Errorf("failed to get host uptime: %w", err)
}

bootTime := time.Unix(int64(tv.Sec), int64(tv.Usec)*int64(time.Microsecond))
15 changes: 8 additions & 7 deletions providers/darwin/host_darwin.go
Original file line number Diff line number Diff line change
@@ -21,11 +21,12 @@
package darwin

import (
"errors"
"fmt"
"os"
"time"

"github.com/joeshaw/multierror"
"github.com/pkg/errors"

"github.com/elastic/go-sysinfo/internal/registry"
"github.com/elastic/go-sysinfo/providers/shared"
@@ -53,7 +54,7 @@ func (h *host) Info() types.HostInfo {
func (h *host) CPUTime() (types.CPUTimes, error) {
cpu, err := getHostCPULoadInfo()
if err != nil {
return types.CPUTimes{}, errors.Wrap(err, "failed to get host CPU usage")
return types.CPUTimes{}, fmt.Errorf("failed to get host CPU usage: %w", err)
}

ticksPerSecond := time.Duration(getClockTicks())
@@ -71,25 +72,25 @@ func (h *host) Memory() (*types.HostMemoryInfo, error) {

// Total physical memory.
if err := sysctlByName("hw.memsize", &mem.Total); err != nil {
return nil, errors.Wrap(err, "failed to get total physical memory")
return nil, fmt.Errorf("failed to get total physical memory: %w", err)
}

// Page size for computing byte totals.
pageSizeBytes, err := getPageSize()
if err != nil {
return nil, errors.Wrap(err, "failed to get page size")
return nil, fmt.Errorf("failed to get page size: %w", err)
}

// Virtual Memory Statistics
vmStat, err := getHostVMInfo64()
if err != nil {
return nil, errors.Wrap(err, "failed to get virtual memory statistics")
return nil, fmt.Errorf("failed to get virtual memory statistics: %w", err)
}

// Swap
swap, err := getSwapUsage()
if err != nil {
return nil, errors.Wrap(err, "failed to get swap usage")
return nil, fmt.Errorf("failed to get swap usage: %w", err)
}

inactiveBytes := uint64(vmStat.Inactive_count) * pageSizeBytes
@@ -150,7 +151,7 @@ type reader struct {

func (r *reader) addErr(err error) bool {
if err != nil {
if errors.Cause(err) != types.ErrNotImplemented {
if !errors.Is(err, types.ErrNotImplemented) {
r.errs = append(r.errs, err)
}
return true
Loading

0 comments on commit 1068c21

Please sign in to comment.