Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[receiver/awsecscontainermetrics] Fix possible panics from unchecked de-references #23644

Merged
merged 14 commits into from
Jun 23, 2023
Merged
20 changes: 20 additions & 0 deletions .chloggen/ecsRecvDeRefCheck.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use this changelog template to create an entry for release notes.
# If your change doesn't affect end users, such as a test fix or a tooling change,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: awsecscontainermetricsreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fix possible panics in awsecscontainerrmetrics receiver
bryan-aguilar marked this conversation as resolved.
Show resolved Hide resolved

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [23644]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
1 change: 1 addition & 0 deletions receiver/awsecscontainermetricsreceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/open-telemetry/opentelemetry-collector-contrib/receiver/awsecs
go 1.19

require (
github.com/aws/aws-sdk-go v1.44.287
github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/ecsutil v0.80.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/common v0.80.0
github.com/stretchr/testify v1.8.4
Expand Down
18 changes: 18 additions & 0 deletions receiver/awsecscontainermetricsreceiver/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package awsecscontainermetrics // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/awsecscontainermetricsreceiver/internal/awsecscontainermetrics"

import (
"github.com/aws/aws-sdk-go/aws"
"go.uber.org/zap"
)

Expand All @@ -12,12 +13,12 @@ func getContainerMetrics(stats *ContainerStats, logger *zap.Logger) ECSMetrics {
m := ECSMetrics{}

if stats.Memory != nil {
m.MemoryUsage = *stats.Memory.Usage
m.MemoryMaxUsage = *stats.Memory.MaxUsage
m.MemoryLimit = *stats.Memory.Limit
m.MemoryUsage = aws.Uint64Value(stats.Memory.Usage)
m.MemoryMaxUsage = aws.Uint64Value(stats.Memory.MaxUsage)
m.MemoryLimit = aws.Uint64Value(stats.Memory.Limit)

if stats.Memory.Stats != nil {
m.MemoryUtilized = (*stats.Memory.Usage - stats.Memory.Stats["cache"]) / bytesInMiB
m.MemoryUtilized = (aws.Uint64Value(stats.Memory.Usage) - stats.Memory.Stats["cache"]) / bytesInMiB
}
} else {
logger.Debug("Nil memory stats found for docker container:" + stats.Name)
Expand All @@ -29,26 +30,26 @@ func getContainerMetrics(stats *ContainerStats, logger *zap.Logger) ECSMetrics {

cpuUsageInVCpu := float64(0)
if timeDiffSinceLastRead > 0 {
cpuDelta := (float64)(*stats.CPU.CPUUsage.TotalUsage - *stats.PreviousCPU.CPUUsage.TotalUsage)
cpuDelta := (float64)(aws.Uint64Value(stats.CPU.CPUUsage.TotalUsage) - aws.Uint64Value(stats.PreviousCPU.CPUUsage.TotalUsage))
cpuUsageInVCpu = cpuDelta / timeDiffSinceLastRead
}
cpuUtilized := cpuUsageInVCpu * 100

m.CPUTotalUsage = *stats.CPU.CPUUsage.TotalUsage
m.CPUUsageInKernelmode = *stats.CPU.CPUUsage.UsageInKernelmode
m.CPUUsageInUserMode = *stats.CPU.CPUUsage.UsageInUserMode
m.CPUTotalUsage = aws.Uint64Value(stats.CPU.CPUUsage.TotalUsage)
m.CPUUsageInKernelmode = aws.Uint64Value(stats.CPU.CPUUsage.UsageInKernelmode)
m.CPUUsageInUserMode = aws.Uint64Value(stats.CPU.CPUUsage.UsageInUserMode)
m.NumOfCPUCores = numOfCores
m.CPUOnlineCpus = *stats.CPU.OnlineCpus
m.SystemCPUUsage = *stats.CPU.SystemCPUUsage
m.CPUOnlineCpus = aws.Uint64Value(stats.CPU.OnlineCpus)
m.SystemCPUUsage = aws.Uint64Value(stats.CPU.SystemCPUUsage)
m.CPUUsageInVCPU = cpuUsageInVCpu
m.CPUUtilized = cpuUtilized
} else {
logger.Debug("Nil CPUUsage stats found for docker container:" + stats.Name)
}

if stats.NetworkRate != nil {
m.NetworkRateRxBytesPerSecond = *stats.NetworkRate.RxBytesPerSecond
m.NetworkRateTxBytesPerSecond = *stats.NetworkRate.TxBytesPerSecond
m.NetworkRateRxBytesPerSecond = aws.Float64Value(stats.NetworkRate.RxBytesPerSecond)
m.NetworkRateTxBytesPerSecond = aws.Float64Value(stats.NetworkRate.TxBytesPerSecond)
} else {
logger.Debug("Nil NetworkRate stats found for docker container:" + stats.Name)
}
Expand Down Expand Up @@ -84,15 +85,15 @@ func getContainerMetrics(stats *ContainerStats, logger *zap.Logger) ECSMetrics {
func getNetworkStats(stats map[string]NetworkStats) [8]uint64 {
var netStatArray [8]uint64
for _, netStat := range stats {
netStatArray[0] += *netStat.RxBytes
netStatArray[1] += *netStat.RxPackets
netStatArray[2] += *netStat.RxErrors
netStatArray[3] += *netStat.RxDropped

netStatArray[4] += *netStat.TxBytes
netStatArray[5] += *netStat.TxPackets
netStatArray[6] += *netStat.TxErrors
netStatArray[7] += *netStat.TxDropped
netStatArray[0] += aws.Uint64Value(netStat.RxBytes)
netStatArray[1] += aws.Uint64Value(netStat.RxPackets)
netStatArray[2] += aws.Uint64Value(netStat.RxErrors)
netStatArray[3] += aws.Uint64Value(netStat.RxDropped)

netStatArray[4] += aws.Uint64Value(netStat.TxBytes)
netStatArray[5] += aws.Uint64Value(netStat.TxPackets)
netStatArray[6] += aws.Uint64Value(netStat.TxErrors)
netStatArray[7] += aws.Uint64Value(netStat.TxDropped)
}
return netStatArray
}
Expand All @@ -108,9 +109,9 @@ func extractStorageUsage(stats *DiskStats) (uint64, uint64) {
for _, blockStat := range stats.IoServiceBytesRecursives {
switch op := blockStat.Op; op {
case "Read":
readBytes = *blockStat.Value
readBytes = aws.Uint64Value(blockStat.Value)
case "Write":
writeBytes = *blockStat.Value
writeBytes = aws.Uint64Value(blockStat.Value)
default:
// ignoring "Async", "Total", "Sum", etc
continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,112 @@ func TestGetContainerMetricsMissingMemory(t *testing.T) {
require.EqualValues(t, v, containerMetrics.StorageWriteBytes)
}

func TestGetContainerDereferenceCheck(t *testing.T) {

tests := []struct {
memoryStats MemoryStats
testName string
cpuStats CPUStats
networkRate NetworkRateStats
prevCpuStats CPUStats
}{
{
memoryStats: MemoryStats{
Usage: nil,
MaxUsage: nil,
Limit: nil,
MemoryReserved: nil,
MemoryUtilized: nil,
Stats: nil,
},
testName: "nil memory stats values",
cpuStats: cpuStats,
networkRate: netRate,
prevCpuStats: previousCPUStats,
},
{
memoryStats: mem,
testName: "nil cpuStats values",
cpuStats: CPUStats{
CPUUsage: &cpuUsage,
OnlineCpus: nil,
SystemCPUUsage: nil,
CPUReserved: nil,
CPUUtilized: nil,
},
networkRate: netRate,
prevCpuStats: previousCPUStats,
},
{
memoryStats: mem,
testName: "nil cpuUsage and CPUstats values",
cpuStats: CPUStats{
CPUUsage: &CPUUsage{
TotalUsage: nil,
UsageInKernelmode: nil,
UsageInUserMode: nil,
PerCPUUsage: nil,
},
OnlineCpus: nil,
SystemCPUUsage: nil,
CPUReserved: nil,
CPUUtilized: nil,
},
networkRate: netRate,
prevCpuStats: previousCPUStats,
},
{
memoryStats: mem,
testName: "nil network rate values",
cpuStats: cpuStats,
networkRate: NetworkRateStats{
RxBytesPerSecond: nil,
TxBytesPerSecond: nil,
},
prevCpuStats: previousCPUStats,
},
{
memoryStats: mem,
testName: "nil prev cpu stats values",
cpuStats: cpuStats,
networkRate: netRate,
prevCpuStats: CPUStats{
CPUUsage: &CPUUsage{
Aneurysm9 marked this conversation as resolved.
Show resolved Hide resolved
TotalUsage: nil,
UsageInKernelmode: nil,
UsageInUserMode: nil,
PerCPUUsage: nil,
},
OnlineCpus: nil,
SystemCPUUsage: nil,
CPUReserved: nil,
CPUUtilized: nil,
},
},
}

for _, test := range tests {
t.Run(test.testName, func(t *testing.T) {
containerStats = ContainerStats{
Name: "test",
ID: "001",
Read: time.Now(),
PreviousRead: time.Now().Add(-10 * time.Second),
Memory: &test.memoryStats,
Disk: &disk,
Network: net,
NetworkRate: &test.networkRate,
CPU: &test.cpuStats,
PreviousCPU: &test.prevCpuStats,
}

require.NotPanics(t, func() {
getContainerMetrics(&containerStats, logger)
})
})
}
}

func TestGetContainerMetricsMissingCpu(t *testing.T) {
containerStats = ContainerStats{
Name: "test",
Expand Down Expand Up @@ -277,6 +383,21 @@ func TestExtractStorageUsage(t *testing.T) {
require.EqualValues(t, v, write)
}

func TestExtractStorageUsageDereferenceCheck(t *testing.T) {
disk := &DiskStats{
IoServiceBytesRecursives: []IoServiceBytesRecursive{
{Op: "Read", Value: nil},
{Op: "Write", Value: nil},
{Op: "Total", Value: nil},
},
}

require.NotPanics(t, func() {
extractStorageUsage(disk)
})

}

func TestGetNetworkStats(t *testing.T) {
v := uint64(100)
stats := make(map[string]NetworkStats)
Expand All @@ -299,3 +420,21 @@ func TestGetNetworkStats(t *testing.T) {
}
require.EqualValues(t, 800, sum)
}

func TestGetNetworkStatsDereferenceCheck(t *testing.T) {
stats := make(map[string]NetworkStats)
stats["eth0"] = NetworkStats{
RxBytes: nil,
RxPackets: nil,
RxErrors: nil,
RxDropped: nil,
TxBytes: nil,
TxPackets: nil,
TxErrors: nil,
TxDropped: nil,
}

require.NotPanics(t, func() {
getNetworkStats(stats)
})
}