Skip to content

Commit

Permalink
chore: Fix linter findings for errorlint (part1) (#12701)
Browse files Browse the repository at this point in the history
Co-authored-by: Pawel Zak <Pawel Zak>
  • Loading branch information
zak-pawel authored Feb 22, 2023
1 parent 6e72fee commit f7949ca
Show file tree
Hide file tree
Showing 19 changed files with 59 additions and 60 deletions.
15 changes: 5 additions & 10 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,36 +196,31 @@ func (a *Agent) initPlugins() error {
}
err := input.Init()
if err != nil {
return fmt.Errorf("could not initialize input %s: %v",
input.LogName(), err)
return fmt.Errorf("could not initialize input %s: %w", input.LogName(), err)
}
}
for _, processor := range a.Config.Processors {
err := processor.Init()
if err != nil {
return fmt.Errorf("could not initialize processor %s: %v",
processor.LogName(), err)
return fmt.Errorf("could not initialize processor %s: %w", processor.LogName(), err)
}
}
for _, aggregator := range a.Config.Aggregators {
err := aggregator.Init()
if err != nil {
return fmt.Errorf("could not initialize aggregator %s: %v",
aggregator.LogName(), err)
return fmt.Errorf("could not initialize aggregator %s: %w", aggregator.LogName(), err)
}
}
for _, processor := range a.Config.AggProcessors {
err := processor.Init()
if err != nil {
return fmt.Errorf("could not initialize processor %s: %v",
processor.LogName(), err)
return fmt.Errorf("could not initialize processor %s: %w", processor.LogName(), err)
}
}
for _, output := range a.Config.Outputs {
err := output.Init()
if err != nil {
return fmt.Errorf("could not initialize output %s: %v",
output.LogName(), err)
return fmt.Errorf("could not initialize output %s: %w", output.LogName(), err)
}
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/telegraf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func runApp(args []string, outputBuffer io.Writer, pprof Server, c TelegrafConfi
err := PrintInputConfig(cCtx.String("usage"), outputBuffer)
err2 := PrintOutputConfig(cCtx.String("usage"), outputBuffer)
if err != nil && err2 != nil {
return fmt.Errorf("%s and %s", err, err2)
return fmt.Errorf("%w and %w", err, err2)
}
return nil
// DEPRECATED
Expand Down
6 changes: 3 additions & 3 deletions cmd/telegraf/telegraf.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ func (t *Telegraf) reloadLoop() error {
}
}()

err := t.runAgent(ctx, cfg)
if err != nil && err != context.Canceled {
return fmt.Errorf("[telegraf] Error running agent: %v", err)
err = t.runAgent(ctx, cfg)
if err != nil && !errors.Is(err, context.Canceled) {
return fmt.Errorf("[telegraf] Error running agent: %w", err)
}
}

Expand Down
16 changes: 8 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ func (c *Config) LoadAll(configFiles ...string) error {
func (c *Config) LoadConfigData(data []byte) error {
tbl, err := parseConfig(data)
if err != nil {
return fmt.Errorf("error parsing data: %s", err)
return fmt.Errorf("error parsing data: %w", err)
}

// Parse tags tables first:
Expand All @@ -484,7 +484,7 @@ func (c *Config) LoadConfigData(data []byte) error {
return fmt.Errorf("invalid configuration, bad table name %q", tableName)
}
if err = c.toml.UnmarshalTable(subTable, c.Tags); err != nil {
return fmt.Errorf("error parsing table name %q: %s", tableName, err)
return fmt.Errorf("error parsing table name %q: %w", tableName, err)
}
}
}
Expand Down Expand Up @@ -614,7 +614,7 @@ func (c *Config) LoadConfigData(data []byte) error {
case []*ast.Table:
for _, t := range pluginSubTable {
if err = c.addAggregator(pluginName, t); err != nil {
return fmt.Errorf("error parsing %s, %s", pluginName, err)
return fmt.Errorf("error parsing %s, %w", pluginName, err)
}
}
default:
Expand All @@ -632,7 +632,7 @@ func (c *Config) LoadConfigData(data []byte) error {
case []*ast.Table:
for _, t := range pluginSubTable {
if err = c.addSecretStore(pluginName, t); err != nil {
return fmt.Errorf("error parsing %s, %s", pluginName, err)
return fmt.Errorf("error parsing %s, %w", pluginName, err)
}
}
default:
Expand All @@ -648,7 +648,7 @@ func (c *Config) LoadConfigData(data []byte) error {
// identifiers are present
default:
if err = c.addInput(name, subTable); err != nil {
return fmt.Errorf("error parsing %s, %s", name, err)
return fmt.Errorf("error parsing %s, %w", name, err)
}
}
}
Expand Down Expand Up @@ -726,7 +726,7 @@ func fetchConfig(u *url.URL) ([]byte, error) {
body, err, retry := func() ([]byte, error, bool) {
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("retry %d of %d failed connecting to HTTP config server %s", i, retries, err), false
return nil, fmt.Errorf("retry %d of %d failed connecting to HTTP config server: %w", i, retries, err), false
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
Expand Down Expand Up @@ -871,13 +871,13 @@ func (c *Config) LinkSecrets() error {
}
resolver, err := store.GetResolver(key)
if err != nil {
return fmt.Errorf("retrieving resolver for %q failed: %v", ref, err)
return fmt.Errorf("retrieving resolver for %q failed: %w", ref, err)
}
resolvers[ref] = resolver
}
// Inject the resolver list into the secret
if err := s.Link(resolvers); err != nil {
return fmt.Errorf("retrieving resolver failed: %v", err)
return fmt.Errorf("retrieving resolver failed: %w", err)
}
}
return nil
Expand Down
8 changes: 4 additions & 4 deletions config/deprecation.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ func (di *DeprecationInfo) determineEscalation(telegrafVersion *semver.Version)

since, err := semver.NewVersion(di.info.Since)
if err != nil {
return fmt.Errorf("cannot parse 'since' version %q: %v", di.info.Since, err)
return fmt.Errorf("cannot parse 'since' version %q: %w", di.info.Since, err)
}

var removal *semver.Version
if di.info.RemovalIn != "" {
removal, err = semver.NewVersion(di.info.RemovalIn)
if err != nil {
return fmt.Errorf("cannot parse 'removal' version %q: %v", di.info.RemovalIn, err)
return fmt.Errorf("cannot parse 'removal' version %q: %w", di.info.RemovalIn, err)
}
} else {
removal = &semver.Version{Major: since.Major}
Expand Down Expand Up @@ -116,7 +116,7 @@ func (c *Config) collectDeprecationInfo(category, name string, plugin interface{
}
}
if err := info.determineEscalation(c.version); err != nil {
panic(fmt.Errorf("plugin %q: %v", info.Name, err))
panic(fmt.Errorf("plugin %q: %w", info.Name, err))
}
if info.LogLevel != telegraf.None {
c.incrementPluginDeprecations(category)
Expand Down Expand Up @@ -148,7 +148,7 @@ func (c *Config) collectDeprecationInfo(category, name string, plugin interface{
optionInfo.info.RemovalIn = tags[1]
}
if err := optionInfo.determineEscalation(c.version); err != nil {
panic(fmt.Errorf("plugin %q option %q: %v", info.Name, field.Name, err))
panic(fmt.Errorf("plugin %q option %q: %w", info.Name, field.Name, err))
}

if optionInfo.LogLevel != telegraf.None {
Expand Down
6 changes: 3 additions & 3 deletions config/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (s *Secret) EqualTo(ref []byte) (bool, error) {
// Get a locked-buffer of the secret to perform the comparison
lockbuf, err := s.enclave.Open()
if err != nil {
return false, fmt.Errorf("opening enclave failed: %v", err)
return false, fmt.Errorf("opening enclave failed: %w", err)
}
defer lockbuf.Destroy()

Expand All @@ -127,7 +127,7 @@ func (s *Secret) Get() ([]byte, error) {
// Decrypt the secret so we can return it
lockbuf, err := s.enclave.Open()
if err != nil {
return nil, fmt.Errorf("opening enclave failed: %v", err)
return nil, fmt.Errorf("opening enclave failed: %w", err)
}
defer lockbuf.Destroy()
secret := lockbuf.Bytes()
Expand Down Expand Up @@ -179,7 +179,7 @@ func (s *Secret) Link(resolvers map[string]telegraf.ResolveFunc) error {
}
lockbuf, err := s.enclave.Open()
if err != nil {
return fmt.Errorf("opening enclave failed: %v", err)
return fmt.Errorf("opening enclave failed: %w", err)
}
defer lockbuf.Destroy()
secret := lockbuf.Bytes()
Expand Down
6 changes: 3 additions & 3 deletions internal/content_coding.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (r *GzipReader) Read(b []byte) (int, error) {

// Since multistream is disabled, io.EOF indicates the end of the gzip
// sequence. On the next read we must read the next gzip header.
if err == io.EOF {
if errors.Is(err, io.EOF) {
r.endOfStream = true
return n, nil
}
Expand Down Expand Up @@ -225,7 +225,7 @@ func (d *GzipDecoder) Decode(data []byte) ([]byte, error) {
d.buf.Reset()

_, err = d.buf.ReadFrom(d.reader)
if err != nil && err != io.EOF {
if err != nil && !errors.Is(err, io.EOF) {
return nil, err
}
err = d.reader.Close()
Expand Down Expand Up @@ -256,7 +256,7 @@ func (d *ZlibDecoder) Decode(data []byte) ([]byte, error) {
return nil, err
}
_, err = io.Copy(d.buf, r)
if err != nil && err != io.EOF {
if err != nil && !errors.Is(err, io.EOF) {
return nil, err
}
err = r.Close()
Expand Down
4 changes: 3 additions & 1 deletion internal/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package internal

import (
"crypto/subtle"
"errors"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -135,7 +136,8 @@ func OnClientError(client *http.Client, err error) {
// connection this ensures that next interval a new connection will be
// used and name lookup will be performed.
// https://github.com/golang/go/issues/36026
if err, ok := err.(*url.Error); ok && err.Timeout() {
var urlErr *url.Error
if errors.As(err, &urlErr) && urlErr.Timeout() {
client.CloseIdleConnections()
}
}
5 changes: 3 additions & 2 deletions internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,9 @@ func AlignTime(tm time.Time, interval time.Duration) time.Time {
// and returns the exit status and true
// if error is not exit status, will return 0 and false
func ExitStatus(err error) (int, bool) {
if exiterr, ok := err.(*exec.ExitError); ok {
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
if status, ok := exitErr.Sys().(syscall.WaitStatus); ok {
return status.ExitStatus(), true
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (p *Process) cmdStart() error {
p.Log.Infof("Starting process: %s %s", p.name, p.args)

if err := p.Cmd.Start(); err != nil {
return fmt.Errorf("error starting process: %s", err)
return fmt.Errorf("error starting process: %w", err)
}
atomic.StoreInt32(&p.pid, int32(p.Cmd.Process.Pid))
return nil
Expand Down
2 changes: 1 addition & 1 deletion internal/snmp/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func walkPaths(paths []string, log telegraf.Logger) ([]string, error) {
return nil
})
if err != nil {
return folders, fmt.Errorf("couldn't walk path %q: %v", mibPath, err)
return folders, fmt.Errorf("couldn't walk path %q: %w", mibPath, err)
}
}
return folders, nil
Expand Down
16 changes: 8 additions & 8 deletions models/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,41 +54,41 @@ func (f *Filter) Compile() error {
var err error
f.nameDropFilter, err = filter.Compile(f.NameDrop)
if err != nil {
return fmt.Errorf("error compiling 'namedrop', %s", err)
return fmt.Errorf("error compiling 'namedrop', %w", err)
}
f.namePassFilter, err = filter.Compile(f.NamePass)
if err != nil {
return fmt.Errorf("error compiling 'namepass', %s", err)
return fmt.Errorf("error compiling 'namepass', %w", err)
}

f.fieldDropFilter, err = filter.Compile(f.FieldDrop)
if err != nil {
return fmt.Errorf("error compiling 'fielddrop', %s", err)
return fmt.Errorf("error compiling 'fielddrop', %w", err)
}
f.fieldPassFilter, err = filter.Compile(f.FieldPass)
if err != nil {
return fmt.Errorf("error compiling 'fieldpass', %s", err)
return fmt.Errorf("error compiling 'fieldpass', %w", err)
}

f.tagExcludeFilter, err = filter.Compile(f.TagExclude)
if err != nil {
return fmt.Errorf("error compiling 'tagexclude', %s", err)
return fmt.Errorf("error compiling 'tagexclude', %w", err)
}
f.tagIncludeFilter, err = filter.Compile(f.TagInclude)
if err != nil {
return fmt.Errorf("error compiling 'taginclude', %s", err)
return fmt.Errorf("error compiling 'taginclude', %w", err)
}

for i := range f.TagDropFilters {
f.TagDropFilters[i].filter, err = filter.Compile(f.TagDropFilters[i].Values)
if err != nil {
return fmt.Errorf("error compiling 'tagdrop', %s", err)
return fmt.Errorf("error compiling 'tagdrop', %w", err)
}
}
for i := range f.TagPassFilters {
f.TagPassFilters[i].filter, err = filter.Compile(f.TagPassFilters[i].Values)
if err != nil {
return fmt.Errorf("error compiling 'tagpass', %s", err)
return fmt.Errorf("error compiling 'tagpass', %w", err)
}
}
return nil
Expand Down
8 changes: 4 additions & 4 deletions testutil/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (c *Container) Start() error {

container, err := testcontainers.GenericContainer(c.ctx, req)
if err != nil {
return fmt.Errorf("container failed to start: %s", err)
return fmt.Errorf("container failed to start: %w", err)
}
c.container = container

Expand All @@ -73,15 +73,15 @@ func (c *Container) Start() error {
c.container.FollowOutput(&c.Logs)
err = c.container.StartLogProducer(c.ctx)
if err != nil {
return fmt.Errorf("log producer failed: %s", err)
return fmt.Errorf("log producer failed: %w", err)
}

c.Address = "localhost"

err = c.LookupMappedPorts()
if err != nil {
c.Terminate()
return fmt.Errorf("port lookup failed: %s", err)
return fmt.Errorf("port lookup failed: %w", err)
}

return nil
Expand Down Expand Up @@ -110,7 +110,7 @@ func (c *Container) LookupMappedPorts() error {

p, err := c.container.MappedPort(c.ctx, nat.Port(port))
if err != nil {
return fmt.Errorf("failed to find '%s' - %s", port, err)
return fmt.Errorf("failed to find %q: %w", port, err)
}
fmt.Printf("mapped container port '%s' to host port '%s'\n", port, p.Port())
c.Ports[port] = p.Port()
Expand Down
4 changes: 2 additions & 2 deletions testutil/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func ParseMetricsFrom(lines []string, header string, parser LineParser) ([]teleg

m, err := parser.ParseLine(content)
if err != nil {
return nil, fmt.Errorf("unable to parse metric in %q failed: %v", content, err)
return nil, fmt.Errorf("unable to parse metric in %q failed: %w", content, err)
}
metrics = append(metrics, m)
}
Expand All @@ -104,7 +104,7 @@ func ParseMetricsFromFile(filename string, parser telegraf.Parser) ([]telegraf.M

nonutc, err := parser.Parse(line)
if err != nil {
return nil, fmt.Errorf("unable to parse metric in %q failed: %v", line, err)
return nil, fmt.Errorf("unable to parse metric in %q failed: %w", line, err)
}
for _, m := range nonutc {
// The timezone needs to be UTC to match the timestamp test results
Expand Down
4 changes: 2 additions & 2 deletions tools/custom_builder/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ func (s *selection) importFiles(configurations []string) error {
for _, cfg := range configurations {
buf, err := config.LoadConfigFile(cfg)
if err != nil {
return fmt.Errorf("reading %q failed: %v", cfg, err)
return fmt.Errorf("reading %q failed: %w", cfg, err)
}

if err := s.extractPluginsFromConfig(buf); err != nil {
return fmt.Errorf("extracting plugins from %q failed: %v", cfg, err)
return fmt.Errorf("extracting plugins from %q failed: %w", cfg, err)
}
}

Expand Down
Loading

0 comments on commit f7949ca

Please sign in to comment.