Skip to content

Commit

Permalink
Use stdlib errors
Browse files Browse the repository at this point in the history
Replace usages of github.com/pkg/errors and github.com/joeshaw/multierror.
  • Loading branch information
andrewkroh committed Apr 10, 2024
1 parent 608fba5 commit 056b9f1
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 36 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.21

require (
github.com/elastic/go-windows v1.0.0
github.com/pkg/errors v0.9.1
github.com/prometheus/procfs v0.8.0
github.com/stretchr/testify v1.7.0
golang.org/x/sys v0.13.0
Expand All @@ -12,7 +13,6 @@ require (

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0 // indirect
)
5 changes: 2 additions & 3 deletions providers/freebsd/arch_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,16 @@
package freebsd

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
Expand Down
5 changes: 2 additions & 3 deletions providers/freebsd/boottime_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@
package freebsd

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))
Expand Down
7 changes: 3 additions & 4 deletions providers/freebsd/host_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ import "C"

import (
"context"
"errors"
"os"
"path/filepath"
"time"

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

"github.com/elastic/go-sysinfo/internal/registry"
Expand Down Expand Up @@ -109,7 +108,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
Expand All @@ -119,7 +118,7 @@ func (r *reader) addErr(err error) bool {

func (r *reader) Err() error {
if len(r.errs) > 0 {
return &multierror.MultiError{Errors: r.errs}
return errors.Join(r.errs...)
}
return nil
}
Expand Down
5 changes: 2 additions & 3 deletions providers/freebsd/kernel_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,16 @@
package freebsd

import (
"fmt"
"syscall"

"github.com/pkg/errors"
)

const kernelReleaseMIB = "kern.osrelease"

func KernelVersion() (string, error) {
version, err := syscall.Sysctl(kernelReleaseMIB)
if err != nil {
return "", errors.Wrap(err, "failed to get kernel version")
return "", fmt.Errorf("failed to get kernel version: %w", err)
}

return version, nil
Expand Down
5 changes: 2 additions & 3 deletions providers/freebsd/machineid_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,16 @@
package freebsd

import (
"fmt"
"syscall"

"github.com/pkg/errors"
)

const kernelHostUUIDMIB = "kern.hostuuid"

func MachineID() (string, error) {
uuid, err := syscall.Sysctl(kernelHostUUIDMIB)
if err != nil {
return "", errors.Wrap(err, "failed to get machine id")
return "", fmt.Errorf("failed to get machine id: %w", err)
}

return uuid, nil
Expand Down
17 changes: 8 additions & 9 deletions providers/freebsd/memory_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ package freebsd
import "C"

import (
"fmt"
"syscall"
"unsafe"

"github.com/pkg/errors"
)

const (
Expand All @@ -50,7 +49,7 @@ const (
func PageSize() (uint32, error) {
var pageSize uint32
if err := sysctlByName(hwPagesizeMIB, &pageSize); err != nil {
return 0, errors.Wrap(err, "failed to get hw.pagesize")
return 0, fmt.Errorf("failed to get hw.pagesize: %w", err)
}

return pageSize, nil
Expand All @@ -59,7 +58,7 @@ func PageSize() (uint32, error) {
func SwapMaxPages() (uint32, error) {
var maxPages uint32
if err := sysctlByName(hwPhysmemMIB, &maxPages); err != nil {
return 0, errors.Wrap(err, "failed to get vm.swap_maxpages")
return 0, fmt.Errorf("failed to get vm.swap_maxpages: %w", err)
}

return maxPages, nil
Expand All @@ -68,7 +67,7 @@ func SwapMaxPages() (uint32, error) {
func TotalMemory() (uint64, error) {
var size uint64
if err := sysctlByName(hwPhysmemMIB, &size); err != nil {
return 0, errors.Wrap(err, "failed to get hw.physmem")
return 0, fmt.Errorf("failed to get hw.physmem: %w", err)
}

return size, nil
Expand All @@ -77,7 +76,7 @@ func TotalMemory() (uint64, error) {
func VmTotal() (vmTotal, error) {
var vm vmTotal
if err := sysctlByName(vmVmtotalMIB, &vm); err != nil {
return vmTotal{}, errors.Wrap(err, "failed to get vm.vmtotal")
return vmTotal{}, fmt.Errorf("failed to get vm.vmtotal: %w", err)
}

return vm, nil
Expand All @@ -86,7 +85,7 @@ func VmTotal() (vmTotal, error) {
func NumFreeBuffers() (uint32, error) {
var numfreebuffers uint32
if err := sysctlByName(vfsNumfreebuffersMIB, &numfreebuffers); err != nil {
return 0, errors.Wrap(err, "failed to get vfs.numfreebuffers")
return 0, fmt.Errorf("failed to get vfs.numfreebuffers: %w", err)
}

return numfreebuffers, nil
Expand All @@ -101,14 +100,14 @@ func KvmGetSwapInfo() (kvmSwap, error) {
defer C.free(unsafe.Pointer(kvmOpenC))

if kdC, err := C.kvm_open(nil, devNullC, nil, syscall.O_RDONLY, kvmOpenC); kdC == nil {
return kvmSwap{}, errors.Wrap(err, "failed to open kvm")
return kvmSwap{}, fmt.Errorf("failed to open kvm: %w", err)
}

defer C.kvm_close((*C.struct___kvm)(unsafe.Pointer(kdC)))

var swap kvmSwap
if n, err := C.kvm_getswapinfo((*C.struct___kvm)(unsafe.Pointer(kdC)), (*C.struct_kvm_swap)(unsafe.Pointer(&swap)), 1, 0); n != 0 {
return kvmSwap{}, errors.Wrap(err, "failed to get kvm_getswapinfo")
return kvmSwap{}, fmt.Errorf("failed to get kvm_getswapinfo: %w", err)
}

return swap, nil
Expand Down
21 changes: 11 additions & 10 deletions providers/freebsd/process_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ package freebsd
import "C"

import (
"fmt"
"io/ioutil"
"os"
"strconv"
Expand All @@ -80,14 +81,14 @@ func getProcInfo(op, arg int) ([]process, error) {
procstat, err := C.procstat_open_sysctl()

if procstat == nil {
return nil, errors.Wrap(err, "failed to open procstat sysctl")
return nil, fmt.Errorf("failed to open procstat sysctl: %w", err)
}
defer C.procstat_close(procstat)

var count C.uint = 0
kprocs, err := C.procstat_getprocs(procstat, C.int(op), C.int(arg), &count)
if kprocs == nil {
return nil, errors.Wrap(err, "getprocs failed")
return nil, fmt.Errorf("getprocs failed: %w", err)
}
defer C.procstat_freeprocs(procstat, kprocs)

Expand Down Expand Up @@ -138,7 +139,7 @@ func getProcEnv(p *process) (map[string]string, error) {
procstat, err := C.procstat_open_sysctl()

if procstat == nil {
return nil, errors.Wrap(err, "failed to open procstat sysctl")
return nil, fmt.Errorf("failed to open procstat sysctl: %w", err)
}
defer C.procstat_close(procstat)

Expand All @@ -152,7 +153,7 @@ func getProcArgs(p *process) ([]string, error) {
procstat, err := C.procstat_open_sysctl()

if procstat == nil {
return nil, errors.Wrap(err, "failed to open procstat sysctl")
return nil, fmt.Errorf("failed to open procstat sysctl: %w", err)
}
defer C.procstat_close(procstat)

Expand All @@ -166,7 +167,7 @@ func getProcPathname(p *process) (string, error) {
procstat, err := C.procstat_open_sysctl()

if procstat == nil {
return "", errors.Wrap(err, "failed to open procstat sysctl")
return "", fmt.Errorf("failed to open procstat sysctl: %w", err)
}
defer C.procstat_close(procstat)

Expand Down Expand Up @@ -195,13 +196,13 @@ func getProcCWD(p *process) (string, error) {
procstat, err := C.procstat_open_sysctl()

if procstat == nil {
return "", errors.Wrap(err, "failed to open procstat sysctl")
return "", fmt.Errorf("failed to open procstat sysctl: %w", err)
}
defer C.procstat_close(procstat)

fs, err := C.procstat_getfiles(procstat, &p.kinfo, 0)
if fs == nil {
return "", errors.Wrap(err, "failed to get files")
return "", fmt.Errorf("failed to get files: %w", err)
}

defer C.procstat_freefiles(procstat, fs)
Expand Down Expand Up @@ -399,12 +400,12 @@ func Cptime() (map[string]uint64, error) {
var clock clockInfo

if err := sysctlByName(kernClockrateMIB, &clock); err != nil {
return make(map[string]uint64), errors.Wrap(err, "failed to get kern.clockrate")
return make(map[string]uint64), fmt.Errorf("failed to get kern.clockrate: %w", err)
}

cptime, err := syscall.Sysctl(kernCptimeMIB)
if err != nil {
return make(map[string]uint64), errors.Wrap(err, "failed to get kern.cp_time")
return make(map[string]uint64), fmt.Errorf("failed to get kern.cp_time: %w", err)
}

cpMap := make(map[string]uint64)
Expand All @@ -415,7 +416,7 @@ func Cptime() (map[string]uint64, error) {
for index, time := range times {
i, err := strconv.ParseUint(time, 10, 64)
if err != nil {
return cpMap, errors.Wrap(err, "error parsing kern.cp_time")
return cpMap, fmt.Errorf("error parsing kern.cp_time: %w", err)
}

cpMap[names[index]] = i * uint64(clock.Tick) * 1000
Expand Down

0 comments on commit 056b9f1

Please sign in to comment.