Skip to content

Commit

Permalink
adjust stats sampling order
Browse files Browse the repository at this point in the history
Signed-off-by: Xinfeng Liu <XinfengLiu@icloud.com>
  • Loading branch information
xinfengliu committed Sep 29, 2023
1 parent 95aea39 commit 48aece9
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions daemon/stats.go
Expand Up @@ -156,28 +156,30 @@ func (daemon *Daemon) unsubscribeToContainerStats(c *container.Container, ch cha

// GetContainerStats collects all the stats published by a container
func (daemon *Daemon) GetContainerStats(container *container.Container) (*types.StatsJSON, error) {
var stats *types.StatsJSON
var err error

stats, err = daemon.stats(container)
var systemUsage uint64
var onlineCPUs uint32
stats, err := daemon.stats(container)
if err != nil {
goto done
}
// Sample system CPU usage close to container usage to avoid
// noise in metric calculations.
systemUsage, onlineCPUs, err = getSystemCPUUsage()
if err != nil {
goto done
}
// FIXME: move to containerd on Linux (not Windows)
stats.CPUStats.SystemUsage = systemUsage
stats.CPUStats.OnlineCPUs = onlineCPUs

// We already have the network stats on Windows directly from HCS.
if err == nil && !container.Config.NetworkDisabled && runtime.GOOS != "windows" {
if !container.Config.NetworkDisabled && runtime.GOOS != "windows" {
stats.Networks, err = daemon.getNetworkStats(container)
}

done:
switch err.(type) {
case nil:
// Sample system CPU usage close to container usage to avoid
// noise in metric calculations.
systemUsage, onlineCPUs, err := getSystemCPUUsage()
if err != nil {
log.G(context.TODO()).WithError(err).WithField("container_id", container.ID).Errorf("collecting system cpu usage")
return nil, err
}
// FIXME: move to containerd on Linux (not Windows)
stats.CPUStats.SystemUsage = systemUsage
stats.CPUStats.OnlineCPUs = onlineCPUs
return stats, nil
case errdefs.ErrConflict, errdefs.ErrNotFound:
// return empty stats containing only name and ID if not running or not found
Expand All @@ -186,7 +188,7 @@ func (daemon *Daemon) GetContainerStats(container *container.Container) (*types.
ID: container.ID,
}, nil
default:
log.G(context.TODO()).Errorf("collecting stats for container %s: %v", container.ID, err)
log.G(context.TODO()).Errorf("collecting stats for container %s: %v", container.Name, err)
return nil, err
}
}

0 comments on commit 48aece9

Please sign in to comment.