Skip to content

Commit

Permalink
Fix new linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
srebhan committed Feb 13, 2025
1 parent b97f0a1 commit c9c1cac
Show file tree
Hide file tree
Showing 21 changed files with 62 additions and 72 deletions.
5 changes: 2 additions & 3 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -740,8 +740,7 @@ func (a *Agent) runAggregators(
log.Printf("D! [agent] Aggregator channel closed")
}

func updateWindow(start time.Time, roundInterval bool, period time.Duration) (time.Time, time.Time) {
var until time.Time
func updateWindow(start time.Time, roundInterval bool, period time.Duration) (since, until time.Time) {
if roundInterval {
until = internal.AlignTime(start, period)
if until.Equal(start) {
Expand All @@ -751,7 +750,7 @@ func updateWindow(start time.Time, roundInterval bool, period time.Duration) (ti
until = start.Add(period)
}

since := until.Add(-period)
since = until.Add(-period)

return since, until
}
Expand Down
18 changes: 9 additions & 9 deletions plugins/common/shim/goshim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ func TestShimSetsUpLogger(t *testing.T) {
require.NoError(t, err)
}

func runErroringInputPlugin(t *testing.T, interval time.Duration, stdin io.Reader, stdout, stderr io.Writer) (chan bool, chan bool) {
metricProcessed := make(chan bool, 1)
exited := make(chan bool, 1)
func runErroringInputPlugin(t *testing.T, interval time.Duration, stdin io.Reader, stdout, stderr io.Writer) (processed, exited chan bool) {
processed = make(chan bool, 1)
exited = make(chan bool, 1)
inp := &erroringInput{}

shim := New()
Expand All @@ -47,15 +47,15 @@ func runErroringInputPlugin(t *testing.T, interval time.Duration, stdin io.Reade
shim.stderr = stderr
logger.RedirectLogging(stderr)
}
err := shim.AddInput(inp)
require.NoError(t, err)
go func() {

require.NoError(t, shim.AddInput(inp))
go func(e chan bool) {
if err := shim.Run(interval); err != nil {
t.Error(err)
}
exited <- true
}()
return metricProcessed, exited
e <- true
}(exited)
return processed, exited
}

type erroringInput struct {
Expand Down
16 changes: 8 additions & 8 deletions plugins/common/shim/input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ func TestInputShimStdinSignalingWorks(t *testing.T) {
<-exited
}

func runInputPlugin(t *testing.T, interval time.Duration, stdin io.Reader, stdout, stderr io.Writer) (chan bool, chan bool) {
metricProcessed := make(chan bool, 1)
exited := make(chan bool, 1)
func runInputPlugin(t *testing.T, interval time.Duration, stdin io.Reader, stdout, stderr io.Writer) (processed, exited chan bool) {
processed = make(chan bool, 1)
exited = make(chan bool, 1)
inp := &testInput{
metricProcessed: metricProcessed,
metricProcessed: processed,
}

shim := New()
Expand All @@ -74,13 +74,13 @@ func runInputPlugin(t *testing.T, interval time.Duration, stdin io.Reader, stdou
}
err := shim.AddInput(inp)
require.NoError(t, err)
go func() {
go func(e chan bool) {
if err := shim.Run(interval); err != nil {
t.Error(err)
}
exited <- true
}()
return metricProcessed, exited
e <- true
}(exited)
return processed, exited
}

type testInput struct {
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/apcupsd/apcupsd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func TestApcupsdGather(t *testing.T) {
// The following functionality is straight from apcupsd tests.

// kvBytes is a helper to generate length and key/value byte buffers.
func kvBytes(kv string) ([]byte, []byte) {
func kvBytes(kv string) (keyValLen, keyVal []byte) {
lenb := make([]byte, 2)
binary.BigEndian.PutUint16(lenb, uint16(len(kv)))

Expand Down
1 change: 0 additions & 1 deletion plugins/inputs/beanstalkd/beanstalkd.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ func (b *Beanstalkd) gatherTubeStats(connection *textproto.Conn, tube string, ac
}

func runQuery(connection *textproto.Conn, cmd string, result interface{}) error {
//nolint:govet // Keep dynamic command as the passed string is constant
requestID, err := connection.Cmd(cmd)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/exec/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func newRunnerMock(out, errout []byte, err error) runner {
}
}

func (r runnerMock) run(_ string, _ []string, _ time.Duration) ([]byte, []byte, error) {
func (r runnerMock) run(string, []string, time.Duration) (out, errout []byte, err error) {
return r.out, r.errout, r.err
}

Expand Down
10 changes: 5 additions & 5 deletions plugins/inputs/exec/run_notwindows.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (c commandRunner) run(
command string,
environments []string,
timeout time.Duration,
) ([]byte, []byte, error) {
) (out, errout []byte, err error) {
splitCmd, err := shellquote.Split(command)
if err != nil || len(splitCmd) == 0 {
return nil, nil, fmt.Errorf("exec: unable to parse command: %w", err)
Expand All @@ -33,19 +33,19 @@ func (c commandRunner) run(
}

var (
out bytes.Buffer
outbuf bytes.Buffer
stderr bytes.Buffer
)
cmd.Stdout = &out
cmd.Stdout = &outbuf
cmd.Stderr = &stderr

runErr := internal.RunTimeout(cmd, timeout)

out = removeWindowsCarriageReturns(out)
outbuf = removeWindowsCarriageReturns(outbuf)
if stderr.Len() > 0 && !c.debug {
stderr = removeWindowsCarriageReturns(stderr)
stderr = truncate(stderr)
}

return out.Bytes(), stderr.Bytes(), runErr
return outbuf.Bytes(), stderr.Bytes(), runErr
}
16 changes: 8 additions & 8 deletions plugins/inputs/execd/shim/shim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ func TestShimStdinSignalingWorks(t *testing.T) {
<-exited
}

func runInputPlugin(t *testing.T, interval time.Duration, stdin io.Reader, stdout, stderr io.Writer) (chan bool, chan bool) {
metricProcessed := make(chan bool)
exited := make(chan bool)
func runInputPlugin(t *testing.T, interval time.Duration, stdin io.Reader, stdout, stderr io.Writer) (processed, exited chan bool) {
processed = make(chan bool)
exited = make(chan bool)
inp := &testInput{
metricProcessed: metricProcessed,
metricProcessed: processed,
}

shim := New()
Expand All @@ -72,13 +72,13 @@ func runInputPlugin(t *testing.T, interval time.Duration, stdin io.Reader, stdou
}

require.NoError(t, shim.AddInput(inp))
go func() {
go func(e chan bool) {
if err := shim.Run(interval); err != nil {
t.Error(err)
}
exited <- true
}()
return metricProcessed, exited
e <- true
}(exited)
return processed, exited
}

type testInput struct {
Expand Down
3 changes: 1 addition & 2 deletions plugins/inputs/ipmi_sensor/ipmi_sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,8 @@ func (m *Ipmi) parse(acc telegraf.Accumulator, server, sensor string) error {
case "sdr":
if m.MetricVersion == 2 {
return m.parseV2(acc, hostname, out, timestamp)
} else {
return m.parseV1(acc, hostname, out, timestamp)
}
return m.parseV1(acc, hostname, out, timestamp)
case "chassis_power_status":
return parseChassisPowerStatus(acc, hostname, out, timestamp)
case "dcmi_power_reading":
Expand Down
5 changes: 2 additions & 3 deletions plugins/inputs/nginx_plus_api/nginx_plus_api_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1534,13 +1534,12 @@ func TestUnknownContentType(t *testing.T) {
require.Error(t, acc.FirstError())
}

func prepareAddr(t *testing.T, ts *httptest.Server) (*url.URL, string, string) {
func prepareAddr(t *testing.T, ts *httptest.Server) (addr *url.URL, host, port string) {
t.Helper()
addr, err := url.Parse(ts.URL + "/api")
require.NoError(t, err)

host, port, err := net.SplitHostPort(addr.Host)

host, port, err = net.SplitHostPort(addr.Host)
if err != nil {
host = addr.Host
if addr.Scheme == "http" {
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/smart/smart.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ func (m *Smart) Gather(acc telegraf.Accumulator) error {
return nil
}

func (m *Smart) scanAllDevices(ignoreExcludes bool) ([]string, []string, error) {
func (m *Smart) scanAllDevices(ignoreExcludes bool) (nvme, nonNvme []string, err error) {
// this will return all devices (including NVMe devices) for smartctl version >= 7.0
// for older versions this will return non NVMe devices
devices, err := m.scanDevices(ignoreExcludes, "--scan")
Expand Down
6 changes: 3 additions & 3 deletions plugins/inputs/stackdriver/stackdriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,16 +192,16 @@ func (s *Stackdriver) initializeStackdriverClient(ctx context.Context) error {
}

// Returns the start and end time for the next collection.
func (s *Stackdriver) updateWindow(prevEnd time.Time) (time.Time, time.Time) {
var start time.Time
func (s *Stackdriver) updateWindow(prevEnd time.Time) (start, end time.Time) {
if time.Duration(s.Window) != 0 {
start = time.Now().Add(-time.Duration(s.Delay)).Add(-time.Duration(s.Window))
} else if prevEnd.IsZero() {
start = time.Now().Add(-time.Duration(s.Delay)).Add(-time.Duration(defaultWindow))
} else {
start = prevEnd
}
end := time.Now().Add(-time.Duration(s.Delay))
end = time.Now().Add(-time.Duration(s.Delay))

return start, end
}

Expand Down
6 changes: 2 additions & 4 deletions plugins/inputs/tail/tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,14 @@ func (t *Tail) getSeekInfo(file string) (*tail.SeekInfo, error) {
if offset, ok := t.offsets[file]; ok {
t.Log.Debugf("Using offset %d for %q", offset, file)
return &tail.SeekInfo{Whence: 0, Offset: offset}, nil
} else {
return &tail.SeekInfo{Whence: 2, Offset: 0}, nil
}
return &tail.SeekInfo{Whence: 2, Offset: 0}, nil
case "save-or-beginning":
if offset, ok := t.offsets[file]; ok {
t.Log.Debugf("Using offset %d for %q", offset, file)
return &tail.SeekInfo{Whence: 0, Offset: offset}, nil
} else {
return &tail.SeekInfo{Whence: 0, Offset: 0}, nil
}
return &tail.SeekInfo{Whence: 0, Offset: 0}, nil
default:
return nil, errors.New("invalid 'initial_read_offset' setting")
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/varnish/varnish.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (s *Varnish) Gather(acc telegraf.Accumulator) error {
}

// Prepare varnish cli tools arguments
func (s *Varnish) prepareCmdArgs() ([]string, []string) {
func (s *Varnish) prepareCmdArgs() (adm, stats []string) {
// default varnishadm arguments
admArgs := []string{"vcl.list", "-j"}

Expand Down
6 changes: 3 additions & 3 deletions plugins/inputs/webhooks/artifactory/artifactory_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ func (awh *Webhook) newEvent(data []byte, et, ed string) (event, error) {
case "artifact":
if et == "deployed" || et == "deleted" {
return generateEvent(data, &artifactDeploymentOrDeletedEvent{})
} else if et == "moved" || et == "copied" {
}
if et == "moved" || et == "copied" {
return generateEvent(data, &artifactMovedOrCopiedEvent{})
} else {
return nil, &newEventError{"Not a recognized event type"}
}
return nil, &newEventError{"Not a recognized event type"}
case "artifact_property":
return generateEvent(data, &artifactPropertiesEvent{})
case "docker":
Expand Down
6 changes: 3 additions & 3 deletions plugins/inputs/zfs/zfs_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ func getTags(pools []poolInfo) map[string]string {
return map[string]string{"pools": poolNames}
}

func gather(lines []string, fileLines int) ([]string, []string, error) {
func gather(lines []string, fileLines int) (keys, values []string, err error) {
if len(lines) < fileLines {
return nil, nil, errors.New("expected lines in kstat does not match")
}

keys := strings.Fields(lines[1])
values := strings.Fields(lines[2])
keys = strings.Fields(lines[1])
values = strings.Fields(lines[2])
if len(keys) != len(values) {
return nil, nil, fmt.Errorf("key and value count don't match Keys:%v Values:%v", keys, values)
}
Expand Down
6 changes: 3 additions & 3 deletions plugins/inputs/zipkin/codec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ func NewBinaryAnnotations(annotations []BinaryAnnotation, endpoint Endpoint) []t
return formatted
}

func minMax(span Span) (time.Time, time.Time) {
low := now().UTC()
high := time.Time{}.UTC()
func minMax(span Span) (low, high time.Time) {
low = now().UTC()
high = time.Time{}.UTC()
for _, annotation := range span.Annotations() {
ts := annotation.Timestamp()
if !ts.IsZero() && ts.Before(low) {
Expand Down
2 changes: 1 addition & 1 deletion plugins/outputs/stackdriver/stackdriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ func getStackdriverIntervalEndpoints(
m telegraf.Metric,
f *telegraf.Field,
cc *counterCache,
) (*timestamppb.Timestamp, *timestamppb.Timestamp) {
) (start, end *timestamppb.Timestamp) {
endTime := timestamppb.New(m.Time())
var startTime *timestamppb.Timestamp
if kind == metricpb.MetricDescriptor_CUMULATIVE {
Expand Down
10 changes: 3 additions & 7 deletions plugins/parsers/json_v2/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,6 @@ func (p *Parser) expandArray(result metricNode, timestamp time.Time) ([]telegraf
}

if result.IsArray() {
var err error
if result.IncludeCollection == nil && (len(p.objectConfig.FieldPaths) > 0 || len(p.objectConfig.TagPaths) > 0) {
result.IncludeCollection = p.existsInpathResults(result.Index)
}
Expand Down Expand Up @@ -380,9 +379,6 @@ func (p *Parser) expandArray(result metricNode, timestamp time.Time) ([]telegraf
results = append(results, r...)
return true
})
if err != nil {
return nil, err
}
} else {
if p.objectConfig.TimestampKey != "" && result.SetName == p.objectConfig.TimestampKey {
if p.objectConfig.TimestampFormat == "" {
Expand Down Expand Up @@ -714,11 +710,11 @@ func convertType(input gjson.Result, desiredType, name string) (interface{}, err
case "bool":
if inputType == 0 {
return false, nil
} else if inputType == 1 {
}
if inputType == 1 {
return true, nil
} else {
return nil, fmt.Errorf("unable to convert field %q to type bool", name)
}
return nil, fmt.Errorf("unable to convert field %q to type bool", name)
}
default:
return nil, fmt.Errorf("unknown format '%T' for field %q", inputType, name)
Expand Down
6 changes: 3 additions & 3 deletions plugins/processors/converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,12 @@ func (p *Converter) convertTags(metric telegraf.Metric) {
metric.AddField(key, v)
}
case p.tagConversions.Timestamp != nil && p.tagConversions.Timestamp.Match(key):
if time, err := internal.ParseTimestamp(p.Tags.TimestampFormat, value, nil); err != nil {
time, err := internal.ParseTimestamp(p.Tags.TimestampFormat, value, nil)
if err != nil {
p.Log.Errorf("Converting to timestamp [%T] failed: %v", value, err)
continue
} else {
metric.SetTime(time)
}
metric.SetTime(time)
default:
continue
}
Expand Down
4 changes: 2 additions & 2 deletions plugins/secretstores/http/key_derivation.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (k *KDFConfig) NewKey(keylen int) (key, iv config.Secret, err error) {
return config.Secret{}, config.Secret{}, fmt.Errorf("unknown key-derivation function %q", k.Algorithm)
}

func (k *KDFConfig) generatePBKDF2HMAC(hf hashFunc, keylen int) (config.Secret, config.Secret, error) {
func (k *KDFConfig) generatePBKDF2HMAC(hf hashFunc, keylen int) (key, iv config.Secret, err error) {
if k.Iterations == 0 {
return config.Secret{}, config.Secret{}, errors.New("'iteration value not set")
}
Expand All @@ -48,6 +48,6 @@ func (k *KDFConfig) generatePBKDF2HMAC(hf hashFunc, keylen int) (config.Secret,
defer salt.Destroy()

rawkey := pbkdf2.Key(passwd.Bytes(), salt.Bytes(), k.Iterations, keylen, hf)
key := config.NewSecret([]byte(hex.EncodeToString(rawkey)))
key = config.NewSecret([]byte(hex.EncodeToString(rawkey)))
return key, config.Secret{}, nil
}

0 comments on commit c9c1cac

Please sign in to comment.