Skip to content

Commit

Permalink
Merge pull request #1752 from prometheus/superq/error_verb
Browse files Browse the repository at this point in the history
Use Go 1.13 error features
  • Loading branch information
SuperQ authored Jun 18, 2020
2 parents 3799895 + dfa53f8 commit e96073c
Show file tree
Hide file tree
Showing 44 changed files with 107 additions and 93 deletions.
4 changes: 2 additions & 2 deletions collector/arp_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func parseARPEntries(data io.Reader) (map[string]uint32, error) {
}

if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("failed to parse ARP info: %s", err)
return nil, fmt.Errorf("failed to parse ARP info: %w", err)
}

return entries, nil
Expand All @@ -93,7 +93,7 @@ func parseARPEntries(data io.Reader) (map[string]uint32, error) {
func (c *arpCollector) Update(ch chan<- prometheus.Metric) error {
entries, err := getARPEntries()
if err != nil {
return fmt.Errorf("could not get ARP entries: %s", err)
return fmt.Errorf("could not get ARP entries: %w", err)
}

for device, entryCount := range entries {
Expand Down
5 changes: 3 additions & 2 deletions collector/bonding_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package collector

import (
"errors"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -59,7 +60,7 @@ func (c *bondingCollector) Update(ch chan<- prometheus.Metric) error {
statusfile := sysFilePath("class/net")
bondingStats, err := readBondingStats(statusfile)
if err != nil {
if os.IsNotExist(err) {
if errors.Is(err, os.ErrNotExist) {
level.Debug(c.logger).Log("msg", "Not collecting bonding, file does not exist", "file", statusfile)
return ErrNoData
}
Expand All @@ -86,7 +87,7 @@ func readBondingStats(root string) (status map[string][2]int, err error) {
sstat := [2]int{0, 0}
for _, slave := range strings.Fields(string(slaves)) {
state, err := ioutil.ReadFile(filepath.Join(root, master, fmt.Sprintf("lower_%s", slave), "bonding_slave", "mii_status"))
if os.IsNotExist(err) {
if errors.Is(err, os.ErrNotExist) {
// some older? kernels use slave_ prefix
state, err = ioutil.ReadFile(filepath.Join(root, master, fmt.Sprintf("slave_%s", slave), "bonding_slave", "mii_status"))
}
Expand Down
4 changes: 2 additions & 2 deletions collector/btrfs_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func init() {
func NewBtrfsCollector(logger log.Logger) (Collector, error) {
fs, err := btrfs.NewFS(*sysPath)
if err != nil {
return nil, fmt.Errorf("failed to open sysfs: %v", err)
return nil, fmt.Errorf("failed to open sysfs: %w", err)
}

return &btrfsCollector{
Expand All @@ -51,7 +51,7 @@ func NewBtrfsCollector(logger log.Logger) (Collector, error) {
func (c *btrfsCollector) Update(ch chan<- prometheus.Metric) error {
stats, err := c.fs.Stats()
if err != nil {
return fmt.Errorf("failed to retrieve Btrfs stats: %v", err)
return fmt.Errorf("failed to retrieve Btrfs stats: %w", err)
}

for _, s := range stats {
Expand Down
2 changes: 1 addition & 1 deletion collector/buddyinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func NewBuddyinfoCollector(logger log.Logger) (Collector, error) {
func (c *buddyinfoCollector) Update(ch chan<- prometheus.Metric) error {
buddyInfo, err := c.fs.BuddyInfo()
if err != nil {
return fmt.Errorf("couldn't get buddyinfo: %s", err)
return fmt.Errorf("couldn't get buddyinfo: %w", err)
}

level.Debug(c.logger).Log("msg", "Set node_buddy", "buddyInfo", buddyInfo)
Expand Down
2 changes: 1 addition & 1 deletion collector/diskstats_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func NewDiskstatsCollector(logger log.Logger) (Collector, error) {
func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) error {
diskStats, err := iostat.ReadDriveStats()
if err != nil {
return fmt.Errorf("couldn't get diskstats: %s", err)
return fmt.Errorf("couldn't get diskstats: %w", err)
}

for _, stats := range diskStats {
Expand Down
4 changes: 2 additions & 2 deletions collector/diskstats_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func NewDiskstatsCollector(logger log.Logger) (Collector, error) {
func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) error {
diskStats, err := getDiskStats()
if err != nil {
return fmt.Errorf("couldn't get diskstats: %s", err)
return fmt.Errorf("couldn't get diskstats: %w", err)
}

for dev, stats := range diskStats {
Expand All @@ -203,7 +203,7 @@ func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) error {
}
v, err := strconv.ParseFloat(value, 64)
if err != nil {
return fmt.Errorf("invalid value %s in diskstats: %s", value, err)
return fmt.Errorf("invalid value %s in diskstats: %w", value, err)
}
ch <- c.descs[i].mustNewConstMetric(v, dev)
}
Expand Down
3 changes: 2 additions & 1 deletion collector/drbd_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package collector

import (
"bufio"
"errors"
"fmt"
"os"
"strconv"
Expand Down Expand Up @@ -188,7 +189,7 @@ func (c *drbdCollector) Update(ch chan<- prometheus.Metric) error {
statsFile := procFilePath("drbd")
file, err := os.Open(statsFile)
if err != nil {
if os.IsNotExist(err) {
if errors.Is(err, os.ErrNotExist) {
level.Debug(c.logger).Log("msg", "stats file does not exist, skipping", "file", statsFile, "err", err)
return ErrNoData
}
Expand Down
12 changes: 6 additions & 6 deletions collector/edac_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,28 +86,28 @@ func (c *edacCollector) Update(ch chan<- prometheus.Metric) error {

value, err := readUintFromFile(filepath.Join(controller, "ce_count"))
if err != nil {
return fmt.Errorf("couldn't get ce_count for controller %s: %s", controllerNumber, err)
return fmt.Errorf("couldn't get ce_count for controller %s: %w", controllerNumber, err)
}
ch <- prometheus.MustNewConstMetric(
c.ceCount, prometheus.CounterValue, float64(value), controllerNumber)

value, err = readUintFromFile(filepath.Join(controller, "ce_noinfo_count"))
if err != nil {
return fmt.Errorf("couldn't get ce_noinfo_count for controller %s: %s", controllerNumber, err)
return fmt.Errorf("couldn't get ce_noinfo_count for controller %s: %w", controllerNumber, err)
}
ch <- prometheus.MustNewConstMetric(
c.csRowCECount, prometheus.CounterValue, float64(value), controllerNumber, "unknown")

value, err = readUintFromFile(filepath.Join(controller, "ue_count"))
if err != nil {
return fmt.Errorf("couldn't get ue_count for controller %s: %s", controllerNumber, err)
return fmt.Errorf("couldn't get ue_count for controller %s: %w", controllerNumber, err)
}
ch <- prometheus.MustNewConstMetric(
c.ueCount, prometheus.CounterValue, float64(value), controllerNumber)

value, err = readUintFromFile(filepath.Join(controller, "ue_noinfo_count"))
if err != nil {
return fmt.Errorf("couldn't get ue_noinfo_count for controller %s: %s", controllerNumber, err)
return fmt.Errorf("couldn't get ue_noinfo_count for controller %s: %w", controllerNumber, err)
}
ch <- prometheus.MustNewConstMetric(
c.csRowUECount, prometheus.CounterValue, float64(value), controllerNumber, "unknown")
Expand All @@ -126,14 +126,14 @@ func (c *edacCollector) Update(ch chan<- prometheus.Metric) error {

value, err = readUintFromFile(filepath.Join(csrow, "ce_count"))
if err != nil {
return fmt.Errorf("couldn't get ce_count for controller/csrow %s/%s: %s", controllerNumber, csrowNumber, err)
return fmt.Errorf("couldn't get ce_count for controller/csrow %s/%s: %w", controllerNumber, csrowNumber, err)
}
ch <- prometheus.MustNewConstMetric(
c.csRowCECount, prometheus.CounterValue, float64(value), controllerNumber, csrowNumber)

value, err = readUintFromFile(filepath.Join(csrow, "ue_count"))
if err != nil {
return fmt.Errorf("couldn't get ue_count for controller/csrow %s/%s: %s", controllerNumber, csrowNumber, err)
return fmt.Errorf("couldn't get ue_count for controller/csrow %s/%s: %w", controllerNumber, csrowNumber, err)
}
ch <- prometheus.MustNewConstMetric(
c.csRowUECount, prometheus.CounterValue, float64(value), controllerNumber, csrowNumber)
Expand Down
2 changes: 1 addition & 1 deletion collector/entropy_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func NewEntropyCollector(logger log.Logger) (Collector, error) {
func (c *entropyCollector) Update(ch chan<- prometheus.Metric) error {
value, err := readUintFromFile(procFilePath("sys/kernel/random/entropy_avail"))
if err != nil {
return fmt.Errorf("couldn't get entropy_avail: %s", err)
return fmt.Errorf("couldn't get entropy_avail: %w", err)
}
ch <- prometheus.MustNewConstMetric(
c.entropyAvail, prometheus.GaugeValue, float64(value))
Expand Down
4 changes: 2 additions & 2 deletions collector/filefd_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ func NewFileFDStatCollector(logger log.Logger) (Collector, error) {
func (c *fileFDStatCollector) Update(ch chan<- prometheus.Metric) error {
fileFDStat, err := parseFileFDStats(procFilePath("sys/fs/file-nr"))
if err != nil {
return fmt.Errorf("couldn't get file-nr: %s", err)
return fmt.Errorf("couldn't get file-nr: %w", err)
}
for name, value := range fileFDStat {
v, err := strconv.ParseFloat(value, 64)
if err != nil {
return fmt.Errorf("invalid value %s in file-nr: %s", value, err)
return fmt.Errorf("invalid value %s in file-nr: %w", value, err)
}
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
Expand Down
3 changes: 2 additions & 1 deletion collector/filesystem_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package collector

import (
"bufio"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -139,7 +140,7 @@ func stuckMountWatcher(mountPoint string, success chan struct{}, logger log.Logg

func mountPointDetails(logger log.Logger) ([]filesystemLabels, error) {
file, err := os.Open(procFilePath("1/mounts"))
if os.IsNotExist(err) {
if errors.Is(err, os.ErrNotExist) {
// Fallback to `/proc/mounts` if `/proc/1/mounts` is missing due hidepid.
level.Debug(logger).Log("msg", "Reading root mounts failed, falling back to system mounts", "err", err)
file, err = os.Open(procFilePath("mounts"))
Expand Down
2 changes: 1 addition & 1 deletion collector/hwmon_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ func (c *hwMonCollector) Update(ch chan<- prometheus.Metric) error {

hwmonFiles, err := ioutil.ReadDir(hwmonPathName)
if err != nil {
if os.IsNotExist(err) {
if errors.Is(err, os.ErrNotExist) {
level.Debug(c.logger).Log("msg", "hwmon collector metrics are not available for this system")
return ErrNoData
}
Expand Down
5 changes: 3 additions & 2 deletions collector/infiniband_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package collector

import (
"errors"
"fmt"
"os"
"strconv"
Expand Down Expand Up @@ -108,11 +109,11 @@ func (c *infinibandCollector) pushCounter(ch chan<- prometheus.Metric, name stri
func (c *infinibandCollector) Update(ch chan<- prometheus.Metric) error {
devices, err := c.fs.InfiniBandClass()
if err != nil {
if os.IsNotExist(err) {
if errors.Is(err, os.ErrNotExist) {
level.Debug(c.logger).Log("msg", "infiniband statistics not found, skipping")
return ErrNoData
}
return fmt.Errorf("error obtaining InfiniBand class info: %s", err)
return fmt.Errorf("error obtaining InfiniBand class info: %w", err)
}

for _, device := range devices {
Expand Down
4 changes: 2 additions & 2 deletions collector/interrupts_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ var (
func (c *interruptsCollector) Update(ch chan<- prometheus.Metric) (err error) {
interrupts, err := getInterrupts()
if err != nil {
return fmt.Errorf("couldn't get interrupts: %s", err)
return fmt.Errorf("couldn't get interrupts: %w", err)
}
for name, interrupt := range interrupts {
for cpuNo, value := range interrupt.values {
fv, err := strconv.ParseFloat(value, 64)
if err != nil {
return fmt.Errorf("invalid value %s in interrupts: %s", value, err)
return fmt.Errorf("invalid value %s in interrupts: %w", value, err)
}
ch <- c.desc.mustNewConstMetric(fv, strconv.Itoa(cpuNo), name, interrupt.info, interrupt.devices)
}
Expand Down
2 changes: 1 addition & 1 deletion collector/interrupts_openbsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ var (
func (c *interruptsCollector) Update(ch chan<- prometheus.Metric) error {
interrupts, err := getInterrupts()
if err != nil {
return fmt.Errorf("couldn't get interrupts: %s", err)
return fmt.Errorf("couldn't get interrupts: %w", err)
}
for dev, interrupt := range interrupts {
for cpuNo, value := range interrupt.values {
Expand Down
7 changes: 4 additions & 3 deletions collector/ipvs_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package collector

import (
"errors"
"fmt"
"os"
"sort"
Expand Down Expand Up @@ -140,11 +141,11 @@ func (c *ipvsCollector) Update(ch chan<- prometheus.Metric) error {
ipvsStats, err := c.fs.IPVSStats()
if err != nil {
// Cannot access ipvs metrics, report no error.
if os.IsNotExist(err) {
if errors.Is(err, os.ErrNotExist) {
level.Debug(c.logger).Log("msg", "ipvs collector metrics are not available for this system")
return ErrNoData
}
return fmt.Errorf("could not get IPVS stats: %s", err)
return fmt.Errorf("could not get IPVS stats: %w", err)
}
ch <- c.connections.mustNewConstMetric(float64(ipvsStats.Connections))
ch <- c.incomingPackets.mustNewConstMetric(float64(ipvsStats.IncomingPackets))
Expand All @@ -154,7 +155,7 @@ func (c *ipvsCollector) Update(ch chan<- prometheus.Metric) error {

backendStats, err := c.fs.IPVSBackendStatus()
if err != nil {
return fmt.Errorf("could not get backend status: %s", err)
return fmt.Errorf("could not get backend status: %w", err)
}

sums := map[string]ipvsBackendStatus{}
Expand Down
2 changes: 1 addition & 1 deletion collector/loadavg.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func NewLoadavgCollector(logger log.Logger) (Collector, error) {
func (c *loadavgCollector) Update(ch chan<- prometheus.Metric) error {
loads, err := getLoad()
if err != nil {
return fmt.Errorf("couldn't get load: %s", err)
return fmt.Errorf("couldn't get load: %w", err)
}
for i, load := range loads {
level.Debug(c.logger).Log("msg", "return load", "index", i, "load", load)
Expand Down
2 changes: 1 addition & 1 deletion collector/loadavg_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func parseLoad(data string) (loads []float64, err error) {
for i, load := range parts[0:3] {
loads[i], err = strconv.ParseFloat(load, 64)
if err != nil {
return nil, fmt.Errorf("could not parse load '%s': %s", load, err)
return nil, fmt.Errorf("could not parse load '%s': %w", load, err)
}
}
return loads, nil
Expand Down
6 changes: 3 additions & 3 deletions collector/logind_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func NewLogindCollector(logger log.Logger) (Collector, error) {
func (lc *logindCollector) Update(ch chan<- prometheus.Metric) error {
c, err := newDbus()
if err != nil {
return fmt.Errorf("unable to connect to dbus: %s", err)
return fmt.Errorf("unable to connect to dbus: %w", err)
}
defer c.conn.Close()

Expand All @@ -102,12 +102,12 @@ func (lc *logindCollector) Update(ch chan<- prometheus.Metric) error {
func collectMetrics(ch chan<- prometheus.Metric, c logindInterface) error {
seats, err := c.listSeats()
if err != nil {
return fmt.Errorf("unable to get seats: %s", err)
return fmt.Errorf("unable to get seats: %w", err)
}

sessionList, err := c.listSessions()
if err != nil {
return fmt.Errorf("unable to get sessions: %s", err)
return fmt.Errorf("unable to get sessions: %w", err)
}

sessions := make(map[logindSession]float64)
Expand Down
11 changes: 6 additions & 5 deletions collector/mdadm_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package collector

import (
"errors"
"fmt"
"os"

Expand Down Expand Up @@ -94,21 +95,21 @@ var (
)

func (c *mdadmCollector) Update(ch chan<- prometheus.Metric) error {
fs, errFs := procfs.NewFS(*procPath)
fs, err := procfs.NewFS(*procPath)

if errFs != nil {
return fmt.Errorf("failed to open procfs: %w", errFs)
if err != nil {
return fmt.Errorf("failed to open procfs: %w", err)
}

mdStats, err := fs.MDStat()

if err != nil {
if os.IsNotExist(err) {
if errors.Is(err, os.ErrNotExist) {
level.Debug(c.logger).Log("msg", "Not collecting mdstat, file does not exist", "file", *procPath)
return ErrNoData
}

return fmt.Errorf("error parsing mdstatus: %s", err)
return fmt.Errorf("error parsing mdstatus: %w", err)
}

for _, mdStat := range mdStats {
Expand Down
2 changes: 1 addition & 1 deletion collector/meminfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (c *meminfoCollector) Update(ch chan<- prometheus.Metric) error {
var metricType prometheus.ValueType
memInfo, err := c.getMemInfo()
if err != nil {
return fmt.Errorf("couldn't get meminfo: %s", err)
return fmt.Errorf("couldn't get meminfo: %w", err)
}
level.Debug(c.logger).Log("msg", "Set node_mem", "memInfo", memInfo)
for k, v := range memInfo {
Expand Down
2 changes: 1 addition & 1 deletion collector/meminfo_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func parseMemInfo(r io.Reader) (map[string]float64, error) {
}
fv, err := strconv.ParseFloat(parts[1], 64)
if err != nil {
return nil, fmt.Errorf("invalid value in meminfo: %s", err)
return nil, fmt.Errorf("invalid value in meminfo: %w", err)
}
key := parts[0][:len(parts[0])-1] // remove trailing : from key
// Active(anon) -> Active_anon
Expand Down
Loading

0 comments on commit e96073c

Please sign in to comment.