Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Fix linter findings for revive:enforce-slice-style in plugins/parsers, plugins/processors, plugins/secretstores and plugins/serializers #15980

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugins/parsers/collectd/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (p *Parser) Parse(buf []byte) ([]telegraf.Metric, error) {
return nil, fmt.Errorf("collectd parser error: %w", err)
}

metrics := []telegraf.Metric{}
metrics := make([]telegraf.Metric, 0, len(valueLists))
for _, valueList := range valueLists {
metrics = append(metrics, p.unmarshalValueList(valueList)...)
}
Expand Down
6 changes: 3 additions & 3 deletions plugins/parsers/collectd/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func TestParse_SignSecurityLevel(t *testing.T) {

metrics, err = parser.Parse(bytes)
require.NoError(t, err)
require.Equal(t, []telegraf.Metric{}, metrics)
require.Empty(t, metrics)

// Wrong password error
buf, err = writeValueList(singleMetric.vl)
Expand Down Expand Up @@ -250,7 +250,7 @@ func TestParse_EncryptSecurityLevel(t *testing.T) {

metrics, err := parser.Parse(bytes)
require.NoError(t, err)
require.Equal(t, []telegraf.Metric{}, metrics)
require.Empty(t, metrics)

// Encrypted data
buf, err = writeValueList(singleMetric.vl)
Expand All @@ -271,7 +271,7 @@ func TestParse_EncryptSecurityLevel(t *testing.T) {

metrics, err = parser.Parse(bytes)
require.NoError(t, err)
require.Equal(t, []telegraf.Metric{}, metrics)
require.Empty(t, metrics)

// Wrong password error
buf, err = writeValueList(singleMetric.vl)
Expand Down
3 changes: 1 addition & 2 deletions plugins/parsers/csv/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ func (record metadataPattern) Less(i, j int) bool {
func (p *Parser) initializeMetadataSeparators() error {
// initialize metadata
p.metadataTags = map[string]string{}
p.metadataSeparatorList = []string{}

if p.MetadataRows <= 0 {
return nil
Expand All @@ -94,7 +93,7 @@ func (p *Parser) initializeMetadataSeparators() error {
return errors.New("csv_metadata_separators required when specifying csv_metadata_rows")
}

p.metadataSeparatorList = metadataPattern{}
p.metadataSeparatorList = make(metadataPattern, 0, len(p.MetadataSeparators))
patternList := map[string]bool{}
for _, pattern := range p.MetadataSeparators {
if patternList[pattern] {
Expand Down
6 changes: 3 additions & 3 deletions plugins/parsers/csv/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestHeaderOverride(t *testing.T) {
require.NoError(t, err)
metrics, err = p.Parse([]byte(testCSVRows[0]))
require.NoError(t, err)
require.Equal(t, []telegraf.Metric{}, metrics)
require.Empty(t, metrics)
m, err := p.ParseLine(testCSVRows[1])
require.NoError(t, err)
require.Equal(t, "test_name", m.Name())
Expand Down Expand Up @@ -849,14 +849,14 @@ func TestParseMetadataSeparators(t *testing.T) {
p := &Parser{
ColumnNames: []string{"a", "b"},
MetadataRows: 0,
MetadataSeparators: []string{},
MetadataSeparators: make([]string, 0),
srebhan marked this conversation as resolved.
Show resolved Hide resolved
}
err := p.Init()
require.NoError(t, err)
p = &Parser{
ColumnNames: []string{"a", "b"},
MetadataRows: 1,
MetadataSeparators: []string{},
MetadataSeparators: make([]string, 0),
}
err = p.Init()
require.Error(t, err)
Expand Down
2 changes: 1 addition & 1 deletion plugins/parsers/influx/influx_upstream/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func (sp *StreamParser) Next() (telegraf.Metric, error) {

m, err := nextMetric(sp.decoder, sp.precision, sp.defaultTime, false)
if err != nil {
return nil, convertToParseError([]byte{}, err)
return nil, convertToParseError(nil, err)
}

return m, nil
Expand Down
15 changes: 6 additions & 9 deletions plugins/parsers/influx/influx_upstream/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,9 +683,8 @@ func TestSeriesParser(t *testing.T) {
err error
}{
{
name: "empty",
input: []byte(""),
metrics: []telegraf.Metric{},
name: "empty",
input: []byte(""),
},
{
name: "minimal",
Expand Down Expand Up @@ -715,9 +714,8 @@ func TestSeriesParser(t *testing.T) {
},
},
{
name: "missing tag value",
input: []byte("cpu,a="),
metrics: []telegraf.Metric{},
name: "missing tag value",
input: []byte("cpu,a="),
err: &ParseError{
DecodeError: &lineprotocol.DecodeError{
Line: 1,
Expand All @@ -728,9 +726,8 @@ func TestSeriesParser(t *testing.T) {
},
},
{
name: "error with carriage return in long line",
input: []byte("cpu,a=" + strings.Repeat("x", maxErrorBufferSize) + "\rcd,b"),
metrics: []telegraf.Metric{},
name: "error with carriage return in long line",
input: []byte("cpu,a=" + strings.Repeat("x", maxErrorBufferSize) + "\rcd,b"),
err: &ParseError{
DecodeError: &lineprotocol.DecodeError{
Line: 1,
Expand Down
15 changes: 6 additions & 9 deletions plugins/parsers/influx/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -761,9 +761,8 @@ func TestSeriesParser(t *testing.T) {
err error
}{
{
name: "empty",
input: []byte(""),
metrics: []telegraf.Metric{},
name: "empty",
input: []byte(""),
},
{
name: "minimal",
Expand Down Expand Up @@ -793,9 +792,8 @@ func TestSeriesParser(t *testing.T) {
},
},
{
name: "missing tag value",
input: []byte("cpu,a="),
metrics: []telegraf.Metric{},
name: "missing tag value",
input: []byte("cpu,a="),
err: &ParseError{
Offset: 6,
LineNumber: 1,
Expand All @@ -805,9 +803,8 @@ func TestSeriesParser(t *testing.T) {
},
},
{
name: "error with carriage return in long line",
input: []byte("cpu,a=" + strings.Repeat("x", maxErrorBufferSize) + "\rcd,b"),
metrics: []telegraf.Metric{},
name: "error with carriage return in long line",
input: []byte("cpu,a=" + strings.Repeat("x", maxErrorBufferSize) + "\rcd,b"),
err: &ParseError{
Offset: 1031,
LineNumber: 1,
Expand Down
22 changes: 9 additions & 13 deletions plugins/parsers/json/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,6 @@ func TestJSONQueryErrorOnArray(t *testing.T) {

parser := &Parser{
MetricName: "json_test",
TagKeys: []string{},
Query: "shares.myArr",
}
require.NoError(t, parser.Init())
Expand Down Expand Up @@ -910,22 +909,19 @@ func TestParse(t *testing.T) {
},
},
{
name: "parse empty array",
parser: &Parser{},
input: []byte(`[]`),
expected: []telegraf.Metric{},
name: "parse empty array",
parser: &Parser{},
input: []byte(`[]`),
},
{
name: "parse null",
parser: &Parser{},
input: []byte(`null`),
expected: []telegraf.Metric{},
name: "parse null",
parser: &Parser{},
input: []byte(`null`),
},
{
name: "parse null with query",
parser: &Parser{Query: "result.data"},
input: []byte(`{"error":null,"result":{"data":null,"items_per_page":10,"total_items":0,"total_pages":0}}`),
expected: []telegraf.Metric{},
name: "parse null with query",
parser: &Parser{Query: "result.data"},
input: []byte(`{"error":null,"result":{"data":null,"items_per_page":10,"total_items":0,"total_pages":0}}`),
},
{
name: "parse simple array",
Expand Down
5 changes: 1 addition & 4 deletions plugins/parsers/json_v2/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,7 @@ func TestMultipleConfigs(t *testing.T) {
}

func TestParserEmptyConfig(t *testing.T) {
plugin := &json_v2.Parser{
Configs: []json_v2.Config{},
}

plugin := &json_v2.Parser{}
require.ErrorContains(t, plugin.Init(), "no configuration provided")
}

Expand Down
5 changes: 0 additions & 5 deletions plugins/parsers/logfmt/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ func TestParse(t *testing.T) {
}{
{
name: "no bytes returns no metrics",
want: []telegraf.Metric{},
},
{
name: "test without trailing end",
Expand Down Expand Up @@ -104,27 +103,23 @@ func TestParse(t *testing.T) {
{
name: "keys without = or values are ignored",
bytes: []byte(`i am no data.`),
want: []telegraf.Metric{},
wantErr: false,
},
{
name: "keys without values are ignored",
bytes: []byte(`foo="" bar=`),
want: []telegraf.Metric{},
wantErr: false,
},
{
name: "unterminated quote produces error",
measurement: "testlog",
bytes: []byte(`bar=baz foo="bar`),
want: []telegraf.Metric{},
wantErr: true,
},
{
name: "malformed key",
measurement: "testlog",
bytes: []byte(`"foo=" bar=baz`),
want: []telegraf.Metric{},
wantErr: true,
},
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/parsers/nagios/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func TestTryAddState(t *testing.T) {
runErrF: func() error {
return nil
},
metrics: []telegraf.Metric{},
metrics: make([]telegraf.Metric, 0),
assertF: func(t *testing.T, metrics []telegraf.Metric) {
require.Len(t, metrics, 1)
m := metrics[0]
Expand Down
2 changes: 1 addition & 1 deletion plugins/parsers/value/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (v *Parser) Parse(buf []byte) ([]telegraf.Metric, error) {
if v.DataType != "string" {
values := strings.Fields(vStr)
if len(values) < 1 {
return []telegraf.Metric{}, nil
return make([]telegraf.Metric, 0), nil
srebhan marked this conversation as resolved.
Show resolved Hide resolved
}
vStr = values[len(values)-1]
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/parsers/xpath/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ func splitLastPathElement(query string) []string {

// Nothing left
if query == "" || query == "/" || query == "//" || query == "." {
return []string{}
return make([]string, 0)
srebhan marked this conversation as resolved.
Show resolved Hide resolved
}

separatorIdx := strings.LastIndex(query, "/")
Expand Down
2 changes: 1 addition & 1 deletion plugins/parsers/xpath/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1388,7 +1388,7 @@ func TestProtobufImporting(t *testing.T) {
ProtobufMessageDef: "person.proto",
ProtobufMessageType: "importtest.Person",
ProtobufImportPaths: []string{"testcases/protos"},
Configs: []Config{},
Configs: make([]Config, 0),
srebhan marked this conversation as resolved.
Show resolved Hide resolved
Log: testutil.Logger{Name: "parsers.protobuf"},
}
require.NoError(t, parser.Init())
Expand Down
2 changes: 1 addition & 1 deletion plugins/processors/aws_ec2/ec2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestBasicStartupWithTagCacheSize(t *testing.T) {
func TestBasicInitNoTagsReturnAnError(t *testing.T) {
p := newAwsEc2Processor()
p.Log = &testutil.Logger{}
p.ImdsTags = []string{}
p.ImdsTags = make([]string, 0)
srebhan marked this conversation as resolved.
Show resolved Hide resolved
err := p.Init()
require.Error(t, err)
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/processors/filter/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestNoMetric(t *testing.T) {
}
require.NoError(t, plugin.Init())

input := []telegraf.Metric{}
input := make([]telegraf.Metric, 0)
srebhan marked this conversation as resolved.
Show resolved Hide resolved
require.Empty(t, plugin.Apply(input...))
}

Expand Down
4 changes: 2 additions & 2 deletions plugins/processors/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ func (p *Parser) SetParser(parser telegraf.Parser) {
}

func (p *Parser) Apply(metrics ...telegraf.Metric) []telegraf.Metric {
results := []telegraf.Metric{}
results := make([]telegraf.Metric, 0, len(metrics))
for _, metric := range metrics {
newMetrics := []telegraf.Metric{}
newMetrics := make([]telegraf.Metric, 0)
srebhan marked this conversation as resolved.
Show resolved Hide resolved
if !p.DropOriginal {
newMetrics = append(newMetrics, metric)
} else {
Expand Down
4 changes: 2 additions & 2 deletions plugins/processors/reverse_dns/rdnscache.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func NewReverseDNSCache(ttl, lookupTimeout time.Duration, workerPoolSize int) *R
ttl: ttl,
lookupTimeout: lookupTimeout,
cache: map[string]*dnslookup{},
expireList: []*dnslookup{},
expireList: make([]*dnslookup, 0),
srebhan marked this conversation as resolved.
Show resolved Hide resolved
maxWorkers: workerPoolSize,
sem: semaphore.NewWeighted(int64(workerPoolSize)),
cancelCleanupWorker: cancel,
Expand Down Expand Up @@ -272,7 +272,7 @@ func (d *ReverseDNSCache) cleanup() {
d.expireListLock.Unlock()
return
}
ipsToDelete := []string{}
ipsToDelete := make([]string, 0, len(d.expireList))
for i := 0; i < len(d.expireList); i++ {
if !d.expireList[i].expiresAt.Before(now) {
break // done. Nothing after this point is expired.
Expand Down
2 changes: 1 addition & 1 deletion plugins/processors/split/split.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (s *Split) Init() error {
}

func (s *Split) Apply(in ...telegraf.Metric) []telegraf.Metric {
newMetrics := []telegraf.Metric{}
newMetrics := make([]telegraf.Metric, 0, len(in)*(len(s.Templates)+1))

for _, point := range in {
if s.DropOriginal {
Expand Down
Loading
Loading