diff --git a/receiver/awscontainerinsightreceiver/internal/cadvisor/extractors/extractor.go b/receiver/awscontainerinsightreceiver/internal/cadvisor/extractors/extractor.go index b80d67e06283..81377adde71b 100644 --- a/receiver/awscontainerinsightreceiver/internal/cadvisor/extractors/extractor.go +++ b/receiver/awscontainerinsightreceiver/internal/cadvisor/extractors/extractor.go @@ -136,7 +136,7 @@ func assignRateValueToField(rateCalculator *awsmetrics.MetricCalculator, fields // MergeMetrics merges an array of cadvisor metrics based on common metric keys func MergeMetrics(metrics []*CAdvisorMetric) []*CAdvisorMetric { - var result []*CAdvisorMetric + result := make([]*CAdvisorMetric, 0, len(metrics)) metricMap := make(map[string]*CAdvisorMetric) for _, metric := range metrics { if metricKey := getMetricKey(metric); metricKey != "" { @@ -159,7 +159,7 @@ func MergeMetrics(metrics []*CAdvisorMetric) []*CAdvisorMetric { // return MetricKey for merge-able metrics func getMetricKey(metric *CAdvisorMetric) string { metricType := metric.GetMetricType() - metricKey := "" + var metricKey string switch metricType { case ci.TypeInstance: // merge cpu, memory, net metric for type Instance diff --git a/receiver/awscontainerinsightreceiver/internal/cadvisor/extractors/fs_extractor.go b/receiver/awscontainerinsightreceiver/internal/cadvisor/extractors/fs_extractor.go index 9ff3cfe5254c..559e4af41ec3 100644 --- a/receiver/awscontainerinsightreceiver/internal/cadvisor/extractors/fs_extractor.go +++ b/receiver/awscontainerinsightreceiver/internal/cadvisor/extractors/fs_extractor.go @@ -24,13 +24,13 @@ func (f *FileSystemMetricExtractor) HasValue(info *cinfo.ContainerInfo) bool { } func (f *FileSystemMetricExtractor) GetValue(info *cinfo.ContainerInfo, _ CPUMemInfoProvider, containerType string) []*CAdvisorMetric { - var metrics []*CAdvisorMetric if containerType == ci.TypePod || containerType == ci.TypeInfraContainer { - return metrics + return nil } containerType = getFSMetricType(containerType, f.logger) stats := GetStats(info) + metrics := make([]*CAdvisorMetric, 0, len(stats.Filesystem)) for _, v := range stats.Filesystem { metric := newCadvisorMetric(containerType, f.logger) diff --git a/receiver/awscontainerinsightreceiver/internal/cadvisor/extractors/net_extractor.go b/receiver/awscontainerinsightreceiver/internal/cadvisor/extractors/net_extractor.go index 50308b96e131..3b66db8fe53a 100644 --- a/receiver/awscontainerinsightreceiver/internal/cadvisor/extractors/net_extractor.go +++ b/receiver/awscontainerinsightreceiver/internal/cadvisor/extractors/net_extractor.go @@ -31,11 +31,10 @@ func (n *NetMetricExtractor) HasValue(info *cinfo.ContainerInfo) bool { } func (n *NetMetricExtractor) GetValue(info *cinfo.ContainerInfo, _ CPUMemInfoProvider, containerType string) []*CAdvisorMetric { - var metrics []*CAdvisorMetric // Just a protection here, there is no Container level Net metrics if containerType == ci.TypePod || containerType == ci.TypeContainer { - return metrics + return nil } // Rename type to pod so the metric name prefix is pod_ @@ -47,9 +46,10 @@ func (n *NetMetricExtractor) GetValue(info *cinfo.ContainerInfo, _ CPUMemInfoPro curIfceStats := getInterfacesStats(curStats) // used for aggregation - var netIfceMetrics []map[string]interface{} + netIfceMetrics := make([]map[string]interface{}, len(curIfceStats)) + metrics := make([]*CAdvisorMetric, len(curIfceStats)) - for _, cur := range curIfceStats { + for i, cur := range curIfceStats { mType := getNetMetricType(containerType, n.logger) netIfceMetric := make(map[string]interface{}) @@ -68,7 +68,7 @@ func (n *NetMetricExtractor) GetValue(info *cinfo.ContainerInfo, _ CPUMemInfoPro netIfceMetric[ci.NetTotalBytes] = netIfceMetric[ci.NetRxBytes].(float64) + netIfceMetric[ci.NetTxBytes].(float64) } - netIfceMetrics = append(netIfceMetrics, netIfceMetric) + netIfceMetrics[i] = netIfceMetric metric := newCadvisorMetric(mType, n.logger) metric.tags[ci.NetIfce] = cur.Name @@ -76,7 +76,7 @@ func (n *NetMetricExtractor) GetValue(info *cinfo.ContainerInfo, _ CPUMemInfoPro metric.fields[ci.MetricName(mType, k)] = v } - metrics = append(metrics, metric) + metrics[i] = metric } aggregatedFields := ci.SumFields(netIfceMetrics) diff --git a/receiver/awscontainerinsightreceiver/internal/cadvisor/testutils/helpers.go b/receiver/awscontainerinsightreceiver/internal/cadvisor/testutils/helpers.go index 6bdc8b5a2d74..6e8b9e2d4bc7 100644 --- a/receiver/awscontainerinsightreceiver/internal/cadvisor/testutils/helpers.go +++ b/receiver/awscontainerinsightreceiver/internal/cadvisor/testutils/helpers.go @@ -17,13 +17,15 @@ func LoadContainerInfo(t *testing.T, file string) []*cinfo.ContainerInfo { info, err := os.ReadFile(file) assert.Nil(t, err, "Fail to read file content") - var result []*cinfo.ContainerInfo containers := map[string]*cinfo.ContainerInfo{} err = json.Unmarshal(info, &containers) assert.Nil(t, err, "Fail to parse json string") + result := make([]*cinfo.ContainerInfo, len(containers)) + i := 0 for _, containerInfo := range containers { - result = append(result, containerInfo) + result[i] = containerInfo + i++ } var b bytes.Buffer