Skip to content

Commit

Permalink
[chore] [receiver/awscontainerinsight] preallocate slices (open-telem…
Browse files Browse the repository at this point in the history
  • Loading branch information
atoulme authored Aug 7, 2023
1 parent 32e4da5 commit c13c645
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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_
Expand All @@ -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{})

Expand All @@ -68,15 +68,15 @@ 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
for k, v := range netIfceMetric {
metric.fields[ci.MetricName(mType, k)] = v
}

metrics = append(metrics, metric)
metrics[i] = metric
}

aggregatedFields := ci.SumFields(netIfceMetrics)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c13c645

Please sign in to comment.