Skip to content

Commit

Permalink
Merge pull request #859 from forta-network/kisel/improve-docker-cpu-m…
Browse files Browse the repository at this point in the history
…etric

Calculate docker cpu percentage
  • Loading branch information
dkeysil authored Apr 12, 2024
2 parents df3194e + 05bcb9a commit c34c465
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
2 changes: 2 additions & 0 deletions clients/docker/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,8 @@ func (d *dockerClient) ContainerStats(ctx context.Context, id string) (*Containe
CPUUsage: CPUUsage{
TotalUsage: containerResources.CPUStats.CPUUsage.TotalUsage,
},
SystemUsage: containerResources.CPUStats.SystemUsage,
OnlineCPUs: containerResources.CPUStats.OnlineCPUs,
},
MemoryStats: MemoryStats{
Usage: containerResources.MemoryStats.Usage,
Expand Down
4 changes: 3 additions & 1 deletion clients/docker/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ type ContainerResources struct {
}

type CPUStats struct {
CPUUsage CPUUsage `json:"cpu_usage"`
CPUUsage CPUUsage `json:"cpu_usage"`
SystemUsage uint64 `json:"system_cpu_usage"`
OnlineCPUs uint32 `json:"online_cpus"`
}

type CPUUsage struct {
Expand Down
23 changes: 22 additions & 1 deletion services/components/containers/bot_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ func (bc *botClient) pollDockerResources(containerID string, agentConfig config.
ticker := initTicker(DockerResourcesPollingInterval)
defer ticker.Stop()

var previousResources docker.ContainerResources

for t := range ticker.C {
logrus.WithField("containerID", containerID).Debug("polling docker resources")
// request docker stats
Expand Down Expand Up @@ -159,17 +161,36 @@ func (bc *botClient) pollDockerResources(containerID string, agentConfig config.
WithField("resources", resources).
Debug("sending docker resources metrics")

cpuPercent := calculateCPUPercentUnix(&previousResources, resources)

metrics.SendAgentMetrics(bc.msgClient, []*protocol.AgentMetric{
metrics.CreateAgentResourcesMetric(
agentConfig, t, domain.MetricDockerResourcesCPU, float64(resources.CPUStats.CPUUsage.TotalUsage)),
agentConfig, t, domain.MetricDockerResourcesCPU, cpuPercent),
metrics.CreateAgentResourcesMetric(
agentConfig, t, domain.MetricDockerResourcesMemory, float64(resources.MemoryStats.Usage)),
metrics.CreateAgentResourcesMetric(
agentConfig, t, domain.MetricDockerResourcesNetworkSent, float64(bytesSent)),
metrics.CreateAgentResourcesMetric(
agentConfig, t, domain.MetricDockerResourcesNetworkReceive, float64(bytesRecv)),
})

previousResources = *resources
}
}

func calculateCPUPercentUnix(previousResources, resources *docker.ContainerResources) float64 {
var (
cpuPercent = 0.0
// calculate the change for the cpu usage of the container in between readings
cpuDelta = float64(resources.CPUStats.CPUUsage.TotalUsage) - float64(previousResources.CPUStats.CPUUsage.TotalUsage)
// calculate the change for the entire system between readings
systemDelta = float64(resources.CPUStats.SystemUsage) - float64(previousResources.CPUStats.SystemUsage)
)

if systemDelta > 0.0 && cpuDelta > 0.0 {
cpuPercent = (cpuDelta / systemDelta) * float64(resources.CPUStats.OnlineCPUs) * 100.0
}
return cpuPercent
}

func (bc *botClient) attachServiceContainers(ctx context.Context, botNetworkID string) error {
Expand Down
2 changes: 1 addition & 1 deletion services/components/containers/bot_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (s *BotClientTestSuite) TestLaunchBot_Exists() {
// CPU metric
assert.Equal(s.T(), botConfig.ID, metrics.Metrics[0].AgentId)
assert.Equal(s.T(), domain.MetricDockerResourcesCPU, metrics.Metrics[0].Name)
assert.Equal(s.T(), float64(33), metrics.Metrics[0].Value)
assert.Equal(s.T(), float64(0), metrics.Metrics[0].Value)

// Memory metric
assert.Equal(s.T(), botConfig.ID, metrics.Metrics[1].AgentId)
Expand Down

0 comments on commit c34c465

Please sign in to comment.