measure docker network stats per second

This commit is contained in:
Henry Dollman
2024-08-05 18:58:00 -04:00
parent c6b9f1ab77
commit a73a01fe37
2 changed files with 21 additions and 13 deletions

View File

@@ -234,41 +234,45 @@ func getContainerStats(ctr *Container) (*ContainerStats, error) {
defer containerStatsMutex.Unlock() defer containerStatsMutex.Unlock()
// add empty values if they doesn't exist in map // add empty values if they doesn't exist in map
_, initialized := containerStatsMap[ctr.IdShort] stats, initialized := containerStatsMap[ctr.IdShort]
if !initialized { if !initialized {
containerStatsMap[ctr.IdShort] = &PrevContainerStats{} stats = &PrevContainerStats{}
containerStatsMap[ctr.IdShort] = stats
} }
// cpu // cpu
cpuDelta := statsJson.CPUStats.CPUUsage.TotalUsage - containerStatsMap[ctr.IdShort].Cpu[0] cpuDelta := statsJson.CPUStats.CPUUsage.TotalUsage - stats.Cpu[0]
systemDelta := statsJson.CPUStats.SystemUsage - containerStatsMap[ctr.IdShort].Cpu[1] systemDelta := statsJson.CPUStats.SystemUsage - stats.Cpu[1]
cpuPct := float64(cpuDelta) / float64(systemDelta) * 100 cpuPct := float64(cpuDelta) / float64(systemDelta) * 100
if cpuPct > 100 { if cpuPct > 100 {
return &ContainerStats{}, fmt.Errorf("%s cpu pct greater than 100: %+v", name, cpuPct) return &ContainerStats{}, fmt.Errorf("%s cpu pct greater than 100: %+v", name, cpuPct)
} }
containerStatsMap[ctr.IdShort].Cpu = [2]uint64{statsJson.CPUStats.CPUUsage.TotalUsage, statsJson.CPUStats.SystemUsage} stats.Cpu = [2]uint64{statsJson.CPUStats.CPUUsage.TotalUsage, statsJson.CPUStats.SystemUsage}
// network // network
var total_sent, total_recv, sent_delta, recv_delta uint64 var total_sent, total_recv uint64
for _, v := range statsJson.Networks { for _, v := range statsJson.Networks {
total_sent += v.TxBytes total_sent += v.TxBytes
total_recv += v.RxBytes total_recv += v.RxBytes
} }
var sent_delta, recv_delta float64
// prevent first run from sending all prev sent/recv bytes // prevent first run from sending all prev sent/recv bytes
if initialized { if initialized {
sent_delta = total_sent - containerStatsMap[ctr.IdShort].Net[0] secondsElapsed := time.Since(stats.Net.Time).Seconds()
recv_delta = total_recv - containerStatsMap[ctr.IdShort].Net[1] sent_delta = float64(total_sent-stats.Net.Sent) / secondsElapsed
recv_delta = float64(total_recv-stats.Net.Recv) / secondsElapsed
// log.Printf("sent delta: %+v, recv delta: %+v\n", sent_delta, recv_delta) // log.Printf("sent delta: %+v, recv delta: %+v\n", sent_delta, recv_delta)
} }
containerStatsMap[ctr.IdShort].Net = [2]uint64{total_sent, total_recv} stats.Net.Sent = total_sent
stats.Net.Recv = total_recv
stats.Net.Time = time.Now()
cStats := &ContainerStats{ cStats := &ContainerStats{
Name: name, Name: name,
Cpu: twoDecimals(cpuPct), Cpu: twoDecimals(cpuPct),
Mem: bytesToMegabytes(float64(usedMemory)), Mem: bytesToMegabytes(float64(usedMemory)),
NetworkSent: bytesToMegabytes(float64(sent_delta)), NetworkSent: bytesToMegabytes(sent_delta),
NetworkRecv: bytesToMegabytes(float64(recv_delta)), NetworkRecv: bytesToMegabytes(recv_delta),
// MemPct: twoDecimals(pctMemory),
} }
return cStats, nil return cStats, nil
} }

View File

@@ -167,5 +167,9 @@ type NetIoStats struct {
type PrevContainerStats struct { type PrevContainerStats struct {
Cpu [2]uint64 Cpu [2]uint64
Net [2]uint64 Net struct {
Sent uint64
Recv uint64
Time time.Time
}
} }