diff --git a/plugins/parsers/collectd/parser.go b/plugins/parsers/collectd/parser.go index 4f884a9e86097..8a617a33d4092 100644 --- a/plugins/parsers/collectd/parser.go +++ b/plugins/parsers/collectd/parser.go @@ -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)...) } diff --git a/plugins/parsers/collectd/parser_test.go b/plugins/parsers/collectd/parser_test.go index 87afe736d8bc9..77b6c36c3881c 100644 --- a/plugins/parsers/collectd/parser_test.go +++ b/plugins/parsers/collectd/parser_test.go @@ -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.Equal(t, make([]telegraf.Metric, 0), metrics) // Wrong password error buf, err = writeValueList(singleMetric.vl) @@ -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.Equal(t, make([]telegraf.Metric, 0), metrics) // Encrypted data buf, err = writeValueList(singleMetric.vl) @@ -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.Equal(t, make([]telegraf.Metric, 0), metrics) // Wrong password error buf, err = writeValueList(singleMetric.vl) diff --git a/plugins/parsers/csv/parser.go b/plugins/parsers/csv/parser.go index f056b6e3a27f4..dac316a94388b 100644 --- a/plugins/parsers/csv/parser.go +++ b/plugins/parsers/csv/parser.go @@ -84,7 +84,7 @@ func (record metadataPattern) Less(i, j int) bool { func (p *Parser) initializeMetadataSeparators() error { // initialize metadata p.metadataTags = map[string]string{} - p.metadataSeparatorList = []string{} + p.metadataSeparatorList = make([]string, 0) if p.MetadataRows <= 0 { return nil @@ -94,7 +94,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] { diff --git a/plugins/parsers/csv/parser_test.go b/plugins/parsers/csv/parser_test.go index 75efdc43c0062..7a433473a13fc 100644 --- a/plugins/parsers/csv/parser_test.go +++ b/plugins/parsers/csv/parser_test.go @@ -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.Equal(t, make([]telegraf.Metric, 0), metrics) m, err := p.ParseLine(testCSVRows[1]) require.NoError(t, err) require.Equal(t, "test_name", m.Name()) @@ -849,14 +849,14 @@ func TestParseMetadataSeparators(t *testing.T) { p := &Parser{ ColumnNames: []string{"a", "b"}, MetadataRows: 0, - MetadataSeparators: []string{}, + MetadataSeparators: make([]string, 0), } 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) diff --git a/plugins/parsers/influx/influx_upstream/parser.go b/plugins/parsers/influx/influx_upstream/parser.go index 6924b4b8b63ca..96ac900f913b8 100644 --- a/plugins/parsers/influx/influx_upstream/parser.go +++ b/plugins/parsers/influx/influx_upstream/parser.go @@ -264,7 +264,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(make([]byte, 0), err) } return m, nil diff --git a/plugins/parsers/influx/influx_upstream/parser_test.go b/plugins/parsers/influx/influx_upstream/parser_test.go index d9ad173bd415b..5624e063f16b0 100644 --- a/plugins/parsers/influx/influx_upstream/parser_test.go +++ b/plugins/parsers/influx/influx_upstream/parser_test.go @@ -685,7 +685,7 @@ func TestSeriesParser(t *testing.T) { { name: "empty", input: []byte(""), - metrics: []telegraf.Metric{}, + metrics: make([]telegraf.Metric, 0), }, { name: "minimal", @@ -717,7 +717,7 @@ func TestSeriesParser(t *testing.T) { { name: "missing tag value", input: []byte("cpu,a="), - metrics: []telegraf.Metric{}, + metrics: make([]telegraf.Metric, 0), err: &ParseError{ DecodeError: &lineprotocol.DecodeError{ Line: 1, @@ -730,7 +730,7 @@ 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{}, + metrics: make([]telegraf.Metric, 0), err: &ParseError{ DecodeError: &lineprotocol.DecodeError{ Line: 1, diff --git a/plugins/parsers/influx/parser_test.go b/plugins/parsers/influx/parser_test.go index f1da1dba48085..06a7576479e14 100644 --- a/plugins/parsers/influx/parser_test.go +++ b/plugins/parsers/influx/parser_test.go @@ -763,7 +763,7 @@ func TestSeriesParser(t *testing.T) { { name: "empty", input: []byte(""), - metrics: []telegraf.Metric{}, + metrics: make([]telegraf.Metric, 0), }, { name: "minimal", @@ -795,7 +795,7 @@ func TestSeriesParser(t *testing.T) { { name: "missing tag value", input: []byte("cpu,a="), - metrics: []telegraf.Metric{}, + metrics: make([]telegraf.Metric, 0), err: &ParseError{ Offset: 6, LineNumber: 1, @@ -807,7 +807,7 @@ 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{}, + metrics: make([]telegraf.Metric, 0), err: &ParseError{ Offset: 1031, LineNumber: 1, diff --git a/plugins/parsers/json/parser_test.go b/plugins/parsers/json/parser_test.go index b69615dbfd2a5..057b21f5c385b 100644 --- a/plugins/parsers/json/parser_test.go +++ b/plugins/parsers/json/parser_test.go @@ -602,7 +602,7 @@ func TestJSONQueryErrorOnArray(t *testing.T) { parser := &Parser{ MetricName: "json_test", - TagKeys: []string{}, + TagKeys: make([]string, 0), Query: "shares.myArr", } require.NoError(t, parser.Init()) @@ -913,19 +913,19 @@ func TestParse(t *testing.T) { name: "parse empty array", parser: &Parser{}, input: []byte(`[]`), - expected: []telegraf.Metric{}, + expected: make([]telegraf.Metric, 0), }, { name: "parse null", parser: &Parser{}, input: []byte(`null`), - expected: []telegraf.Metric{}, + expected: make([]telegraf.Metric, 0), }, { 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{}, + expected: make([]telegraf.Metric, 0), }, { name: "parse simple array", diff --git a/plugins/parsers/json_v2/parser_test.go b/plugins/parsers/json_v2/parser_test.go index d2650bda16128..1f1bf6cec5921 100644 --- a/plugins/parsers/json_v2/parser_test.go +++ b/plugins/parsers/json_v2/parser_test.go @@ -102,7 +102,7 @@ func TestMultipleConfigs(t *testing.T) { func TestParserEmptyConfig(t *testing.T) { plugin := &json_v2.Parser{ - Configs: []json_v2.Config{}, + Configs: make([]json_v2.Config, 0), } require.ErrorContains(t, plugin.Init(), "no configuration provided") diff --git a/plugins/parsers/logfmt/parser_test.go b/plugins/parsers/logfmt/parser_test.go index 4d5950f0d951c..f42087564a5eb 100644 --- a/plugins/parsers/logfmt/parser_test.go +++ b/plugins/parsers/logfmt/parser_test.go @@ -21,7 +21,7 @@ func TestParse(t *testing.T) { }{ { name: "no bytes returns no metrics", - want: []telegraf.Metric{}, + want: make([]telegraf.Metric, 0), }, { name: "test without trailing end", @@ -104,27 +104,27 @@ func TestParse(t *testing.T) { { name: "keys without = or values are ignored", bytes: []byte(`i am no data.`), - want: []telegraf.Metric{}, + want: make([]telegraf.Metric, 0), wantErr: false, }, { name: "keys without values are ignored", bytes: []byte(`foo="" bar=`), - want: []telegraf.Metric{}, + want: make([]telegraf.Metric, 0), wantErr: false, }, { name: "unterminated quote produces error", measurement: "testlog", bytes: []byte(`bar=baz foo="bar`), - want: []telegraf.Metric{}, + want: make([]telegraf.Metric, 0), wantErr: true, }, { name: "malformed key", measurement: "testlog", bytes: []byte(`"foo=" bar=baz`), - want: []telegraf.Metric{}, + want: make([]telegraf.Metric, 0), wantErr: true, }, } diff --git a/plugins/parsers/nagios/parser_test.go b/plugins/parsers/nagios/parser_test.go index c7a111c0c4f41..663567ac3a219 100644 --- a/plugins/parsers/nagios/parser_test.go +++ b/plugins/parsers/nagios/parser_test.go @@ -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] diff --git a/plugins/parsers/value/parser.go b/plugins/parsers/value/parser.go index 2ac053c08d6d7..206b072edd1c9 100644 --- a/plugins/parsers/value/parser.go +++ b/plugins/parsers/value/parser.go @@ -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 } vStr = values[len(values)-1] } diff --git a/plugins/parsers/xpath/parser.go b/plugins/parsers/xpath/parser.go index 0cf45082fc9e4..7a306d97973c9 100644 --- a/plugins/parsers/xpath/parser.go +++ b/plugins/parsers/xpath/parser.go @@ -562,7 +562,7 @@ func splitLastPathElement(query string) []string { // Nothing left if query == "" || query == "/" || query == "//" || query == "." { - return []string{} + return make([]string, 0) } separatorIdx := strings.LastIndex(query, "/") diff --git a/plugins/parsers/xpath/parser_test.go b/plugins/parsers/xpath/parser_test.go index 76997d98612a6..c4580ca2f8a04 100644 --- a/plugins/parsers/xpath/parser_test.go +++ b/plugins/parsers/xpath/parser_test.go @@ -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), Log: testutil.Logger{Name: "parsers.protobuf"}, } require.NoError(t, parser.Init()) diff --git a/plugins/processors/aws_ec2/ec2_test.go b/plugins/processors/aws_ec2/ec2_test.go index 7f3b3aa302803..f913261efc859 100644 --- a/plugins/processors/aws_ec2/ec2_test.go +++ b/plugins/processors/aws_ec2/ec2_test.go @@ -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) err := p.Init() require.Error(t, err) } diff --git a/plugins/processors/filter/filter_test.go b/plugins/processors/filter/filter_test.go index a450b2252f0b8..ad3012d0d8bb3 100644 --- a/plugins/processors/filter/filter_test.go +++ b/plugins/processors/filter/filter_test.go @@ -95,7 +95,7 @@ func TestNoMetric(t *testing.T) { } require.NoError(t, plugin.Init()) - input := []telegraf.Metric{} + input := make([]telegraf.Metric, 0) require.Empty(t, plugin.Apply(input...)) } diff --git a/plugins/processors/parser/parser.go b/plugins/processors/parser/parser.go index ae1911f17da71..f3eecfa9040a2 100644 --- a/plugins/processors/parser/parser.go +++ b/plugins/processors/parser/parser.go @@ -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) if !p.DropOriginal { newMetrics = append(newMetrics, metric) } else { diff --git a/plugins/processors/reverse_dns/rdnscache.go b/plugins/processors/reverse_dns/rdnscache.go index c027fc132ef33..cb99e753922e6 100644 --- a/plugins/processors/reverse_dns/rdnscache.go +++ b/plugins/processors/reverse_dns/rdnscache.go @@ -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), maxWorkers: workerPoolSize, sem: semaphore.NewWeighted(int64(workerPoolSize)), cancelCleanupWorker: cancel, @@ -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. diff --git a/plugins/processors/split/split.go b/plugins/processors/split/split.go index 2104cf6eb9537..41f68ee49971d 100644 --- a/plugins/processors/split/split.go +++ b/plugins/processors/split/split.go @@ -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 { diff --git a/plugins/processors/starlark/starlark_test.go b/plugins/processors/starlark/starlark_test.go index a93f71d066d1a..a65ddbd1257f2 100644 --- a/plugins/processors/starlark/starlark_test.go +++ b/plugins/processors/starlark/starlark_test.go @@ -113,7 +113,7 @@ def apply(metric): time.Unix(0, 0), ), }, - expected: []telegraf.Metric{}, + expected: make([]telegraf.Metric, 0), }, { name: "passthrough", @@ -185,7 +185,7 @@ def apply(metric): time.Unix(0, 0), ), }, - expected: []telegraf.Metric{}, + expected: make([]telegraf.Metric, 0), expectedErrorStr: "append: cannot append to frozen list", }, { @@ -348,7 +348,7 @@ def apply(metric): time.Unix(0, 0), ), }, - expected: []telegraf.Metric{}, + expected: make([]telegraf.Metric, 0), expectedErrorStr: "type error", }, { @@ -417,7 +417,7 @@ def apply(metric): time.Unix(0, 0), ), }, - expected: []telegraf.Metric{}, + expected: make([]telegraf.Metric, 0), expectedErrorStr: "cannot set tags", }, { @@ -546,7 +546,7 @@ def apply(metric): time.Unix(0, 0), ), }, - expected: []telegraf.Metric{}, + expected: make([]telegraf.Metric, 0), expectedErrorStr: `key "foo" not in Tags`, }, { @@ -661,7 +661,7 @@ def apply(metric): time.Unix(0, 0), ), }, - expected: []telegraf.Metric{}, + expected: make([]telegraf.Metric, 0), expectedErrorStr: "tag value must be of type 'str'", }, { @@ -773,7 +773,7 @@ def apply(metric): time.Unix(0, 0), ), }, - expected: []telegraf.Metric{}, + expected: make([]telegraf.Metric, 0), expectedErrorStr: "popitem(): tag dictionary is empty", }, { @@ -1238,7 +1238,7 @@ def apply(metric): time.Unix(0, 0), ), }, - expected: []telegraf.Metric{}, + expected: make([]telegraf.Metric, 0), expectedErrorStr: "pop: cannot delete during iteration", }, { @@ -1261,7 +1261,7 @@ def apply(metric): time.Unix(0, 0), ), }, - expected: []telegraf.Metric{}, + expected: make([]telegraf.Metric, 0), expectedErrorStr: "cannot delete during iteration", }, { @@ -1284,7 +1284,7 @@ def apply(metric): time.Unix(0, 0), ), }, - expected: []telegraf.Metric{}, + expected: make([]telegraf.Metric, 0), expectedErrorStr: "cannot delete during iteration", }, { @@ -1307,7 +1307,7 @@ def apply(metric): time.Unix(0, 0), ), }, - expected: []telegraf.Metric{}, + expected: make([]telegraf.Metric, 0), expectedErrorStr: "cannot insert during iteration", }, { @@ -1378,7 +1378,7 @@ def apply(metric): time.Unix(0, 0), ), }, - expected: []telegraf.Metric{}, + expected: make([]telegraf.Metric, 0), expectedErrorStr: "cannot set fields", }, { @@ -1585,7 +1585,7 @@ def apply(metric): time.Unix(0, 0), ), }, - expected: []telegraf.Metric{}, + expected: make([]telegraf.Metric, 0), expectedErrorStr: `key "foo" not in Fields`, }, { @@ -1771,7 +1771,7 @@ def apply(metric): time.Unix(0, 0), ), }, - expected: []telegraf.Metric{}, + expected: make([]telegraf.Metric, 0), expectedErrorStr: "invalid starlark type", }, { @@ -1887,7 +1887,7 @@ def apply(metric): time.Unix(0, 0), ), }, - expected: []telegraf.Metric{}, + expected: make([]telegraf.Metric, 0), expectedErrorStr: "popitem(): field dictionary is empty", }, { @@ -2309,7 +2309,7 @@ def apply(metric): time.Unix(0, 0), ), }, - expected: []telegraf.Metric{}, + expected: make([]telegraf.Metric, 0), expectedErrorStr: "pop: cannot delete during iteration", }, { @@ -2327,7 +2327,7 @@ def apply(metric): time.Unix(0, 0), ), }, - expected: []telegraf.Metric{}, + expected: make([]telegraf.Metric, 0), expectedErrorStr: "cannot delete during iteration", }, { @@ -2345,7 +2345,7 @@ def apply(metric): time.Unix(0, 0), ), }, - expected: []telegraf.Metric{}, + expected: make([]telegraf.Metric, 0), expectedErrorStr: "cannot delete during iteration", }, { @@ -2363,7 +2363,7 @@ def apply(metric): time.Unix(0, 0), ), }, - expected: []telegraf.Metric{}, + expected: make([]telegraf.Metric, 0), expectedErrorStr: "cannot insert during iteration", }, { @@ -2435,7 +2435,7 @@ def apply(metric): time.Unix(0, 0).UTC(), ), }, - expected: []telegraf.Metric{}, + expected: make([]telegraf.Metric, 0), expectedErrorStr: "type error", }, { @@ -2909,7 +2909,7 @@ func TestScript(t *testing.T) { time.Unix(0, 0), ), }, - expected: []telegraf.Metric{}, + expected: make([]telegraf.Metric, 0), expectedErrorStr: "fail: The field value should be greater than 1", }, } @@ -3306,7 +3306,7 @@ func TestAllScriptTestData(t *testing.T) { lines := strings.Split(string(b), "\n") inputMetrics := parseMetricsFrom(t, lines, "Example Input:") expectedErrorStr := parseErrorMessage(t, lines, "Example Output Error:") - outputMetrics := []telegraf.Metric{} + outputMetrics := make([]telegraf.Metric, 0) if expectedErrorStr == "" { outputMetrics = parseMetricsFrom(t, lines, "Example Output:") } diff --git a/plugins/processors/topk/topk.go b/plugins/processors/topk/topk.go index 61f756d2b9994..3c1b358aa1f77 100644 --- a/plugins/processors/topk/topk.go +++ b/plugins/processors/topk/topk.go @@ -48,8 +48,8 @@ func New() *TopK { topk.Aggregation = "mean" topk.GroupBy = []string{"*"} topk.AddGroupByTag = "" - topk.AddRankFields = []string{} - topk.AddAggregateFields = []string{} + topk.AddRankFields = make([]string, 0) + topk.AddAggregateFields = make([]string, 0) // Initialize cache topk.Reset() @@ -187,7 +187,7 @@ func (t *TopK) Apply(in ...telegraf.Metric) []telegraf.Metric { return t.push() } - return []telegraf.Metric{} + return make([]telegraf.Metric, 0) } func convert(in interface{}) (float64, bool) { @@ -211,7 +211,7 @@ func (t *TopK) push() []telegraf.Metric { // If we could not generate the aggregation // function, fail hard by dropping all metrics t.Log.Errorf("%v", err) - return []telegraf.Metric{} + return make([]telegraf.Metric, 0) } for k, ms := range t.cache { aggregations = append(aggregations, MetricAggregation{groupbykey: k, values: aggregator(ms, t.Fields)}) diff --git a/plugins/processors/topk/topk_test.go b/plugins/processors/topk/topk_test.go index 14aa91baffa3c..61ddc0e37ec05 100644 --- a/plugins/processors/topk/topk_test.go +++ b/plugins/processors/topk/topk_test.go @@ -55,7 +55,7 @@ type metricChange struct { // they are semantically equal. // Therefore the fields and tags must be in the same order that the processor would add them func generateAns(input []telegraf.Metric, changeSet map[int]metricChange) []telegraf.Metric { - answer := []telegraf.Metric{} + answer := make([]telegraf.Metric, 0, len(input)) // For every input metric, we check if there is a change we need to apply // If there is no change for a given input metric, the metric is dropped @@ -411,7 +411,7 @@ func TestTopkGroupbyMetricName1(t *testing.T) { topk.K = 1 topk.Aggregation = "sum" topk.AddAggregateFields = []string{"value"} - topk.GroupBy = []string{} + topk.GroupBy = make([]string, 0) // Get the input input := deepCopy(MetricsSet2) diff --git a/plugins/secretstores/http/decryption_test.go b/plugins/secretstores/http/decryption_test.go index 94fcb6c2cc036..51abc6bc08bc7 100644 --- a/plugins/secretstores/http/decryption_test.go +++ b/plugins/secretstores/http/decryption_test.go @@ -14,7 +14,7 @@ func TestCreateAESFail(t *testing.T) { } func TestTrimPKCSFail(t *testing.T) { - _, err := PKCS5or7Trimming([]byte{}) + _, err := PKCS5or7Trimming(make([]byte, 0)) require.ErrorContains(t, err, "empty value to trim") _, err = PKCS5or7Trimming([]byte{0x00, 0x05}) diff --git a/plugins/serializers/graphite/graphite.go b/plugins/serializers/graphite/graphite.go index b50e28879b757..0790da75a5edb 100644 --- a/plugins/serializers/graphite/graphite.go +++ b/plugins/serializers/graphite/graphite.go @@ -84,7 +84,7 @@ func (s *GraphiteSerializer) Init() error { } func (s *GraphiteSerializer) Serialize(metric telegraf.Metric) ([]byte, error) { - out := []byte{} + out := make([]byte, 0) // Convert UnixNano to Unix timestamps timestamp := metric.Time().UnixNano() / 1000000000 diff --git a/plugins/serializers/json/json.go b/plugins/serializers/json/json.go index 3923aec96ef31..65e66d5a0796c 100644 --- a/plugins/serializers/json/json.go +++ b/plugins/serializers/json/json.go @@ -70,7 +70,7 @@ func (s *Serializer) Serialize(metric telegraf.Metric) ([]byte, error) { serialized, err := json.Marshal(obj) if err != nil { - return []byte{}, err + return make([]byte, 0), err } serialized = append(serialized, '\n') @@ -101,7 +101,7 @@ func (s *Serializer) SerializeBatch(metrics []telegraf.Metric) ([]byte, error) { serialized, err := json.Marshal(obj) if err != nil { - return []byte{}, err + return make([]byte, 0), err } serialized = append(serialized, '\n') diff --git a/plugins/serializers/prometheus/collection_test.go b/plugins/serializers/prometheus/collection_test.go index b386e37e4df45..9145a5255491a 100644 --- a/plugins/serializers/prometheus/collection_test.go +++ b/plugins/serializers/prometheus/collection_test.go @@ -49,7 +49,7 @@ func TestCollectionExpire(t *testing.T) { Type: dto.MetricType_UNTYPED.Enum(), Metric: []*dto.Metric{ { - Label: []*dto.LabelPair{}, + Label: make([]*dto.LabelPair, 0), Untyped: &dto.Untyped{Value: proto.Float64(42.0)}, }, }, @@ -91,7 +91,7 @@ func TestCollectionExpire(t *testing.T) { Type: dto.MetricType_UNTYPED.Enum(), Metric: []*dto.Metric{ { - Label: []*dto.LabelPair{}, + Label: make([]*dto.LabelPair, 0), Untyped: &dto.Untyped{Value: proto.Float64(43.0)}, }, }, @@ -132,7 +132,7 @@ func TestCollectionExpire(t *testing.T) { Type: dto.MetricType_UNTYPED.Enum(), Metric: []*dto.Metric{ { - Label: []*dto.LabelPair{}, + Label: make([]*dto.LabelPair, 0), Untyped: &dto.Untyped{Value: proto.Float64(42.0)}, }, }, @@ -156,7 +156,7 @@ func TestCollectionExpire(t *testing.T) { addtime: time.Unix(0, 0), }, }, - expected: []*dto.MetricFamily{}, + expected: make([]*dto.MetricFamily, 0), }, { name: "expired one metric in metric family", @@ -192,7 +192,7 @@ func TestCollectionExpire(t *testing.T) { Type: dto.MetricType_UNTYPED.Enum(), Metric: []*dto.Metric{ { - Label: []*dto.LabelPair{}, + Label: make([]*dto.LabelPair, 0), Untyped: &dto.Untyped{Value: proto.Float64(42.0)}, }, }, @@ -282,7 +282,7 @@ func TestCollectionExpire(t *testing.T) { Type: dto.MetricType_HISTOGRAM.Enum(), Metric: []*dto.Metric{ { - Label: []*dto.LabelPair{}, + Label: make([]*dto.LabelPair, 0), Histogram: &dto.Histogram{ SampleCount: proto.Uint64(4), SampleSum: proto.Float64(20.0), @@ -343,7 +343,7 @@ func TestCollectionExpire(t *testing.T) { addtime: time.Unix(0, 0), }, }, - expected: []*dto.MetricFamily{}, + expected: make([]*dto.MetricFamily, 0), }, { name: "histogram does not expire because of addtime from bucket", @@ -393,7 +393,7 @@ func TestCollectionExpire(t *testing.T) { Type: dto.MetricType_HISTOGRAM.Enum(), Metric: []*dto.Metric{ { - Label: []*dto.LabelPair{}, + Label: make([]*dto.LabelPair, 0), Histogram: &dto.Histogram{ SampleCount: proto.Uint64(2), SampleSum: proto.Float64(10.0), @@ -474,7 +474,7 @@ func TestCollectionExpire(t *testing.T) { Type: dto.MetricType_SUMMARY.Enum(), Metric: []*dto.Metric{ { - Label: []*dto.LabelPair{}, + Label: make([]*dto.LabelPair, 0), Summary: &dto.Summary{ SampleCount: proto.Uint64(2), SampleSum: proto.Float64(2.0), @@ -520,7 +520,7 @@ func TestCollectionExpire(t *testing.T) { addtime: time.Unix(0, 0), }, }, - expected: []*dto.MetricFamily{}, + expected: make([]*dto.MetricFamily, 0), }, { name: "summary does not expire because of quantile addtime", @@ -570,7 +570,7 @@ func TestCollectionExpire(t *testing.T) { Type: dto.MetricType_SUMMARY.Enum(), Metric: []*dto.Metric{ { - Label: []*dto.LabelPair{}, + Label: make([]*dto.LabelPair, 0), Summary: &dto.Summary{ SampleSum: proto.Float64(1), SampleCount: proto.Uint64(1), @@ -614,7 +614,7 @@ func TestCollectionExpire(t *testing.T) { Type: dto.MetricType_UNTYPED.Enum(), Metric: []*dto.Metric{ { - Label: []*dto.LabelPair{}, + Label: make([]*dto.LabelPair, 0), Untyped: &dto.Untyped{Value: proto.Float64(42.0)}, }, }, @@ -728,7 +728,7 @@ func TestExportTimestamps(t *testing.T) { Type: dto.MetricType_HISTOGRAM.Enum(), Metric: []*dto.Metric{ { - Label: []*dto.LabelPair{}, + Label: make([]*dto.LabelPair, 0), TimestampMs: proto.Int64(time.Unix(20, 0).UnixNano() / int64(time.Millisecond)), Histogram: &dto.Histogram{ SampleCount: proto.Uint64(4), @@ -810,7 +810,7 @@ func TestExportTimestamps(t *testing.T) { Type: dto.MetricType_SUMMARY.Enum(), Metric: []*dto.Metric{ { - Label: []*dto.LabelPair{}, + Label: make([]*dto.LabelPair, 0), TimestampMs: proto.Int64(time.Unix(20, 0).UnixNano() / int64(time.Millisecond)), Summary: &dto.Summary{ SampleCount: proto.Uint64(2),