Skip to content

Commit e96073c

Browse files
authored
Merge pull request prometheus#1752 from prometheus/superq/error_verb
Use Go 1.13 error features
2 parents 3799895 + dfa53f8 commit e96073c

44 files changed

Lines changed: 107 additions & 93 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

collector/arp_linux.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func parseARPEntries(data io.Reader) (map[string]uint32, error) {
8484
}
8585

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

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

9999
for device, entryCount := range entries {

collector/bonding_linux.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package collector
1717

1818
import (
19+
"errors"
1920
"fmt"
2021
"io/ioutil"
2122
"os"
@@ -59,7 +60,7 @@ func (c *bondingCollector) Update(ch chan<- prometheus.Metric) error {
5960
statusfile := sysFilePath("class/net")
6061
bondingStats, err := readBondingStats(statusfile)
6162
if err != nil {
62-
if os.IsNotExist(err) {
63+
if errors.Is(err, os.ErrNotExist) {
6364
level.Debug(c.logger).Log("msg", "Not collecting bonding, file does not exist", "file", statusfile)
6465
return ErrNoData
6566
}
@@ -86,7 +87,7 @@ func readBondingStats(root string) (status map[string][2]int, err error) {
8687
sstat := [2]int{0, 0}
8788
for _, slave := range strings.Fields(string(slaves)) {
8889
state, err := ioutil.ReadFile(filepath.Join(root, master, fmt.Sprintf("lower_%s", slave), "bonding_slave", "mii_status"))
89-
if os.IsNotExist(err) {
90+
if errors.Is(err, os.ErrNotExist) {
9091
// some older? kernels use slave_ prefix
9192
state, err = ioutil.ReadFile(filepath.Join(root, master, fmt.Sprintf("slave_%s", slave), "bonding_slave", "mii_status"))
9293
}

collector/btrfs_linux.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func init() {
3737
func NewBtrfsCollector(logger log.Logger) (Collector, error) {
3838
fs, err := btrfs.NewFS(*sysPath)
3939
if err != nil {
40-
return nil, fmt.Errorf("failed to open sysfs: %v", err)
40+
return nil, fmt.Errorf("failed to open sysfs: %w", err)
4141
}
4242

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

5757
for _, s := range stats {

collector/buddyinfo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func NewBuddyinfoCollector(logger log.Logger) (Collector, error) {
5959
func (c *buddyinfoCollector) Update(ch chan<- prometheus.Metric) error {
6060
buddyInfo, err := c.fs.BuddyInfo()
6161
if err != nil {
62-
return fmt.Errorf("couldn't get buddyinfo: %s", err)
62+
return fmt.Errorf("couldn't get buddyinfo: %w", err)
6363
}
6464

6565
level.Debug(c.logger).Log("msg", "Set node_buddy", "buddyInfo", buddyInfo)

collector/diskstats_darwin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func NewDiskstatsCollector(logger log.Logger) (Collector, error) {
189189
func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) error {
190190
diskStats, err := iostat.ReadDriveStats()
191191
if err != nil {
192-
return fmt.Errorf("couldn't get diskstats: %s", err)
192+
return fmt.Errorf("couldn't get diskstats: %w", err)
193193
}
194194

195195
for _, stats := range diskStats {

collector/diskstats_linux.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ func NewDiskstatsCollector(logger log.Logger) (Collector, error) {
187187
func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) error {
188188
diskStats, err := getDiskStats()
189189
if err != nil {
190-
return fmt.Errorf("couldn't get diskstats: %s", err)
190+
return fmt.Errorf("couldn't get diskstats: %w", err)
191191
}
192192

193193
for dev, stats := range diskStats {
@@ -203,7 +203,7 @@ func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) error {
203203
}
204204
v, err := strconv.ParseFloat(value, 64)
205205
if err != nil {
206-
return fmt.Errorf("invalid value %s in diskstats: %s", value, err)
206+
return fmt.Errorf("invalid value %s in diskstats: %w", value, err)
207207
}
208208
ch <- c.descs[i].mustNewConstMetric(v, dev)
209209
}

collector/drbd_linux.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package collector
1717

1818
import (
1919
"bufio"
20+
"errors"
2021
"fmt"
2122
"os"
2223
"strconv"
@@ -188,7 +189,7 @@ func (c *drbdCollector) Update(ch chan<- prometheus.Metric) error {
188189
statsFile := procFilePath("drbd")
189190
file, err := os.Open(statsFile)
190191
if err != nil {
191-
if os.IsNotExist(err) {
192+
if errors.Is(err, os.ErrNotExist) {
192193
level.Debug(c.logger).Log("msg", "stats file does not exist, skipping", "file", statsFile, "err", err)
193194
return ErrNoData
194195
}

collector/edac_linux.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,28 +86,28 @@ func (c *edacCollector) Update(ch chan<- prometheus.Metric) error {
8686

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

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

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

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

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

134134
value, err = readUintFromFile(filepath.Join(csrow, "ue_count"))
135135
if err != nil {
136-
return fmt.Errorf("couldn't get ue_count for controller/csrow %s/%s: %s", controllerNumber, csrowNumber, err)
136+
return fmt.Errorf("couldn't get ue_count for controller/csrow %s/%s: %w", controllerNumber, csrowNumber, err)
137137
}
138138
ch <- prometheus.MustNewConstMetric(
139139
c.csRowUECount, prometheus.CounterValue, float64(value), controllerNumber, csrowNumber)

collector/entropy_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func NewEntropyCollector(logger log.Logger) (Collector, error) {
4646
func (c *entropyCollector) Update(ch chan<- prometheus.Metric) error {
4747
value, err := readUintFromFile(procFilePath("sys/kernel/random/entropy_avail"))
4848
if err != nil {
49-
return fmt.Errorf("couldn't get entropy_avail: %s", err)
49+
return fmt.Errorf("couldn't get entropy_avail: %w", err)
5050
}
5151
ch <- prometheus.MustNewConstMetric(
5252
c.entropyAvail, prometheus.GaugeValue, float64(value))

collector/filefd_linux.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ func NewFileFDStatCollector(logger log.Logger) (Collector, error) {
4646
func (c *fileFDStatCollector) Update(ch chan<- prometheus.Metric) error {
4747
fileFDStat, err := parseFileFDStats(procFilePath("sys/fs/file-nr"))
4848
if err != nil {
49-
return fmt.Errorf("couldn't get file-nr: %s", err)
49+
return fmt.Errorf("couldn't get file-nr: %w", err)
5050
}
5151
for name, value := range fileFDStat {
5252
v, err := strconv.ParseFloat(value, 64)
5353
if err != nil {
54-
return fmt.Errorf("invalid value %s in file-nr: %s", value, err)
54+
return fmt.Errorf("invalid value %s in file-nr: %w", value, err)
5555
}
5656
ch <- prometheus.MustNewConstMetric(
5757
prometheus.NewDesc(

0 commit comments

Comments
 (0)