Skip to content

Commit

Permalink
chore: Fix linter findings for revive:enforce-slice-style in `plugi…
Browse files Browse the repository at this point in the history
…ns/outputs`
  • Loading branch information
zak-pawel committed Oct 16, 2024
1 parent 809480e commit 051460b
Show file tree
Hide file tree
Showing 29 changed files with 237 additions and 322 deletions.
6 changes: 3 additions & 3 deletions plugins/outputs/amon/amon.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,9 @@ func (a *Amon) Write(metrics []telegraf.Metric) error {
if len(metrics) == 0 {
return nil
}
ts := TimeSeries{}
tempSeries := []*Metric{}
metricCounter := 0

metricCounter := 0
tempSeries := make([]*Metric, 0, len(metrics))
for _, m := range metrics {
mname := strings.ReplaceAll(m.Name(), "_", ".")
if amonPts, err := buildMetrics(m); err == nil {
Expand All @@ -80,6 +79,7 @@ func (a *Amon) Write(metrics []telegraf.Metric) error {
}
}

ts := TimeSeries{}
ts.Series = make([]*Metric, metricCounter)
copy(ts.Series, tempSeries[0:])
tsBytes, err := json.Marshal(ts)
Expand Down
3 changes: 1 addition & 2 deletions plugins/outputs/clarify/clarify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,7 @@ func TestTimeout(t *testing.T) {
}),
}

metrics := []telegraf.Metric{}
err := clfy.Write(metrics)
err := clfy.Write(nil)
require.ErrorIs(t, err, errTimeout)
}

Expand Down
4 changes: 2 additions & 2 deletions plugins/outputs/cloudwatch/cloudwatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,12 @@ func TestPartitionDatums(t *testing.T) {
Value: aws.Float64(1),
}

zeroDatum := []types.MetricDatum{}
zeroDatum := make([]types.MetricDatum, 0)
oneDatum := []types.MetricDatum{testDatum}
twoDatum := []types.MetricDatum{testDatum, testDatum}
threeDatum := []types.MetricDatum{testDatum, testDatum, testDatum}

require.Equal(t, [][]types.MetricDatum{}, PartitionDatums(2, zeroDatum))
require.Empty(t, PartitionDatums(2, zeroDatum))
require.Equal(t, [][]types.MetricDatum{oneDatum}, PartitionDatums(2, oneDatum))
require.Equal(t, [][]types.MetricDatum{oneDatum}, PartitionDatums(2, oneDatum))
require.Equal(t, [][]types.MetricDatum{twoDatum}, PartitionDatums(2, twoDatum))
Expand Down
11 changes: 6 additions & 5 deletions plugins/outputs/cloudwatch_logs/cloudwatch_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ func (c *CloudWatchLogs) Write(metrics []telegraf.Metric) error {
lsContainer = val
} else {
lsContainer.messageBatches[0].messageCount = 0
lsContainer.messageBatches[0].logEvents = []types.InputLogEvent{}
lsContainer.messageBatches[0].logEvents = make([]types.InputLogEvent, 0)
c.ls[logStream] = lsContainer
}

Expand All @@ -312,8 +312,9 @@ func (c *CloudWatchLogs) Write(metrics []telegraf.Metric) error {
lsContainer.currentBatchIndex++
lsContainer.messageBatches = append(lsContainer.messageBatches,
messageBatch{
logEvents: []types.InputLogEvent{},
messageCount: 0})
messageCount: 0,
},
)
lsContainer.currentBatchSizeBytes = messageSizeInBytesForAWS
} else {
lsContainer.currentBatchSizeBytes += messageSizeInBytesForAWS
Expand Down Expand Up @@ -387,8 +388,8 @@ func (c *CloudWatchLogs) Write(metrics []telegraf.Metric) error {
}
// Cleanup batch
elem.messageBatches[index] = messageBatch{
logEvents: []types.InputLogEvent{},
messageCount: 0}
messageCount: 0,
}

elem.sequenceToken = *putLogEventsOutput.NextSequenceToken
}
Expand Down
3 changes: 1 addition & 2 deletions plugins/outputs/datadog/datadog.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ func (d *Datadog) Connect() error {
}

