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/inputs/[a-d]*` and `plugins/common` (#16161)

(cherry picked from commit b4fdd52)
  • Loading branch information
zak-pawel authored and srebhan committed Nov 18, 2024
1 parent fad4b1e commit 10b4311
Show file tree
Hide file tree
Showing 29 changed files with 77 additions and 152 deletions.
2 changes: 1 addition & 1 deletion plugins/common/jolokia2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func makeReadResponses(jresponses []jolokiaResponse) []ReadResponse {
rrequest := ReadRequest{
Mbean: jr.Request.Mbean,
Path: jr.Request.Path,
Attributes: []string{},
Attributes: make([]string, 0),
}

attrValue := jr.Request.Attribute
Expand Down
2 changes: 1 addition & 1 deletion plugins/common/jolokia2/gatherer.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func makeReadRequests(metrics []Metric) []ReadRequest {
if len(metric.Paths) == 0 {
requests = append(requests, ReadRequest{
Mbean: metric.Mbean,
Attributes: []string{},
Attributes: make([]string, 0),
})
} else {
attributes := make(map[string][]string)
Expand Down
2 changes: 1 addition & 1 deletion plugins/common/jolokia2/gatherer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestJolokia2_makeReadRequests(t *testing.T) {
expected: []ReadRequest{
{
Mbean: "test:foo=bar",
Attributes: []string{},
Attributes: make([]string, 0),
},
},
}, {
Expand Down
4 changes: 2 additions & 2 deletions plugins/common/jolokia2/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ func (m Metric) MatchAttributeAndPath(attribute, innerPath string) bool {
func parseMbeanObjectName(name string) (string, []string) {
index := strings.Index(name, ":")
if index == -1 {
return name, []string{}
return name, nil
}

domain := name[:index]

if index+1 > len(name) {
return domain, []string{}
return domain, nil
}

return domain, strings.Split(name[index+1:], ",")
Expand Down
21 changes: 0 additions & 21 deletions plugins/common/opcua/input/input_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ func TestValidateOPCTags(t *testing.T) {
TagsSlice: [][]string{{"t1", "v1"}, {"t3", "v2"}},
},
},
Groups: []NodeGroupSettings{},
},
nil,
},
Expand All @@ -150,15 +149,13 @@ func TestValidateOPCTags(t *testing.T) {
TagsSlice: [][]string{{"t1", "bar"}, {"t2", "v2"}},
},
},
Groups: []NodeGroupSettings{},
},
nil,
},
{
"different metric names",
InputClientConfig{
MetricName: "mn",
RootNodes: []NodeSettings{},
Groups: []NodeGroupSettings{
{
MetricName: "mn",
Expand Down Expand Up @@ -208,7 +205,6 @@ func TestValidateOPCTags(t *testing.T) {
TagsSlice: [][]string{{"t1", "v1"}, {"t2", "v2"}},
},
},
Groups: []NodeGroupSettings{},
},
nil,
},
Expand Down Expand Up @@ -239,7 +235,6 @@ func TestNewNodeMetricMappingTags(t *testing.T) {
Namespace: "2",
IdentifierType: "s",
Identifier: "h",
TagsSlice: [][]string{},
},
groupTags: map[string]string{},
expectedTags: map[string]string{},
Expand All @@ -265,7 +260,6 @@ func TestNewNodeMetricMappingTags(t *testing.T) {
Namespace: "2",
IdentifierType: "s",
Identifier: "h",
TagsSlice: [][]string{},
},
groupTags: map[string]string{"t1": "v1"},
expectedTags: map[string]string{"t1": "v1"},
Expand Down Expand Up @@ -313,7 +307,6 @@ func TestNewNodeMetricMappingIdStrInstantiated(t *testing.T) {
Namespace: "2",
IdentifierType: "s",
Identifier: "h",
TagsSlice: [][]string{},
}, map[string]string{})
require.NoError(t, err)
require.Equal(t, "ns=2;s=h", nmm.idStr)
Expand All @@ -335,7 +328,6 @@ func TestValidateNodeToAdd(t *testing.T) {
Namespace: "2",
IdentifierType: "s",
Identifier: "hf",
TagsSlice: [][]string{},
}, map[string]string{})
require.NoError(t, err)
return nmm
Expand All @@ -351,7 +343,6 @@ func TestValidateNodeToAdd(t *testing.T) {
Namespace: "2",
IdentifierType: "s",
Identifier: "hf",
TagsSlice: [][]string{},
}, map[string]string{})
require.NoError(t, err)
return nmm
Expand All @@ -367,7 +358,6 @@ func TestValidateNodeToAdd(t *testing.T) {
Namespace: "",
IdentifierType: "s",
Identifier: "hf",
TagsSlice: [][]string{},
}, map[string]string{})
require.NoError(t, err)
return nmm
Expand All @@ -383,7 +373,6 @@ func TestValidateNodeToAdd(t *testing.T) {
Namespace: "2",
IdentifierType: "",
Identifier: "hf",
TagsSlice: [][]string{},
}, map[string]string{})
require.NoError(t, err)
return nmm
Expand All @@ -399,7 +388,6 @@ func TestValidateNodeToAdd(t *testing.T) {
Namespace: "2",
IdentifierType: "j",
Identifier: "hf",
TagsSlice: [][]string{},
}, map[string]string{})
require.NoError(t, err)
return nmm
Expand Down Expand Up @@ -433,7 +421,6 @@ func TestValidateNodeToAdd(t *testing.T) {
Namespace: "2",
IdentifierType: "i",
Identifier: "hf",
TagsSlice: [][]string{},
}, map[string]string{})
require.NoError(t, err)
return nmm
Expand Down Expand Up @@ -461,7 +448,6 @@ func TestValidateNodeToAdd(t *testing.T) {
Namespace: "2",
IdentifierType: idT,
Identifier: idV,
TagsSlice: [][]string{},
}, map[string]string{})
require.NoError(t, err)
return nmm
Expand Down Expand Up @@ -499,7 +485,6 @@ func TestInitNodeMetricMapping(t *testing.T) {
TagsSlice: [][]string{{"t1", "v1"}},
},
},
Groups: []NodeGroupSettings{},
},
expected: []NodeMetricMapping{
{
Expand Down Expand Up @@ -543,7 +528,6 @@ func TestInitNodeMetricMapping(t *testing.T) {
TagsSlice: [][]string{{"t2", "v2"}},
},
},
TagsSlice: [][]string{},
},
},
},
Expand Down Expand Up @@ -580,7 +564,6 @@ func TestInitNodeMetricMapping(t *testing.T) {
config: InputClientConfig{
MetricName: "testmetric",
Timestamp: TimestampSourceTelegraf,
RootNodes: []NodeSettings{},
Groups: []NodeGroupSettings{
{
MetricName: "groupmetric",
Expand All @@ -593,7 +576,6 @@ func TestInitNodeMetricMapping(t *testing.T) {
TagsSlice: [][]string{{"t2", "v2"}},
},
},
TagsSlice: [][]string{},
},
},
},
Expand All @@ -618,7 +600,6 @@ func TestInitNodeMetricMapping(t *testing.T) {
config: InputClientConfig{
MetricName: "testmetric",
Timestamp: TimestampSourceTelegraf,
RootNodes: []NodeSettings{},
Groups: []NodeGroupSettings{
{
MetricName: "groupmetric",
Expand All @@ -632,7 +613,6 @@ func TestInitNodeMetricMapping(t *testing.T) {
DefaultTags: map[string]string{"t3": "v3"},
},
},
TagsSlice: [][]string{},
},
},
},
Expand Down Expand Up @@ -668,7 +648,6 @@ func TestInitNodeMetricMapping(t *testing.T) {
DefaultTags: map[string]string{"t3": "v3"},
},
},
Groups: []NodeGroupSettings{},
},
expected: []NodeMetricMapping{
{
Expand Down
5 changes: 2 additions & 3 deletions plugins/common/opcua/opcua_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,15 @@ func pemBlockForKey(priv interface{}) (*pem.Block, error) {
}

func (o *OpcUAClient) generateClientOpts(endpoints []*ua.EndpointDescription) ([]opcua.Option, error) {
opts := []opcua.Option{}
appuri := "urn:telegraf:gopcua:client"
appname := "Telegraf"

// ApplicationURI is automatically read from the cert so is not required if a cert if provided
opts = append(opts,
opts := []opcua.Option{
opcua.ApplicationURI(appuri),
opcua.ApplicationName(appname),
opcua.RequestTimeout(time.Duration(o.Config.RequestTimeout)),
)
}

if o.Config.SessionTimeout != 0 {
opts = append(opts, opcua.SessionTimeout(time.Duration(o.Config.SessionTimeout)))
Expand Down
6 changes: 3 additions & 3 deletions plugins/common/shim/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,17 @@ func DefaultImportedPlugins() config {
}
for name := range inputs.Inputs {
log.Println("No config found. Loading default config for plugin", name)
conf.Inputs[name] = []toml.Primitive{}
conf.Inputs[name] = make([]toml.Primitive, 0)
return conf
}
for name := range processors.Processors {
log.Println("No config found. Loading default config for plugin", name)
conf.Processors[name] = []toml.Primitive{}
conf.Processors[name] = make([]toml.Primitive, 0)
return conf
}
for name := range outputs.Outputs {
log.Println("No config found. Loading default config for plugin", name)
conf.Outputs[name] = []toml.Primitive{}
conf.Outputs[name] = make([]toml.Primitive, 0)
return conf
}
return conf
Expand Down
4 changes: 2 additions & 2 deletions plugins/common/starlark/starlark.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (s *Common) GetState() interface{} {
// Return the actual byte-type instead of nil allowing the persister
// to guess instantiate variable of the appropriate type
if s.state == nil {
return []byte{}
return make([]byte, 0)
}

// Convert the starlark dict into a golang dictionary for serialization
Expand Down Expand Up @@ -65,7 +65,7 @@ func (s *Common) GetState() interface{} {
var buf bytes.Buffer
if err := gob.NewEncoder(&buf).Encode(state); err != nil {
s.Log.Errorf("encoding state failed: %v", err)
return []byte{}
return make([]byte, 0)
}

return buf.Bytes()
Expand Down
3 changes: 1 addition & 2 deletions plugins/common/tls/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ func Ciphers() (secure, insecure []string) {
// ParseCiphers returns a `[]uint16` by received `[]string` key that represents ciphers from crypto/tls.
// If some of ciphers in received list doesn't exists ParseCiphers returns nil with error
func ParseCiphers(ciphers []string) ([]uint16, error) {
suites := []uint16{}

suites := make([]uint16, 0)
added := make(map[uint16]bool, len(ciphers))
for _, c := range ciphers {
// Handle meta-keywords
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/aliyuncms/aliyuncms.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (s *AliyunCMS) Init() error {
continue
}
metric.dimensionsUdObj = make(map[string]string)
metric.dimensionsUdArr = []map[string]string{}
metric.dimensionsUdArr = make([]map[string]string, 0)

// first try to unmarshal as an object
if err := json.Unmarshal([]byte(metric.Dimensions), &metric.dimensionsUdObj); err == nil {
Expand Down
15 changes: 5 additions & 10 deletions plugins/inputs/aliyuncms/aliyuncms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,7 @@ func TestPluginMetricsInitialize(t *testing.T) {
accessKeySecret: "dummy",
metrics: []*metric{
{
MetricNames: []string{},
Dimensions: `{"instanceId": "i-abcdefgh123456"}`,
Dimensions: `{"instanceId": "i-abcdefgh123456"}`,
},
},
},
Expand All @@ -263,8 +262,7 @@ func TestPluginMetricsInitialize(t *testing.T) {
accessKeySecret: "dummy",
metrics: []*metric{
{
MetricNames: []string{},
Dimensions: `[{"instanceId": "p-example"},{"instanceId": "q-example"}]`,
Dimensions: `[{"instanceId": "p-example"},{"instanceId": "q-example"}]`,
},
},
},
Expand All @@ -277,8 +275,7 @@ func TestPluginMetricsInitialize(t *testing.T) {
expectedErrorString: `cannot parse dimensions (neither obj, nor array) "[": unexpected end of JSON input`,
metrics: []*metric{
{
MetricNames: []string{},
Dimensions: `[`,
Dimensions: `[`,
},
},
},
Expand Down Expand Up @@ -344,8 +341,7 @@ func TestGatherMetric(t *testing.T) {
}

metric := &metric{
MetricNames: []string{},
Dimensions: `"instanceId": "i-abcdefgh123456"`,
Dimensions: `"instanceId": "i-abcdefgh123456"`,
}

tests := []struct {
Expand Down Expand Up @@ -375,8 +371,7 @@ func TestGatherMetric(t *testing.T) {

func TestGather(t *testing.T) {
m := &metric{
MetricNames: []string{},
Dimensions: `{"instanceId": "i-abcdefgh123456"}`,
Dimensions: `{"instanceId": "i-abcdefgh123456"}`,
}
plugin := &AliyunCMS{
AccessKeyID: "my_access_key_id",
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/amd_rocm_smi/amd_rocm_smi.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (rsmi *ROCmSMI) pollROCmSMI() ([]byte, error) {
}

func genTagsFields(gpus map[string]gpu, system map[string]sysInfo) []metric {
metrics := []metric{}
metrics := make([]metric, 0, len(gpus))
for cardID := range gpus {
if strings.Contains(cardID, "card") {
tags := map[string]string{
Expand Down
8 changes: 4 additions & 4 deletions plugins/inputs/azure_monitor/azure_monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ func TestGather_Success(t *testing.T) {

am.receiver, err = receiver.NewAzureMonitorMetricsReceiver(
am.SubscriptionID,
receiver.NewTargets(resourceTargets, []*receiver.ResourceGroupTarget{}, []*receiver.Resource{}),
receiver.NewTargets(resourceTargets, nil, nil),
azureClients,
)
require.NoError(t, err)
Expand Down Expand Up @@ -1010,7 +1010,7 @@ func TestGather_China_Success(t *testing.T) {

am.receiver, err = receiver.NewAzureMonitorMetricsReceiver(
am.SubscriptionID,
receiver.NewTargets(resourceTargets, []*receiver.ResourceGroupTarget{}, []*receiver.Resource{}),
receiver.NewTargets(resourceTargets, nil, nil),
azureClients,
)
require.NoError(t, err)
Expand Down Expand Up @@ -1043,7 +1043,7 @@ func TestGather_Government_Success(t *testing.T) {

am.receiver, err = receiver.NewAzureMonitorMetricsReceiver(
am.SubscriptionID,
receiver.NewTargets(resourceTargets, []*receiver.ResourceGroupTarget{}, []*receiver.Resource{}),
receiver.NewTargets(resourceTargets, nil, nil),
azureClients,
)
require.NoError(t, err)
Expand Down Expand Up @@ -1076,7 +1076,7 @@ func TestGather_Public_Success(t *testing.T) {

am.receiver, err = receiver.NewAzureMonitorMetricsReceiver(
am.SubscriptionID,
receiver.NewTargets(resourceTargets, []*receiver.ResourceGroupTarget{}, []*receiver.Resource{}),
receiver.NewTargets(resourceTargets, nil, nil),
azureClients,
)
require.NoError(t, err)
Expand Down
Loading

0 comments on commit 10b4311

Please sign in to comment.