Skip to content

Commit

Permalink
chore: Fix linter findings for errorlint (part4) (#12723)
Browse files Browse the repository at this point in the history
Co-authored-by: Pawel Zak <Pawel Zak>
(cherry picked from commit 312fb04)
  • Loading branch information
zak-pawel authored and powersj committed Feb 27, 2023
1 parent f4620ab commit a8300ab
Show file tree
Hide file tree
Showing 41 changed files with 153 additions and 142 deletions.
6 changes: 3 additions & 3 deletions plugins/inputs/activemq/activemq.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func (a *ActiveMQ) Gather(acc telegraf.Accumulator) error {
queues := Queues{}
err = xml.Unmarshal(dataQueues, &queues)
if err != nil {
return fmt.Errorf("queues XML unmarshal error: %v", err)
return fmt.Errorf("queues XML unmarshal error: %w", err)
}

dataTopics, err := a.GetMetrics(a.TopicsURL())
Expand All @@ -239,7 +239,7 @@ func (a *ActiveMQ) Gather(acc telegraf.Accumulator) error {
topics := Topics{}
err = xml.Unmarshal(dataTopics, &topics)
if err != nil {
return fmt.Errorf("topics XML unmarshal error: %v", err)
return fmt.Errorf("topics XML unmarshal error: %w", err)
}

dataSubscribers, err := a.GetMetrics(a.SubscribersURL())
Expand All @@ -249,7 +249,7 @@ func (a *ActiveMQ) Gather(acc telegraf.Accumulator) error {
subscribers := Subscribers{}
err = xml.Unmarshal(dataSubscribers, &subscribers)
if err != nil {
return fmt.Errorf("subscribers XML unmarshal error: %v", err)
return fmt.Errorf("subscribers XML unmarshal error: %w", err)
}

a.GatherQueuesMetrics(acc, queues)
Expand Down
14 changes: 7 additions & 7 deletions plugins/inputs/amqp_consumer/amqp_consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (a *AMQPConsumer) connect(amqpConf *amqp.Config) (<-chan amqp.Delivery, err

ch, err := a.conn.Channel()
if err != nil {
return nil, fmt.Errorf("failed to open a channel: %s", err.Error())
return nil, fmt.Errorf("failed to open a channel: %w", err)
}

if a.Exchange != "" {
Expand Down Expand Up @@ -250,7 +250,7 @@ func (a *AMQPConsumer) connect(amqpConf *amqp.Config) (<-chan amqp.Delivery, err
nil,
)
if err != nil {
return nil, fmt.Errorf("failed to bind a queue: %s", err)
return nil, fmt.Errorf("failed to bind a queue: %w", err)
}
}

Expand All @@ -260,7 +260,7 @@ func (a *AMQPConsumer) connect(amqpConf *amqp.Config) (<-chan amqp.Delivery, err
false, // global
)
if err != nil {
return nil, fmt.Errorf("failed to set QoS: %s", err)
return nil, fmt.Errorf("failed to set QoS: %w", err)
}

msgs, err := ch.Consume(
Expand All @@ -273,7 +273,7 @@ func (a *AMQPConsumer) connect(amqpConf *amqp.Config) (<-chan amqp.Delivery, err
nil, // arguments
)
if err != nil {
return nil, fmt.Errorf("failed establishing connection to queue: %s", err)
return nil, fmt.Errorf("failed establishing connection to queue: %w", err)
}

return msgs, err
Expand Down Expand Up @@ -307,7 +307,7 @@ func (a *AMQPConsumer) declareExchange(
)
}
if err != nil {
return fmt.Errorf("error declaring exchange: %v", err)
return fmt.Errorf("error declaring exchange: %w", err)
}
return nil
}
Expand Down Expand Up @@ -341,7 +341,7 @@ func (a *AMQPConsumer) declareQueue(channel *amqp.Channel) (*amqp.Queue, error)
)
}
if err != nil {
return nil, fmt.Errorf("error declaring queue: %v", err)
return nil, fmt.Errorf("error declaring queue: %w", err)
}
return &queue, nil
}
Expand Down Expand Up @@ -442,7 +442,7 @@ func (a *AMQPConsumer) Stop() {
a.cancel()
a.wg.Wait()
err := a.conn.Close()
if err != nil && err != amqp.ErrClosed {
if err != nil && !errors.Is(err, amqp.ErrClosed) {
a.Log.Errorf("Error closing AMQP connection: %s", err)
return
}
Expand Down
6 changes: 3 additions & 3 deletions plugins/inputs/apache/apache.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (n *Apache) Gather(acc telegraf.Accumulator) error {
for _, u := range n.Urls {
addr, err := url.Parse(u)
if err != nil {
acc.AddError(fmt.Errorf("unable to parse address '%s': %s", u, err))
acc.AddError(fmt.Errorf("unable to parse address %q: %w", u, err))
continue
}

Expand Down Expand Up @@ -91,7 +91,7 @@ func (n *Apache) createHTTPClient() (*http.Client, error) {
func (n *Apache) gatherURL(addr *url.URL, acc telegraf.Accumulator) error {
req, err := http.NewRequest("GET", addr.String(), nil)
if err != nil {
return fmt.Errorf("error on new request to %s : %s", addr.String(), err)
return fmt.Errorf("error on new request to %q: %w", addr.String(), err)
}

if len(n.Username) != 0 && len(n.Password) != 0 {
Expand All @@ -100,7 +100,7 @@ func (n *Apache) gatherURL(addr *url.URL, acc telegraf.Accumulator) error {

resp, err := n.client.Do(req)
if err != nil {
return fmt.Errorf("error on request to %s : %s", addr.String(), err)
return fmt.Errorf("error on request to %q: %w", addr.String(), err)
}
defer resp.Body.Close()

Expand Down
8 changes: 4 additions & 4 deletions plugins/inputs/aurora/aurora.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (a *Aurora) Gather(acc telegraf.Accumulator) error {
defer wg.Done()
role, err := a.gatherRole(ctx, u)
if err != nil {
acc.AddError(fmt.Errorf("%s: %v", u, err))
acc.AddError(fmt.Errorf("%s: %w", u, err))
return
}

Expand All @@ -91,7 +91,7 @@ func (a *Aurora) Gather(acc telegraf.Accumulator) error {

err = a.gatherScheduler(ctx, u, role, acc)
if err != nil {
acc.AddError(fmt.Errorf("%s: %v", u, err))
acc.AddError(fmt.Errorf("%s: %w", u, err))
}
}(u)
}
Expand Down Expand Up @@ -167,7 +167,7 @@ func (a *Aurora) gatherRole(ctx context.Context, origin *url.URL) (RoleType, err
return Unknown, err
}
if err := resp.Body.Close(); err != nil {
return Unknown, fmt.Errorf("closing body failed: %v", err)
return Unknown, fmt.Errorf("closing body failed: %w", err)
}

switch resp.StatusCode {
Expand Down Expand Up @@ -212,7 +212,7 @@ func (a *Aurora) gatherScheduler(
decoder.UseNumber()
err = decoder.Decode(&vars)
if err != nil {
return fmt.Errorf("decoding response: %v", err)
return fmt.Errorf("decoding response: %w", err)
}

var fields = make(map[string]interface{}, len(vars))
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/bcache/bcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (b *Bcache) Gather(acc telegraf.Accumulator) error {
}
}
if err := b.gatherBcache(bdev, acc); err != nil {
return fmt.Errorf("gathering bcache failed: %v", err)
return fmt.Errorf("gathering bcache failed: %w", err)
}
}
return nil
Expand Down
3 changes: 2 additions & 1 deletion plugins/inputs/beanstalkd/beanstalkd_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package beanstalkd_test

import (
"errors"
"io"
"net"
"net/textproto"
Expand Down Expand Up @@ -121,7 +122,7 @@ func startTestServer(t *testing.T) (net.Listener, error) {

for {
cmd, err := tp.ReadLine()
if err == io.EOF {
if errors.Is(err, io.EOF) {
return
} else if err != nil {
t.Log("Test server: failed read command. Error: ", err)
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/bind/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (b *Bind) Gather(acc telegraf.Accumulator) error {
for _, u := range b.Urls {
addr, err := url.Parse(u)
if err != nil {
acc.AddError(fmt.Errorf("unable to parse address '%s': %s", u, err))
acc.AddError(fmt.Errorf("unable to parse address %q: %w", u, err))
continue
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/bind/json_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (b *Bind) readStatsJSON(addr *url.URL, acc telegraf.Accumulator) error {
}

if err := json.NewDecoder(resp.Body).Decode(&stats); err != nil {
return fmt.Errorf("unable to decode JSON blob: %s", err)
return fmt.Errorf("unable to decode JSON blob: %w", err)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/bind/xml_stats_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (b *Bind) readStatsXMLv2(addr *url.URL, acc telegraf.Accumulator) error {
}

if err := xml.NewDecoder(resp.Body).Decode(&stats); err != nil {
return fmt.Errorf("unable to decode XML document: %s", err)
return fmt.Errorf("unable to decode XML document: %w", err)
}

tags := map[string]string{"url": addr.Host}
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/bind/xml_stats_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (b *Bind) readStatsXMLv3(addr *url.URL, acc telegraf.Accumulator) error {
}

if err := xml.NewDecoder(resp.Body).Decode(&stats); err != nil {
return fmt.Errorf("unable to decode XML document: %s", err)
return fmt.Errorf("unable to decode XML document: %w", err)
}

return nil
Expand Down
10 changes: 5 additions & 5 deletions plugins/inputs/bond/bond.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ func (bond *Bond) Gather(acc telegraf.Accumulator) error {
bondAbsPath := bond.HostProc + "/net/bonding/" + bondName
file, err := os.ReadFile(bondAbsPath)
if err != nil {
acc.AddError(fmt.Errorf("error inspecting %q interface: %v", bondAbsPath, err))
acc.AddError(fmt.Errorf("error inspecting %q interface: %w", bondAbsPath, err))
continue
}
rawProcFile := strings.TrimSpace(string(file))
err = bond.gatherBondInterface(bondName, rawProcFile, acc)
if err != nil {
acc.AddError(fmt.Errorf("error inspecting %q interface: %v", bondName, err))
acc.AddError(fmt.Errorf("error inspecting %q interface: %w", bondName, err))
}

/*
Expand Down Expand Up @@ -153,18 +153,18 @@ func (bond *Bond) readSysFiles(bondDir string) (sysFiles, error) {

file, err := os.ReadFile(bondDir + "/bonding/mode")
if err != nil {
return sysFiles{}, fmt.Errorf("error inspecting %q interface: %v", bondDir+"/bonding/mode", err)
return sysFiles{}, fmt.Errorf("error inspecting %q interface: %w", bondDir+"/bonding/mode", err)
}
output.ModeFile = strings.TrimSpace(string(file))
file, err = os.ReadFile(bondDir + "/bonding/slaves")
if err != nil {
return sysFiles{}, fmt.Errorf("error inspecting %q interface: %v", bondDir+"/bonding/slaves", err)
return sysFiles{}, fmt.Errorf("error inspecting %q interface: %w", bondDir+"/bonding/slaves", err)
}
output.SlaveFile = strings.TrimSpace(string(file))
if bond.BondType == "IEEE 802.3ad Dynamic link aggregation" {
file, err = os.ReadFile(bondDir + "/bonding/ad_num_ports")
if err != nil {
return sysFiles{}, fmt.Errorf("error inspecting %q interface: %v", bondDir+"/bonding/ad_num_ports", err)
return sysFiles{}, fmt.Errorf("error inspecting %q interface: %w", bondDir+"/bonding/ad_num_ports", err)
}
output.ADPortsFile = strings.TrimSpace(string(file))
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/burrow/burrow.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (b *burrow) Gather(acc telegraf.Accumulator) error {
for _, addr := range b.Servers {
u, err := url.Parse(addr)
if err != nil {
acc.AddError(fmt.Errorf("unable to parse address '%s': %s", addr, err))
acc.AddError(fmt.Errorf("unable to parse address %q: %w", addr, err))
continue
}
if u.Path == "" {
Expand Down
24 changes: 12 additions & 12 deletions plugins/inputs/ceph/ceph.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,18 @@ func (c *Ceph) Gather(acc telegraf.Accumulator) error {
func (c *Ceph) gatherAdminSocketStats(acc telegraf.Accumulator) error {
sockets, err := findSockets(c)
if err != nil {
return fmt.Errorf("failed to find sockets at path '%s': %v", c.SocketDir, err)
return fmt.Errorf("failed to find sockets at path %q: %w", c.SocketDir, err)
}

for _, s := range sockets {
dump, err := perfDump(c.CephBinary, s)
if err != nil {
acc.AddError(fmt.Errorf("error reading from socket '%s': %v", s.socket, err))
acc.AddError(fmt.Errorf("error reading from socket %q: %w", s.socket, err))
continue
}
data, err := c.parseDump(dump)
if err != nil {
acc.AddError(fmt.Errorf("error parsing dump from socket '%s': %v", s.socket, err))
acc.AddError(fmt.Errorf("error parsing dump from socket %q: %w", s.socket, err))
continue
}
for tag, metrics := range data {
Expand All @@ -107,11 +107,11 @@ func (c *Ceph) gatherClusterStats(acc telegraf.Accumulator) error {
for _, job := range jobs {
output, err := c.execute(job.command)
if err != nil {
return fmt.Errorf("error executing command: %v", err)
return fmt.Errorf("error executing command: %w", err)
}
err = job.parser(acc, output)
if err != nil {
return fmt.Errorf("error parsing output: %v", err)
return fmt.Errorf("error parsing output: %w", err)
}
}

Expand Down Expand Up @@ -157,7 +157,7 @@ var perfDump = func(binary string, socket *socket) (string, error) {
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return "", fmt.Errorf("error running ceph dump: %s", err)
return "", fmt.Errorf("error running ceph dump: %w", err)
}

return out.String(), nil
Expand All @@ -166,7 +166,7 @@ var perfDump = func(binary string, socket *socket) (string, error) {
var findSockets = func(c *Ceph) ([]*socket, error) {
listing, err := os.ReadDir(c.SocketDir)
if err != nil {
return []*socket{}, fmt.Errorf("Failed to read socket directory '%s': %v", c.SocketDir, err)
return []*socket{}, fmt.Errorf("failed to read socket directory %q: %w", c.SocketDir, err)
}
sockets := make([]*socket, 0, len(listing))
for _, info := range listing {
Expand Down Expand Up @@ -239,7 +239,7 @@ func (c *Ceph) parseDump(dump string) (taggedMetricMap, error) {
data := make(map[string]interface{})
err := json.Unmarshal([]byte(dump), &data)
if err != nil {
return nil, fmt.Errorf("failed to parse json: '%s': %v", dump, err)
return nil, fmt.Errorf("failed to parse json: %q: %w", dump, err)
}

return c.newTaggedMetricMap(data), nil
Expand Down Expand Up @@ -299,7 +299,7 @@ func (c *Ceph) execute(command string) (string, error) {
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return "", fmt.Errorf("error running ceph %v: %s", command, err)
return "", fmt.Errorf("error running ceph %q: %w", command, err)
}

output := out.String()
Expand Down Expand Up @@ -378,7 +378,7 @@ type CephStatus struct {
func decodeStatus(acc telegraf.Accumulator, input string) error {
data := &CephStatus{}
if err := json.Unmarshal([]byte(input), data); err != nil {
return fmt.Errorf("failed to parse json: '%s': %v", input, err)
return fmt.Errorf("failed to parse json: %q: %w", input, err)
}

decoders := []func(telegraf.Accumulator, *CephStatus) error{
Expand Down Expand Up @@ -539,7 +539,7 @@ type CephDf struct {
func decodeDf(acc telegraf.Accumulator, input string) error {
data := &CephDf{}
if err := json.Unmarshal([]byte(input), data); err != nil {
return fmt.Errorf("failed to parse json: '%s': %v", input, err)
return fmt.Errorf("failed to parse json: %q: %w", input, err)
}

// ceph.usage: records global utilization and number of objects
Expand Down Expand Up @@ -618,7 +618,7 @@ type CephOSDPoolStats []struct {
func decodeOsdPoolStats(acc telegraf.Accumulator, input string) error {
data := CephOSDPoolStats{}
if err := json.Unmarshal([]byte(input), &data); err != nil {
return fmt.Errorf("failed to parse json: '%s': %v", input, err)
return fmt.Errorf("failed to parse json: %q: %w", input, err)
}

// ceph.pool.stats: records pre pool IO and recovery throughput
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/chrony/chrony.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (c *Chrony) Gather(acc telegraf.Accumulator) error {
cmd := execCommand(c.path, flags...)
out, err := internal.CombinedOutputTimeout(cmd, time.Second*5)
if err != nil {
return fmt.Errorf("failed to run command %s: %s - %s", strings.Join(cmd.Args, " "), err, string(out))
return fmt.Errorf("failed to run command %q: %w - %s", strings.Join(cmd.Args, " "), err, string(out))
}
fields, tags, err := processChronycOutput(string(out))
if err != nil {
Expand Down
Loading

0 comments on commit a8300ab

Please sign in to comment.