func (d *Datadog) convertToDatadogMetric(metrics []telegraf.Metric) []*Metric {
tempSeries := []*Metric{}

tempSeries := make([]*Metric, 0, len(metrics))
for _, m := range metrics {
if dogMs, err := buildMetrics(m); err == nil {
metricTags := buildTags(m.TagList())
Expand Down
12 changes: 6 additions & 6 deletions plugins/outputs/datadog/datadog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func TestBuildTags(t *testing.T) {
outTags []string
}{
{
[]*telegraf.Tag{
ptIn: []*telegraf.Tag{
{
Key: "one",
Value: "two",
Expand All @@ -109,20 +109,20 @@ func TestBuildTags(t *testing.T) {
Value: "four",
},
},
[]string{"one:two", "three:four"},
outTags: []string{"one:two", "three:four"},
},
{
[]*telegraf.Tag{
ptIn: []*telegraf.Tag{
{
Key: "aaa",
Value: "bbb",
},
},
[]string{"aaa:bbb"},
outTags: []string{"aaa:bbb"},
},
{
[]*telegraf.Tag{},
[]string{},
ptIn: make([]*telegraf.Tag, 0),
outTags: make([]string, 0),
},
}
for _, tt := range tagtests {
Expand Down
7 changes: 3 additions & 4 deletions plugins/outputs/dynatrace/dynatrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,9 @@ func (d *Dynatrace) Write(metrics []telegraf.Metric) error {
return nil
}

lines := []string{}

lines := make([]string, 0, len(metrics))
for _, tm := range metrics {
dims := []dimensions.Dimension{}
dims := make([]dimensions.Dimension, 0, len(tm.TagList()))
for _, tag := range tm.TagList() {
// Ignore special tags for histogram and summary types.
switch tm.Type() {
Expand Down Expand Up @@ -211,7 +210,7 @@ func (d *Dynatrace) Init() error {
Timeout: time.Duration(d.Timeout),
}

dims := []dimensions.Dimension{}
dims := make([]dimensions.Dimension, 0, len(d.DefaultDimensions))
for key, value := range d.DefaultDimensions {
dims = append(dims, dimensions.NewDimension(key, value))
}
Expand Down
22 changes: 9 additions & 13 deletions plugins/outputs/dynatrace/dynatrace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ func TestEmptyMetricsSlice(t *testing.T) {

err = d.Connect()
require.NoError(t, err)
empty := []telegraf.Metric{}
err = d.Write(empty)
err = d.Write(nil)
require.NoError(t, err)
}

Expand Down Expand Up @@ -127,7 +126,7 @@ func TestMissingAPIToken(t *testing.T) {
}

func TestSendMetrics(t *testing.T) {
expected := []string{}
var expected []string

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// check the encoded result
Expand All @@ -152,10 +151,9 @@ func TestSendMetrics(t *testing.T) {
defer ts.Close()

d := &Dynatrace{
URL: ts.URL,
APIToken: config.NewSecret([]byte("123")),
Log: testutil.Logger{},
AddCounterMetrics: []string{},
URL: ts.URL,
APIToken: config.NewSecret([]byte("123")),
Log: testutil.Logger{},
}

err := d.Init()
Expand Down Expand Up @@ -214,7 +212,7 @@ func TestSendMetrics(t *testing.T) {
}

func TestSendMetricsWithPatterns(t *testing.T) {
expected := []string{}
var expected []string

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// check the encoded result
Expand All @@ -239,11 +237,9 @@ func TestSendMetricsWithPatterns(t *testing.T) {
defer ts.Close()

d := &Dynatrace{
URL: ts.URL,
APIToken: config.NewSecret([]byte("123")),
Log: testutil.Logger{},
AddCounterMetrics: []string{},
AddCounterMetricsPatterns: []string{},
URL: ts.URL,
APIToken: config.NewSecret([]byte("123")),
Log: testutil.Logger{},
}

err := d.Init()
Expand Down
5 changes: 2 additions & 3 deletions plugins/outputs/elasticsearch/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ func (a *Elasticsearch) createNewTemplate(templatePattern string) (*bytes.Buffer
}

func (a *Elasticsearch) GetTagKeys(indexName string) (string, []string) {
tagKeys := []string{}
tagKeys := make([]string, 0)
startTag := strings.Index(indexName, "{{")

for startTag >= 0 {
Expand Down Expand Up @@ -464,8 +464,7 @@ func (a *Elasticsearch) GetIndexName(indexName string, eventTime time.Time, tagK
indexName = dateReplacer.Replace(indexName)
}

tagValues := []interface{}{}

tagValues := make([]interface{}, 0, len(tagKeys))
for _, key := range tagKeys {
if value, ok := metricTags[key]; ok {
tagValues = append(tagValues, value)
Expand Down
Loading

0 comments on commit 051460b

Please sign in to comment.